Reputation: 4104
I'm trying to understand the use of Attributes
a bit better.
I understand that:
Attributes
in my code using reflection.Now I wants to do some more with attributes and curious about:
DllImportAttribute
or STAThread
) perform there functionalities? Because we just use these attributes and respective functionalities are performed. e.g. With the help of DllImportAttribute
, I just declare that my method abc()
needs xyz.dll
and respective DLL is loaded. I didn't write code to search and load the DLL xyz.dll
.Upvotes: 2
Views: 564
Reputation: 81573
There is no mystery really...
An attribute is actually an object that is associated with any of these elements: Assembly
, Class
, Method
, Delegate
, Enum
, Event
, Field
, Interface
, Property
and Struct
.
They can be used to associate declarative information and retrieve such information (at Runtime) by using reflection. In other words, you can use attributes to inject additional information to the assemblies that can be queried at Runtime if needed using reflection.
An attribute basically just comprises of its name and optionally, a list of parameters.
From MSDN Attributes (C#)
Attributes provide a powerful method of associating metadata, or declarative information, with code (assemblies, types, methods, properties, and so forth). After an attribute is associated with a program entity, the attribute can be queried at run time by using a technique called reflection. For more information, see Reflection (C#).
Attributes have the following properties:
Attributes add metadata to your program. Metadata is information about the types defined in a program. All .NET assemblies contain a specified set of metadata that describes the types and type members defined in the assembly. You can add custom attributes to specify any additional information that is required. For more information, see, Creating Custom Attributes (C#).
You can apply one or more attributes to entire assemblies, modules, or smaller program elements such as classes and properties.
Attributes can accept arguments in the same way as methods and properties.
Your program can examine its own metadata or the metadata in other programs by using reflection. For more information, see Accessing Attributes by Using Reflection (C#).
If you want to receive information about the Metadata stored in an Attribute you need to do something like this
Exmaple
Lifted from How do I read an attribute on a class at runtime?
[DomainName("MyTable")]
Public class MyClass : DomainBase
{}
...
public static class AttributeExtensions
{
public static TValue GetAttributeValue<TAttribute, TValue>(
this Type type,
Func<TAttribute, TValue> valueSelector)
where TAttribute : Attribute
{
var att = type.GetCustomAttributes(
typeof(TAttribute), true
).FirstOrDefault() as TAttribute;
if (att != null)
{
return valueSelector(att);
}
return default(TValue);
}
}
and use like this:
string name = typeof(MyClass).GetAttributeValue((DomainNameAttribute dna) => dna.Name);
Upvotes: 0