C# attributes.
Could someone refresh my memory as to what a C# attribute is, I can''t remember. And how they might/are used?
thanks in advance,
L-
Attributes are a handy way of writing
SetX()
GetX()
but without the Set and Get. They provide a means to control how a variable is modified but can still be used like a variable instead of a function.
so if you had an attribute
protected int x;
X {
set() { x = value; }
get() { return x; }
}
then in your code, you could write
X = 10;
y = X + 1;
(P.S. The syntax of the attribute isn''t quiet right, but I hope you get the drift)
SetX()
GetX()
but without the Set and Get. They provide a means to control how a variable is modified but can still be used like a variable instead of a function.
so if you had an attribute
protected int x;
X {
set() { x = value; }
get() { return x; }
}
then in your code, you could write
X = 10;
y = X + 1;
(P.S. The syntax of the attribute isn''t quiet right, but I hope you get the drift)
Actually, what you have described is a property. Properties are a replacement for accessor methods.
An attribute is as follows:
-->[STATThread]<-- Attribute
static void Main(string[] args)
They are used to give certain properties to different variable/methods. Basically they allow you to put information onto things that can be retrieved at runtime through System.Reflection.
An attribute is as follows:
-->[STATThread]<-- Attribute
static void Main(string[] args)
They are used to give certain properties to different variable/methods. Basically they allow you to put information onto things that can be retrieved at runtime through System.Reflection.
quote:Original post by jperalta
-->[STATThread]<-- Attribute
static void Main(string[] args)
They are used to give certain properties to different variable/methods. Basically they allow you to put information onto things that can be retrieved at runtime through System.Reflection.
There are also some attributes that are interpreted directly by the compiler, and used to direct the code generation. For example, whenever the compiler sees the DllImportAttribute attribute, it knows that the function in question should be imported from a DLL:
[DllImport( "kernel32.dll" )]public static void extern SetWindowText( IntPtr hWnd, string text );
You can also apply attributes to classes, interfaces, return values and method parameters.
For the project in my signature, I have a class defined for each implemented VS.NET command. When the addin loads, it searches through an assembly for classes implementing the ICommand interface(using the reflection APIs), and that has the VSNetCommandAttribute attribute applied. It then creates an instance of that class, and uses the information embedded in the VSNetCommandAttribute and VSNetControlAttribute attributes to attach the command to the VS.NET UI. See http://ankhsvn.com:8088/viewcvs/trunk/src/Ankh/Commands/CleanupCommand.cs?rev=674&view=markup for an example.
--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement