.NET Framework IL: How can I extend Metadata using attributes?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: .NET Framework IL: How can I extend Metadata using attributes?

  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,097

    .NET Framework IL: How can I extend Metadata using attributes?

    Q: How can I extend Metadata using attributes?

    A: The common language runtime allows you to add keyword-like descriptive declarations, called attributes, to annotate programming elements such as types, fields, methods, and properties. Attributes are saved with the metadata of a Microsoft .NET Framework file and can be used to describe your code to the runtime or to affect application behavior at run time. While the .NET Framework supplies many useful attributes, you can also design and deploy your own.


    • What are attributes?

      When you compile your code for the runtime, it is converted into Microsoft intermediate language (MSIL) and placed inside a portable executable (PE) file along with metadata generated by the compiler. Attributes allow you to place extra descriptive information into metadata that can be extracted using runtime reflection services. The compiler creates attributes when you declare instances of special classes that derive from 'System.Attribute'.



    • Why use attributes?


      • Attributes describe how to serialize data

      • Attributes specify characteristics that are used to enforce security

      • Attributes limit optimizations by the just-in-time (JIT) compiler so the code remains easy to debug

      • Attributes can also record the name of a file or the author of code, or control the visibility of controls and members during forms development

      • Attributes describe your code in practically any way conceivable and to affect run-time behavior in creative new ways

      • Attributes allow you to add your own descriptive elements to C#, the Managed Extensions for C++, Microsoft Visual Basic .NET, or any other language that targets the runtime, without having to rewrite your compiler.




    • How do I apply attributes?

      Use the following process to apply an attribute to an element of your code:


      1. Define a new attribute or use an existing attribute by importing its namespace from the .NET Framework.

      2. Initialize the attribute directly preceding the element that you want described, calling the attribute's constructor with the desired flag or information.




    • Can I have an example of applying attributes in C#?

      The following code example shows how to declare 'System.ObsoleteAttribute', which marks code as obsolete. The string 'Will be removed in next version' is passed to the attribute. This attribute causes a compiler warning that displays the passed string when code that the attribute describes is called.


      Code:
      using System;
      
      public class MainApp
      {
          public static void Main()
          {
              //This generates a compile-time warning.
              int MyInt = Add(2,2); 
          }
      
          //Specify attributes between square brackets in C#.
          //This attribute is applied only to the Add method.
          [Obsolete("Will be removed in next version")]
          public static int Add( int a, int b)
          {
              return (a+b);
          }  
      }

    • Can I have an example of applying attributes using Visual Basic.NET?

      The following code example shows how to declare 'System.ObsoleteAttribute', which marks code as obsolete. The string 'Will be removed in next version' is passed to the attribute. This attribute causes a compiler warning that displays the passed string when code that the attribute describes is called.


      Code:
      Imports System 
      
      'Call attributes between < and > in Visual Basic.
      Public Module main
          Sub Main()
          'This generates a compile-time warning.
          Dim MyInt as Integer = Add(2,2)
          End Sub
          'Specify attributes between < and > brackets in Visual Basic.
          'This attribute is applied only to the Add method.
          Obsolete("Will be removed in next version ")
      
          Function Add(a as Integer, b as Integer) as Integer
              Add = a + b
          End Function
      End Module

    • Can I have an example of applying attributes at Assembly Level?

      If you want to apply an attribute at the assembly level, use the 'Assembly' keyword. The following code shows the 'AssemblyNameAttribute' applied at the assembly level:


      • [C#]


        Code:
        using System.Reflection;
        [assembly:AssemblyName("MyAssembly")]
      • [Visual Basic]


        Code:
        Imports System.Reflection
        <Assembly:AssemblyName("MyAssembly")>



      When this attribute is applied, the string 'MyAssembly' is placed in the assembly manifest in the metadata portion of the file. You can view the attribute either by using the MSIL Disassembler or by creating a custom program to retrieve the attribute.



    Last edited by Andreas Masur; December 22nd, 2005 at 05:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center