Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Lucidquiet

C# attributes.

This topic is 5559 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Guest Anonymous Poster
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!