EnumBuilder.SetCustomAttribute Method
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Sets a custom attribute using a custom attribute builder.
Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Sub SetCustomAttribute ( _
customBuilder As CustomAttributeBuilder _
)
public void SetCustomAttribute(
CustomAttributeBuilder customBuilder
)
Parameters
- customBuilder
Type: System.Reflection.Emit.CustomAttributeBuilder
An instance of a helper class to define the custom attribute.
Exceptions
Exception | Condition |
---|---|
ArgumentNullException | con is nulla null reference (Nothing in Visual Basic). |
Examples
The following code sample illustrates the use of SetCustomAttribute in the context of EnumBuilder, passing a CustomAttributeBuilder.
Note: |
---|
To run this example, see Building Examples That Use a Demo Method and a TextBlock Control. |
Imports System.Reflection
Imports System.Reflection.Emit
<AttributeUsage(AttributeTargets.All, AllowMultiple:=False)> _
Public Class MyAttribute
Inherits Attribute
Private myBoolValue As Boolean
Public ReadOnly Property MyBool As Boolean
Get
Return myBoolValue
End Get
End Property
Public Sub New(ByVal myBool As Boolean)
myBoolValue = myBool
End Sub
End Class
Class Example
Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
' Get the current application domain.
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
' Create a dynamic assembly in the current application domain,
' and allow it to be executed.
Dim aName As AssemblyName = New AssemblyName("TempAssembly")
Dim ab As AssemblyBuilder = currentDomain.DefineDynamicAssembly( _
aName, AssemblyBuilderAccess.Run)
' Define a dynamic module in "TempAssembly" assembly. The module can
' have the same name as the assembly.
Dim mb As ModuleBuilder = _
ab.DefineDynamicModule(aName.Name)
' Define a public enumeration with the name "Elevation" and an
' underlying type of Integer.
Dim eb As EnumBuilder = _
mb.DefineEnum("Elevation", TypeAttributes.Public, GetType(Integer))
' Define two members, "High" and "Low".
eb.DefineLiteral("Low", 0)
eb.DefineLiteral("High", 1)
' 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.
Dim attrType As Type = GetType(MyAttribute)
Dim attrCtor As ConstructorInfo = _
attrType.GetConstructor(New Type() {GetType(Boolean)})
Dim attrBuilder As New CustomAttributeBuilder( _
attrCtor, New Object() { True })
eb.SetCustomAttribute(attrBuilder)
' Create the type.
Dim finished As Type = eb.CreateType()
For Each fi As FieldInfo in finished.GetFields()
outputBlock.Text &= String.Format("{0}.{1} = {2}" & vbCrLf, _
finished.Name, fi.Name, fi.GetRawConstantValue())
Next
' Read the attributes and display them.
outputBlock.Text &= vbCrLf & "Custom attributes: " & vbCrLf
For Each attr As Object In finished.GetCustomAttributes(True)
outputBlock.Text &= attr.ToString() & vbCrLf
If TypeOf attr Is MyAttribute Then
outputBlock.Text &= String.Format(" MyBool: {0}" & vbCrLf, _
CType(attr, MyAttribute).MyBool.ToString())
End If
Next
End Sub
End Class
' This code example produces output similar to the following:
'
'Elevation.Low = 0
'Elevation.High = 1
'
'Custom attributes:
'SilverlightApplication.MyAttribute
' MyBool: True
using System;
using System.Reflection;
using System.Reflection.Emit;
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
private bool myBoolValue;
public bool MyBool { get { return myBoolValue; }}
public MyAttribute(bool myBool)
{
this.myBoolValue = myBool;
}
}
class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
// Get the current application domain.
AppDomain currentDomain = AppDomain.CurrentDomain;
// Create a dynamic assembly in the current application domain,
// and allow it to be executed.
AssemblyName aName = new AssemblyName("TempAssembly");
AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(
aName, AssemblyBuilderAccess.Run);
// Define a dynamic module in "TempAssembly" assembly. The module can
// have the same name as the assembly.
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
// Define a public enumeration with the name "Elevation" and an
// underlying type of Integer.
EnumBuilder eb = mb.DefineEnum("Elevation", TypeAttributes.Public, typeof(int));
// Define two members, "High" and "Low".
eb.DefineLiteral("Low", 0);
eb.DefineLiteral("High", 1);
// 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 attrType = typeof(MyAttribute);
ConstructorInfo attrCtor =
attrType.GetConstructor(new Type[] {typeof(bool)});
CustomAttributeBuilder attrBuilder =
new CustomAttributeBuilder(attrCtor, new Object[] { true });
eb.SetCustomAttribute(attrBuilder);
// Create the type.
Type finished = eb.CreateType();
foreach (FieldInfo fi in finished.GetFields())
{
outputBlock.Text += String.Format("{0}.{1} = {2}\n",
finished.Name, fi.Name, fi.GetRawConstantValue());
}
// Read the attributes and display them.
outputBlock.Text += "\nCustom attributes: \n";
foreach (object attr in finished.GetCustomAttributes(true))
{
outputBlock.Text += attr.ToString() + "\n";
if (attr is MyAttribute)
{
outputBlock.Text += String.Format(" MyBool: {0}\n",
((MyAttribute) attr).MyBool.ToString());
}
}
}
}
/* This code example produces the following output:
Elevation.Low = 0
Elevation.High = 1
Custom attributes:
MyAttribute
MyBool: True
*/
Version Information
Silverlight
Supported in: 5, 4, 3
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.