Programming in C Attributes CSE 494 R proposed
Programming in C# Attributes CSE 494 R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
Attributes l Many systems have a need to decorate code with additional information. l Traditional solutions Add keywords or pragma’s to language l Use external files, e. g. , . IDL, . DEF l l C# l solution: Attributes Metadata – descriptive elements that decorate types and members (assembly, module, type, member, return value and parameter).
Attributes - Example l Attributes are classes; they inherit from System. Attribute class Help. Url. Attribute : System. Attribute { public Help. Url. Attribute(string url) { … } l Attach an attribute to a class, type, etc. [Help. Url(“http: //Some. Url/APIDocs/Some. Class”)] class Some. Class { … }
Attributes - Example l Attributes can be queried at runtime by reflection: Type type = typeof(My. Class); foreach (object attr in type. Get. Custom. Attributes()) { if (attr is Help. Url. Attribute) { Help. Url. Attribute help = (Help. Url. Attribute) attr; my. Browser. Navigate(help. Url); } }
Uses of Attributes in. Net Provide custom additions to metadata for managed types l Support serialization l Support debugging and tracing l Set COM+ attributes l l l l Activation, queuing, security, events, contexts, object pooling, synchronization, transactions Support creation of COM objects Support creation of. Net controls Support creation of Web Services Create ATL Server code – essentially builds ISAPI filters Implement performance counters Implement OLEDB consumers
Kinds of Attributes l Custom attributes l l Distinguished custom attributes l l l Add entries to metadata but are not used by run-time These attributes have data stored in the assembly next to the items to which it applies. One. Way is a distinguished custom attribute that affects marshaling by the run-time Pseudo custom attributes l l Changes, does not extend existing metadata Serializable is a pseudo custom attribute. It sets or resets the metadata flag td. Serializable
Defining Custom Attributes l Create a class marked with the Attribute. Usage attribute [Attribute. Usage(Attribute. Targets. All, Allow. Multiple=true)] class my. Attribute : System. Attribute { … } l Targets include: l Assembly, Class, Delegate, Event, Field, Method, …, All The attribute class provides a constructor some state, and properties to retrieve the state. l The state is stored in the metadata of the assembly that implements the attributed target. l It is retrieved using the Reflection API. l
Attributes - Example l Attributes are classes; they inherit from System. Attribute class Help. Url. Attribute : System. Attribute { public Help. Url. Attribute(string url) { … } … Note, it is allowed and customary } to remove the Attribute suffix from the type name. l Attach an attribute to a class, type, etc. [Help. Url(“http: //Some. Url/APIDocs/Some. Class”)] class Some. Class { … }
Provided Attributes in. Net l l l l l [CLSCompliant(true)] - class fails to compile if not compliant [Conditional(“Debug”)] - won’t get called unless Debug defined [Assembly: Assembly. Title(“…”)] - assembly descriptions [Assembly: Assembly. Version(“ 1. 2”)] [Dll. Import(“kernel 32. dll”)] - accessing unmanaged global function public static extern int Beep(int freq, int dur); [Serializable()] - enabling serialization public class my. Class { … } [One. Way()] - marshal only to remote object public void my. Func(string msg) { … } [Synchronization()] - allow access by one thread at a time class Some. Class : Context. Bound. Object { … } [Obsolete()] - generates a compiler error when used
Design-Time and Security Attributes used with user defined controls l [Category(“Custom Properties”)] - makes property page category l [Default. Event(my. Event)] - double click on control to wire up l [Description(“my. Propert. Desc”)] - description shown when selected l [Tool. Box. Bitmap(“my. Bit. Map. bmp”)] – defines bitmap used in toolbox Declarative security settings l [File. IOPermission(Security. Action. Deny, Read=@”c: WindowsSystem 32”)] public in Read. File(string path) { … }
Preprocessor Directives C# provides preprocessor directives that serve a number of functions l Unlike C++, there is not a separate preprocessor l l l The “preprocessor” name is preserved only for consistency with C++ Some C++ preprocessor features removed: l l #include: Not needed Macro version of #define: removed for clarity
Preprocessor Directives Directive Description #define, #undef Define and undefine conditional symbols #if, #else, #endif Conditionally skip sections of code #error, #warning Issue errors and warnings #region, #end Delimit outline regions #line Specify line number
Conditional Compilation #define Debug public class Debug { [Conditional("Debug")] public static void Assert(bool condition, String msg) { if (!condition) { throw new Assertion. Exception(msg); } } void Do. Something() {. . . // If Debug is not defined, the next line is // not even called Assert((x == y), “X should equal Y”); . . . } }
Programming in C# Attributes CSE 494 R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
- Slides: 14