FieldBuilder.SetCustomAttribute Method
Silverlight
Sets a custom attribute using a custom attribute builder.
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
[SecuritySafeCriticalAttribute] public void SetCustomAttribute( CustomAttributeBuilder customBuilder )
Parameters
- customBuilder
- Type: System.Reflection.Emit.CustomAttributeBuilder
An instance of a helper class to define the custom attribute.
| Exception | Condition |
|---|---|
| ArgumentNullException | con is null. |
| InvalidOperationException | The parent type of this field is complete. |
The following code sample illustrates the use of SetCustomAttribute in the context of FieldBuilder, using a CustomAttributeBuilder.
Note: |
|---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
using System; using System.Reflection; using System.Reflection.Emit; [AttributeUsage(AttributeTargets.All, AllowMultiple = false)] public class MyAttribute : Attribute { private string myStringValue; public string MyString { get { return myStringValue; }} public MyAttribute(string myString) { myStringValue = myString; } } class Example { public static void Demo(System.Windows.Controls.TextBlock outputBlock) { // Create a simple name for the assembly. AssemblyName myAssemblyName = new AssemblyName("EmittedAssembly"); // Create the dynamic assembly. AssemblyBuilder myAssemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess.Run); ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("EmittedModule"); // Define a public class named 'CustomClass' in the assembly. TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("CustomClass", TypeAttributes.Public); // Define a private String field named 'MyField' in the type. FieldBuilder myFieldBuilder = myTypeBuilder.DefineField("MyField", typeof(String), FieldAttributes.Public); // To set a custom attribute, first get the constructor for the attribute. // Use the constructor and an array of arguments for the constructor to // create a CustomAttributeBuilder. Finally, use the CustomAttributeBuilder // to set the attribute on the enumeration. Type myAttributeType = typeof(MyAttribute); ConstructorInfo myConstructorInfo = myAttributeType.GetConstructor(new Type[] { typeof(string) }); CustomAttributeBuilder attributeBuilder = new CustomAttributeBuilder(myConstructorInfo, new object[] { "Test" }); myFieldBuilder.SetCustomAttribute(attributeBuilder); Type myCustomClass = myTypeBuilder.CreateType(); // Retrieve the values of Attributes applied to field and display to console. FieldInfo myFieldInfo = myCustomClass.GetField("MyField"); foreach (object attr in myFieldInfo.GetCustomAttributes(true)) { outputBlock.Text += attr.ToString() + "\n"; if (attr is MyAttribute) { outputBlock.Text += String.Format(" MyString: {0}\n", ((MyAttribute) attr).MyString.ToString()); } } } } /* This example produces output similar to the following: MyAttribute MyString: Test */
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Note: