ECMA-334: 24.3.2 Run-time retrieval of an attribute instance
| C# Language Specification |
|
| © 2006 ECMA International |
24.3.2 Run-time retrieval of an attribute instance
The attribute instance represented by T, C, P, and N, and associated with E can be retrieved at run-time from
the assembly A using the following steps:
- Follow the run-time processing steps for executing an object-creation-expression of the form
new T(P), using the instance constructorCand values as determined at compile-time. These steps either result in an exception, or produce an instance ofT. Call this instanceO. - For each named-argument
ArginN, in order:- Let
Namebe the identifier of the named-argumentArg. IfNamedoes not identify a non-static public read-write field or property onO, then an exception is thrown. - Let
Valuebe the result of evaluating the attribute-argument-expression ofArg. - If
Nameidentifies a field onO, then set this field to the valueValue. - Otherwise,
Nameidentifies a property onO. Set this property to the valueValue. - The result is
O, an instance of the attribute classTthat has been initialized with the positional-argument-listPand the named-argument-listN.
- Let
- Follow the run-time processing steps for executing an object-creation-expression of the form
[Note: The format for storing T, C, P, N (and associating it with E) in A and the mechanism to specify E and
retrieve T, C, P, N from A (and hence how an attribute instance is obtained at runtime) is beyond the scope of
this standard. end note]
[Example: In an implementation of the CLI, the Help attribute instances in the assembly created by
compiling the example program in §24.1.2 can be retrieved with the following program:
using System; using System.Reflection; public sealed class InterrogateHelpUrls { public static void Main(string[] args) { Type helpType = typeof(HelpAttribute); string assemblyName = args[0]; foreach (Type t in Assembly.Load(assemblyName).GetTypes()) { Console.WriteLine("Type : {0}", t.ToString()); HelpAttribute[] helpers = (HelpAttribute[])t.GetCustomAttributes(helpType, false); for (int at = 0; at != helpers.Length; at++) { Console.WriteLine("\tUrl : {0}", helpers[at].Url); } } } }
end example]