There are numerous ways designers have coupled information with classes, fields, members of a class. Category of a property, transaction context for a method, persistence mapping, keywords to extend programming languages, using external files, etcetera, have been suggested by Anders Hejlsberg Microsoft. Each of these Have Advantages over each other and along with a usable solution.
While working for a previous employer, a major Dutch company that no longer exists, we created a programming environment for application programmers, where we wanted to hide the database details (including but not limited to which database was used) behind object-oriented programming. The idea was simple:. Application programmers (people using our environment) could create Java applications quickly without having to worry about database specific details They would also maintain metadata about classes in a XML-file per Java class, using a user interface We developed. an XML based development repository to store this metadata (XML files). The programming environment (implemented using Visual Studio shell extensions) and the development repository was written in C . The environment generated table creation / alteration code and stored procedures for a multitude of databases . Based on The Metadata for Each Java Class (Generated by Java Complier) Was 'Patch' To Insert Cal ls to the specific stored procedures. The clear advantage was code written once in Java could be made to perform differently without recompiling, simply by editing the associated XML. The disadvantage was XML and Java needed to be kept in sync. To overcome this hurdle, We Had to Develop Tools to Maintain Synchronicities. We Could Have Used C # 's Attributes INSTEAD OF WORRING ABOUT SYNCHRONIZATION.
C # introduced attributes, which allow you to embed information right into the C # source code. Attributes are placed in square brackets, eg [MTAThread], above an Attribute Target (eg class, field, method, etcetera). C # Attributes can be used to mark fields, methods, classes etcetera with markings that another program can interpret. For example you can send instructions to compiler to emit a warning message [Obsolete ( "This delegate should be avoided. Support may be discontinued in next version")] or generate an error for obsoleted methods. [Obsolete ( "This method has been obsoleted as of version 1.2.3.4", true)] See help on the [ObsoleteAttribute] in the C # language specification.Specify attributes between square brackets ([and]) in C and Between Angle Brackets (
Examples:
[STAThreadAttribute] public static int Main (string [] args) {} Indicates that the default threading model for an application is single-threaded apartment. Here the Attribute Target is Method. It also implies that there is a class (implemented by the environment in this case) by the name of STAThreadAttribute, with a parameter-less constructor STAThreadAttribute () [MTAThread] [MTAThread] is short for [MTAThreadAttribute], in C #. in other words "Attribute" suffix is optional. If you are working in a mixed environment, you should use the full name MTAThreadAttribute versus the shortcut MTAThread. [Obsolete] [Obsolete] can be used to mark an attribute Target as obsolete. The compiler picks this attribute and emits a message or generates an error [AttributeUsageAttribute (AttributeTargets .Field)] public class PrecisionAttribute {public void PrecisionAttribute (int length, int precision) {}} Indicates that the user defined attribute PrecisionAttribute can be applied to fields only. It also i ndicates that there is a constructor of name AttributeUsageAttribute (AttributeTargets t) in a class AttributeUsageAttribute. Again this class AttributeUsageAttribute is implemented by the .Net environment. [PrecisionAttribute (7, 3)] or [Precision (7, 3)] Indicates that there is a constructor of name PrecisionAttribute (int, int) in a class PrecisionAttribute, possibly implemented by you. [in] or [InAttribute] Both mean the same thing. [InAttribute, Out, CustomMarshaller (true, UnmanagedType.U4))] TraderIdentifier trIdor [In] [Out] [CustomMarshaller (true, UnmanagedType.U4)] TraderIdentifier trId; Attributes may be combined using the comma separator or listed separately within square brackets [DllImport ( "User32.dll".
] Public Static Extern INTPTR GETWINDOW (INTPTR HWND, INT UCMD); Instructs The Compiler To Insert Code To Allow Dynamic Access To DLL.SAMPLE CODE
Here Is A Sample That Shows How To Create a Custom Attribute and Create a Custom Attribute and Core To Use The Same Program The Formatting with The Declaration. For Example Consider these TWO Examples:
[Formatting ("### - ## - ####")] public int designcuritynumber; [FormattingAttribute "] public long phonenumber;
Class hierarchy
Here is The class hierarchy for the sample code.
Class AttributeSample houses the main function. FormattingAttribute is the code for the attribute and contains a private class Core with core functionality. Classes Person, USCitizen, USEmployee implement the ISupportsFormatting interface and the hierarchical relationship between them is shown in the class hierarchy below. ISupportsFormatting requires ................... ..
Code
Here Is The Code Sample In It's EntireTy. Enjoy!
Let me know what you think or if you can Find A Sharp Use of C # attributes.