14 Attributes

download 14  Attributes

of 14

Transcript of 14 Attributes

  • 8/14/2019 14 Attributes

    1/14

    Attributes

  • 8/14/2019 14 Attributes

    2/14

    Attributes can be defined on various items within a

    program.

    It allows for defining declarative tags placed in certain

    places in the source code to specify additional

    information. this information can be retrieved by

    reflection.

    Custom attributes are the ones defined by the user.

    ofcourse there is no special benefits that you get from

    compiler out of them because compiler doesnt know

    about them.

    however these will be used as metadata in the compiled

    assembly when they are applied to the program element.

  • 8/14/2019 14 Attributes

    3/14

    you could use these for documentation or may be the

    reflection classes can use the meta-data to make some

    decisions at runtime.

    When compiler looks at an attribute attached to an

    element, it first appends Attribute to the attribute name (if

    it does not already have Attribute ending its name). it

    then looks for the Attribute class in all the namespace in

    the search path. Attributes classes are public classes derived from

    System.Attribute that have at least one public

    constructor.

  • 8/14/2019 14 Attributes

    4/14

    Common Default Attributes

    System.SerializableAttribute [Serializable]

    Allows your class to be serializable to disk or over a

    network.

    System.NonSerializedAttribute [NonSerialized]

    Allows certain members to be nonserialized so that data

    wont be saved to disk or over a network. Similar to the

    transient keyword in Java.

    System.Web.Services.WebServiceAttribute

    Allows you to specify a name [WebService] and

    description for a Web service.

  • 8/14/2019 14 Attributes

    5/14

    System.Web.Services.WebMethodAttribute

    Marks a method to be [WebMethod] exposed as part of aWeb service.

    System.AttributeUsageAttribute

    Defines the usage parameters [AttributeUsage] forcustom attributes.

    System.ObsoleteAttribute [Obsolete]

    Marks a specific section of code as obsolete.

  • 8/14/2019 14 Attributes

    6/14

    Example

    using System;

    [Serializable]// OR [Serializable, WebService]

    class SerialaizableClass

    {//Class definition here

    }

    [Obsolete("Use ConcatStrings instead.")]

    public static string ConcatTwoStrings(string

    StringOne,string StringTwo)

  • 8/14/2019 14 Attributes

    7/14

    Creating Attribute-CustomAttributes

    using System;[AttributeUsage(AttributeTargets.All)]

    public class HelpAttribute : System.Attribute

    { public readonly string Url;

    private string topic;

    // Topic is a named parameter

    public string Topic{

    get{return topic;}

    set{topic = value;} }

    // url is a positional parameter

    public HelpAttribute(string url)

    { this.Url = url; }

  • 8/14/2019 14 Attributes

    8/14

    AttributeTargetsmultiple targets can be given separated by |

    Target Description

    All

    Supported by the .NET Compact

    Framework. default

    Attribute can be applied to any

    application element.

    AssemblySupported by the .NET Compact

    Framework.

    Attribute can be applied to anassembly.

    Class

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to a class.

    Constructor

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to a

    constructor.

  • 8/14/2019 14 Attributes

    9/14

    Delegate

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to a

    delegate.

    Enum

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to an

    enumeration.

    Event

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to an

    event.

    Field

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to a field.

    InterfaceSupported by the .NET Compact

    Framework.

    Attribute can be applied to aninterface.

    Method

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to a

    method.

  • 8/14/2019 14 Attributes

    10/14

    Module

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to a module.

    Note Module refers to a portable

    executable file (.dll or .exe) and nota Visual Basic standard module.

    Parameter

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to a

    parameter.

    PropertySupported by the .NET Compact

    Framework.

    Attribute can be applied to aproperty.

    ReturnValue

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to a return

    value.

    Struct

    Supported by the .NET Compact

    Framework.

    Attribute can be applied to a

    structure; that is, a value type.

  • 8/14/2019 14 Attributes

    11/14

  • 8/14/2019 14 Attributes

    12/14

    Using Attributes

    Style 1: using Positional parameter

    HelpAttribute("http://localhost/MyClassInfo")]

    class MyClass {

    //class definition

    }Style 2: using Positional parameter with Named

    parameter

    [Help(http://localhost/MyClassInfo,

    Topic=MyTopic)]class MyClass

    {

    //class definition

    }

  • 8/14/2019 14 Attributes

    13/14

    AllowMultiple

    A Boolean value that indicates whether multiple

    attributes can be specified for one program element.

    The default value for this parameter is False.

    Example;

    using System;

    [AttributeUsage(AttributeTargets.Assembly

    | AttributeTargets.Class

    | AttributeTargets.Method,

    AllowMultiple=true)]

    public class AAAttribute : System.Attribute{

    }

  • 8/14/2019 14 Attributes

    14/14

    Accessing Attributes Through

    Reflection

    class MainClass {

    public static void Main()

    { System.Reflection.MemberInfo info =

    typeof(MyClass);object[] attributes = info.GetCustomAttributes(true);

    for (int i = 0; i < attributes.Length; i ++)

    {

    System.Console.WriteLine(attributes[i]);

    }

    } }Specifies to search the attribute in

    inheritance heirarchy.