ConstructorBuilder.Attributes Property


Retrieves the attributes for this constructor.

Namespace:   System.Reflection.Emit
Assembly:  mscorlib (in mscorlib.dll)

public override MethodAttributes Attributes { get; }

Property Value

Type: System.Reflection.MethodAttributes

Returns the attributes for this constructor.

The following code sample illustrates the use of Attributes.

MethodBuilder myMethodBuilder=null;

AppDomain myCurrentDomain = AppDomain.CurrentDomain;
// Create assembly in current CurrentDomain
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TempAssembly";
// Create a dynamic assembly
myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly
   (myAssemblyName, AssemblyBuilderAccess.RunAndSave);
// Create a dynamic module in the assembly.
myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule");
FieldInfo myFieldInfo =
// Create a type in the module
TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("TempClass",TypeAttributes.Public);
FieldBuilder myGreetingField = myTypeBuilder.DefineField("Greeting",
   typeof(String), FieldAttributes.Public);
Type[] myConstructorArgs = { typeof(String) };
// Define a constructor of the dynamic class.
ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
   MethodAttributes.Public, CallingConventions.Standard, myConstructorArgs);
PermissionSet myPset = new PermissionSet(PermissionState.Unrestricted);
// Add declarative security to the constructor.
Console.WriteLine("Adding declarative security to the constructor.....");
Console.WriteLine("The Security action to be taken is \"DENY\" and" +
   " Permission set is \"UNRESTRICTED\".");
MethodAttributes myMethodAttributes = myConstructor.Attributes;
Type myAttributeType = typeof(MethodAttributes);
int myAttribValue = (int) myMethodAttributes;
if(! myAttributeType.IsEnum)
   Console.WriteLine("This is not an Enum");
FieldInfo[] myFieldInfo1 = myAttributeType.GetFields(BindingFlags.Public | BindingFlags.Static);
Console.WriteLine("The Field info names of the Attributes for the constructor are:");
for (int i = 0; i < myFieldInfo1.Length; i++)
   int myFieldValue = (Int32)myFieldInfo1[i].GetValue(null);
   if ((myFieldValue & myAttribValue) == myFieldValue)
      Console.WriteLine("   " + myFieldInfo1[i].Name);

Type myType2 = myConstructor.DeclaringType;
Console.WriteLine("The declaring type is : "+myType2.ToString());

.NET Framework
Available since 1.1
Available since 2.0
Return to top