You are on page 1of 14

Attributes

• 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 doesn’t know
about them.
• however these will be used as metadata in the compiled
assembly when they are applied to the program element.
• 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.
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
won’t 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.
System.Web.Services.WebMethodAttribute
Marks a method to be [WebMethod] exposed as part of a
Web service.

System.AttributeUsageAttribute
Defines the usage parameters [AttributeUsage] for
custom attributes.

System.ObsoleteAttribute [Obsolete]
Marks a specific section of code as obsolete.
Example
using System;
[Serializable]// OR [Serializable, WebService]
class SerialaizableClass
{
//Class definition here
}

[Obsolete("Use ConcatStrings instead.")]


public static string ConcatTwoStrings(string
StringOne,string StringTwo)
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; }
AttributeTargets
multiple targets can be given separated by |
Target Description
All Attribute can be applied to any
Supported by the .NET Compact application element.
Framework. default
Assembly Attribute can be applied to an
Supported by the .NET Compact assembly.
Framework.
Class Attribute can be applied to a class.
Supported by the .NET Compact
Framework.
Constructor Attribute can be applied to a
Supported by the .NET Compact constructor.
Framework.
Delegate Attribute can be applied to a
Supported by the .NET Compact delegate.
Framework.
Enum Attribute can be applied to an
Supported by the .NET Compact enumeration.
Framework.
Event Attribute can be applied to an
Supported by the .NET Compact event.
Framework.
Field Attribute can be applied to a field.
Supported by the .NET Compact
Framework.
Interface Attribute can be applied to an
Supported by the .NET Compact interface.
Framework.
Method Attribute can be applied to a
Supported by the .NET Compact method.
Framework.
Module Attribute can be applied to a module.
Supported by the .NET Compact Note Module refers to a portable
Framework. executable file (.dll or .exe) and not
a Visual Basic standard module.
Parameter Attribute can be applied to a
Supported by the .NET Compact parameter.
Framework.
Property Attribute can be applied to a
Supported by the .NET Compact property.
Framework.
ReturnValue Attribute can be applied to a return
Supported by the .NET Compact value.
Framework.
Struct Attribute can be applied to a
Supported by the .NET Compact structure; that is, a value type.
Framework.
Attribute Parameter Types
• Attribute classes have two types of parameters:
– Positional parameters must be specified every time
the attribute is used. Positional parameters are
specified as constructor arguments to the attribute
class. In the previous example , url is a positional
parameter.
– Named parameters are optional. If they are specified
when the attribute is used, the name of the parameter
must be used. Named parameters are defined by
having a nonstatic field or property. In the previous
example , Topic is a named parameter.
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
}
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{
…}
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.

You might also like