AssemblyBuilder 類別

定義

定義及表示動態組件。Defines and represents a dynamic assembly.

public ref class AssemblyBuilder sealed : System::Reflection::Assembly, System::Runtime::InteropServices::_AssemblyBuilder
public sealed class AssemblyBuilder : System.Reflection.Assembly, System.Runtime.InteropServices._AssemblyBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
public sealed class AssemblyBuilder : System.Reflection.Assembly, System.Runtime.InteropServices._AssemblyBuilder
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class AssemblyBuilder : System.Reflection.Assembly, System.Runtime.InteropServices._AssemblyBuilder
type AssemblyBuilder = class
    inherit Assembly
    interface _AssemblyBuilder
Public NotInheritable Class AssemblyBuilder
Inherits Assembly
Implements _AssemblyBuilder
繼承
AssemblyBuilder
屬性
實作

範例

下列程式碼範例示範如何使用一個模組來定義動態元件。The following code example shows how to define a dynamic assembly with one module. 範例元件中的模組包含一種類型,MyDynamicType,其具有私用欄位、取得和設定私用欄位的屬性、初始化私用欄位的程式碼,以及將使用者提供的數位乘以私用域值並傳回結果的方法。The module in the example assembly contains one type, MyDynamicType, which has a private field, a property that gets and sets the private field, constructors that initialize the private field, and a method that multiplies a user-supplied number by the private field value and returns the result.

建立元件時,會指定 AssemblyBuilderAccess.RunAndSaveAssemblyBuilderAccess.RunAndSave is specified when the assembly is created. 系統會立即使用元件程式碼,而且元件也會儲存到磁片中,以便使用Ildasm (IL解譯器)來檢查,或在其他程式中使用。The assembly code is used immediately, and the assembly is also saved to disk so that it can be examined with Ildasm.exe (IL Disassembler) or used in another program.

using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;

void main()
{
    // An assembly consists of one or more modules, each of which
    // contains zero or more types. This code creates a single-module
    // assembly, the most common case. The module contains one type,
    // named "MyDynamicType", that has a private field, a property 
    // that gets and sets the private field, constructors that 
    // initialize the private field, and a method that multiplies 
    // a user-supplied number by the private field value and returns
    // the result. In Visual C++ the type might look like this:
    /*
      public ref class MyDynamicType
      {
      private:
          int m_number;

      public:
          MyDynamicType() : m_number(42) {};
          MyDynamicType(int initNumber) : m_number(initNumber) {};
      
          property int Number
          {
              int get() { return m_number; }
              void set(int value) { m_number = value; }
          }

          int MyMethod(int multiplier)
          {
              return m_number * multiplier;
          }
      };
    */
      
    AssemblyName^ aName = gcnew AssemblyName("DynamicAssemblyExample");
    AssemblyBuilder^ ab = 
        AppDomain::CurrentDomain->DefineDynamicAssembly(
            aName, 
            AssemblyBuilderAccess::RunAndSave);

    // For a single-module assembly, the module name is usually
    // the assembly name plus an extension.
    ModuleBuilder^ mb = 
        ab->DefineDynamicModule(aName->Name, aName->Name + ".dll");
      
    TypeBuilder^ tb = mb->DefineType(
        "MyDynamicType", 
         TypeAttributes::Public);

    // Add a private field of type int (Int32).
    FieldBuilder^ fbNumber = tb->DefineField(
        "m_number", 
        int::typeid, 
        FieldAttributes::Private);

    // Define a constructor that takes an integer argument and 
    // stores it in the private field. 
    array<Type^>^ parameterTypes = { int::typeid };
    ConstructorBuilder^ ctor1 = tb->DefineConstructor(
        MethodAttributes::Public, 
        CallingConventions::Standard, 
        parameterTypes);

    ILGenerator^ ctor1IL = ctor1->GetILGenerator();
    // For a constructor, argument zero is a reference to the new
    // instance. Push it on the stack before calling the base
    // class constructor. Specify the default constructor of the 
    // base class (System::Object) by passing an empty array of 
    // types (Type::EmptyTypes) to GetConstructor.
    ctor1IL->Emit(OpCodes::Ldarg_0);
    ctor1IL->Emit(OpCodes::Call, 
        Object::typeid->GetConstructor(Type::EmptyTypes));
    // Push the instance on the stack before pushing the argument
    // that is to be assigned to the private field m_number.
    ctor1IL->Emit(OpCodes::Ldarg_0);
    ctor1IL->Emit(OpCodes::Ldarg_1);
    ctor1IL->Emit(OpCodes::Stfld, fbNumber);
    ctor1IL->Emit(OpCodes::Ret);

    // Define a default constructor that supplies a default value
    // for the private field. For parameter types, pass the empty
    // array of types or pass nullptr.
    ConstructorBuilder^ ctor0 = tb->DefineConstructor(
        MethodAttributes::Public, 
        CallingConventions::Standard, 
        Type::EmptyTypes);

    ILGenerator^ ctor0IL = ctor0->GetILGenerator();
    ctor0IL->Emit(OpCodes::Ldarg_0);
    ctor0IL->Emit(OpCodes::Call, 
        Object::typeid->GetConstructor(Type::EmptyTypes));
    // For a constructor, argument zero is a reference to the new
    // instance. Push it on the stack before pushing the default
    // value on the stack.
    ctor0IL->Emit(OpCodes::Ldarg_0);
    ctor0IL->Emit(OpCodes::Ldc_I4_S, 42);
    ctor0IL->Emit(OpCodes::Stfld, fbNumber);
    ctor0IL->Emit(OpCodes::Ret);

    // Define a property named Number that gets and sets the private 
    // field.
    //
    // The last argument of DefineProperty is nullptr, because the
    // property has no parameters. (If you don't specify nullptr, you must
    // specify an array of Type objects. For a parameterless property,
    // use the built-in array with no elements: Type::EmptyTypes)
    PropertyBuilder^ pbNumber = tb->DefineProperty(
        "Number", 
        PropertyAttributes::HasDefault, 
        int::typeid, 
        nullptr);
      
    // The property "set" and property "get" methods require a special
    // set of attributes.
    MethodAttributes getSetAttr = MethodAttributes::Public | 
        MethodAttributes::SpecialName | MethodAttributes::HideBySig;

    // Define the "get" accessor method for Number. The method returns
    // an integer and has no arguments. (Note that nullptr could be 
    // used instead of Types::EmptyTypes)
    MethodBuilder^ mbNumberGetAccessor = tb->DefineMethod(
        "get_Number", 
        getSetAttr, 
        int::typeid, 
        Type::EmptyTypes);
      
    ILGenerator^ numberGetIL = mbNumberGetAccessor->GetILGenerator();
    // For an instance property, argument zero is the instance. Load the 
    // instance, then load the private field and return, leaving the
    // field value on the stack.
    numberGetIL->Emit(OpCodes::Ldarg_0);
    numberGetIL->Emit(OpCodes::Ldfld, fbNumber);
    numberGetIL->Emit(OpCodes::Ret);
    
    // Define the "set" accessor method for Number, which has no return
    // type and takes one argument of type int (Int32).
    MethodBuilder^ mbNumberSetAccessor = tb->DefineMethod(
        "set_Number", 
        getSetAttr, 
        nullptr, 
        gcnew array<Type^> { int::typeid });
      
    ILGenerator^ numberSetIL = mbNumberSetAccessor->GetILGenerator();
    // Load the instance and then the numeric argument, then store the
    // argument in the field.
    numberSetIL->Emit(OpCodes::Ldarg_0);
    numberSetIL->Emit(OpCodes::Ldarg_1);
    numberSetIL->Emit(OpCodes::Stfld, fbNumber);
    numberSetIL->Emit(OpCodes::Ret);
      
    // Last, map the "get" and "set" accessor methods to the 
    // PropertyBuilder. The property is now complete. 
    pbNumber->SetGetMethod(mbNumberGetAccessor);
    pbNumber->SetSetMethod(mbNumberSetAccessor);

    // Define a method that accepts an integer argument and returns
    // the product of that integer and the private field m_number. This
    // time, the array of parameter types is created on the fly.
    MethodBuilder^ meth = tb->DefineMethod(
        "MyMethod", 
        MethodAttributes::Public, 
        int::typeid, 
        gcnew array<Type^> { int::typeid });

    ILGenerator^ methIL = meth->GetILGenerator();
    // To retrieve the private instance field, load the instance it
    // belongs to (argument zero). After loading the field, load the 
    // argument one and then multiply. Return from the method with 
    // the return value (the product of the two numbers) on the 
    // execution stack.
    methIL->Emit(OpCodes::Ldarg_0);
    methIL->Emit(OpCodes::Ldfld, fbNumber);
    methIL->Emit(OpCodes::Ldarg_1);
    methIL->Emit(OpCodes::Mul);
    methIL->Emit(OpCodes::Ret);

    // Finish the type->
    Type^ t = tb->CreateType();
     
    // The following line saves the single-module assembly. This
    // requires AssemblyBuilderAccess to include Save. You can now
    // type "ildasm MyDynamicAsm.dll" at the command prompt, and 
    // examine the assembly. You can also write a program that has
    // a reference to the assembly, and use the MyDynamicType type.
    // 
    ab->Save(aName->Name + ".dll");

    // Because AssemblyBuilderAccess includes Run, the code can be
    // executed immediately. Start by getting reflection objects for
    // the method and the property.
    MethodInfo^ mi = t->GetMethod("MyMethod");
    PropertyInfo^ pi = t->GetProperty("Number");
  
    // Create an instance of MyDynamicType using the default 
    // constructor. 
    Object^ o1 = Activator::CreateInstance(t);

    // Display the value of the property, then change it to 127 and 
    // display it again. Use nullptr to indicate that the property
    // has no index.
    Console::WriteLine("o1->Number: {0}", pi->GetValue(o1, nullptr));
    pi->SetValue(o1, 127, nullptr);
    Console::WriteLine("o1->Number: {0}", pi->GetValue(o1, nullptr));

    // Call MyMethod, passing 22, and display the return value, 22
    // times 127. Arguments must be passed as an array, even when
    // there is only one.
    array<Object^>^ arguments = { 22 };
    Console::WriteLine("o1->MyMethod(22): {0}", 
        mi->Invoke(o1, arguments));

    // Create an instance of MyDynamicType using the constructor
    // that specifies m_Number. The constructor is identified by
    // matching the types in the argument array. In this case, 
    // the argument array is created on the fly. Display the 
    // property value.
    Object^ o2 = Activator::CreateInstance(t, 
        gcnew array<Object^> { 5280 });
    Console::WriteLine("o2->Number: {0}", pi->GetValue(o2, nullptr));
};

/* This code produces the following output:

o1->Number: 42
o1->Number: 127
o1->MyMethod(22): 2794
o2->Number: 5280
 */
using System;
using System.Reflection;
using System.Reflection.Emit;

class DemoAssemblyBuilder
{
    public static void Main()
    {
        // An assembly consists of one or more modules, each of which
        // contains zero or more types. This code creates a single-module
        // assembly, the most common case. The module contains one type,
        // named "MyDynamicType", that has a private field, a property 
        // that gets and sets the private field, constructors that 
        // initialize the private field, and a method that multiplies 
        // a user-supplied number by the private field value and returns
        // the result. In C# the type might look like this:
        /*
        public class MyDynamicType
        {
            private int m_number;
        
            public MyDynamicType() : this(42) {}
            public MyDynamicType(int initNumber)
            {
                m_number = initNumber;
            }

            public int Number
            {
                get { return m_number; }
                set { m_number = value; }
            }

            public int MyMethod(int multiplier)
            {
                return m_number * multiplier;
            }
        }
        */
      
        AssemblyName aName = new AssemblyName("DynamicAssemblyExample");
        AssemblyBuilder ab = 
            AppDomain.CurrentDomain.DefineDynamicAssembly(
                aName, 
                AssemblyBuilderAccess.RunAndSave);

        // For a single-module assembly, the module name is usually
        // the assembly name plus an extension.
        ModuleBuilder mb = 
            ab.DefineDynamicModule(aName.Name, aName.Name + ".dll");
      
        TypeBuilder tb = mb.DefineType(
            "MyDynamicType", 
             TypeAttributes.Public);

        // Add a private field of type int (Int32).
        FieldBuilder fbNumber = tb.DefineField(
            "m_number", 
            typeof(int), 
            FieldAttributes.Private);

        // Define a constructor that takes an integer argument and 
        // stores it in the private field. 
        Type[] parameterTypes = { typeof(int) };
        ConstructorBuilder ctor1 = tb.DefineConstructor(
            MethodAttributes.Public, 
            CallingConventions.Standard, 
            parameterTypes);

        ILGenerator ctor1IL = ctor1.GetILGenerator();
        // For a constructor, argument zero is a reference to the new
        // instance. Push it on the stack before calling the base
        // class constructor. Specify the default constructor of the 
        // base class (System.Object) by passing an empty array of 
        // types (Type.EmptyTypes) to GetConstructor.
        ctor1IL.Emit(OpCodes.Ldarg_0);
        ctor1IL.Emit(OpCodes.Call, 
            typeof(object).GetConstructor(Type.EmptyTypes));
        // Push the instance on the stack before pushing the argument
        // that is to be assigned to the private field m_number.
        ctor1IL.Emit(OpCodes.Ldarg_0);
        ctor1IL.Emit(OpCodes.Ldarg_1);
        ctor1IL.Emit(OpCodes.Stfld, fbNumber);
        ctor1IL.Emit(OpCodes.Ret);

        // Define a default constructor that supplies a default value
        // for the private field. For parameter types, pass the empty
        // array of types or pass null.
        ConstructorBuilder ctor0 = tb.DefineConstructor(
            MethodAttributes.Public, 
            CallingConventions.Standard, 
            Type.EmptyTypes);

        ILGenerator ctor0IL = ctor0.GetILGenerator();
        // For a constructor, argument zero is a reference to the new
        // instance. Push it on the stack before pushing the default
        // value on the stack, then call constructor ctor1.
        ctor0IL.Emit(OpCodes.Ldarg_0);
        ctor0IL.Emit(OpCodes.Ldc_I4_S, 42);
        ctor0IL.Emit(OpCodes.Call, ctor1);
        ctor0IL.Emit(OpCodes.Ret);

        // Define a property named Number that gets and sets the private 
        // field.
        //
        // The last argument of DefineProperty is null, because the
        // property has no parameters. (If you don't specify null, you must
        // specify an array of Type objects. For a parameterless property,
        // use the built-in array with no elements: Type.EmptyTypes)
        PropertyBuilder pbNumber = tb.DefineProperty(
            "Number", 
            PropertyAttributes.HasDefault, 
            typeof(int), 
            null);
      
        // The property "set" and property "get" methods require a special
        // set of attributes.
        MethodAttributes getSetAttr = MethodAttributes.Public | 
            MethodAttributes.SpecialName | MethodAttributes.HideBySig;

        // Define the "get" accessor method for Number. The method returns
        // an integer and has no arguments. (Note that null could be 
        // used instead of Types.EmptyTypes)
        MethodBuilder mbNumberGetAccessor = tb.DefineMethod(
            "get_Number", 
            getSetAttr, 
            typeof(int), 
            Type.EmptyTypes);
      
        ILGenerator numberGetIL = mbNumberGetAccessor.GetILGenerator();
        // For an instance property, argument zero is the instance. Load the 
        // instance, then load the private field and return, leaving the
        // field value on the stack.
        numberGetIL.Emit(OpCodes.Ldarg_0);
        numberGetIL.Emit(OpCodes.Ldfld, fbNumber);
        numberGetIL.Emit(OpCodes.Ret);
        
        // Define the "set" accessor method for Number, which has no return
        // type and takes one argument of type int (Int32).
        MethodBuilder mbNumberSetAccessor = tb.DefineMethod(
            "set_Number", 
            getSetAttr, 
            null, 
            new Type[] { typeof(int) });
      
        ILGenerator numberSetIL = mbNumberSetAccessor.GetILGenerator();
        // Load the instance and then the numeric argument, then store the
        // argument in the field.
        numberSetIL.Emit(OpCodes.Ldarg_0);
        numberSetIL.Emit(OpCodes.Ldarg_1);
        numberSetIL.Emit(OpCodes.Stfld, fbNumber);
        numberSetIL.Emit(OpCodes.Ret);
      
        // Last, map the "get" and "set" accessor methods to the 
        // PropertyBuilder. The property is now complete. 
        pbNumber.SetGetMethod(mbNumberGetAccessor);
        pbNumber.SetSetMethod(mbNumberSetAccessor);

        // Define a method that accepts an integer argument and returns
        // the product of that integer and the private field m_number. This
        // time, the array of parameter types is created on the fly.
        MethodBuilder meth = tb.DefineMethod(
            "MyMethod", 
            MethodAttributes.Public, 
            typeof(int), 
            new Type[] { typeof(int) });

        ILGenerator methIL = meth.GetILGenerator();
        // To retrieve the private instance field, load the instance it
        // belongs to (argument zero). After loading the field, load the 
        // argument one and then multiply. Return from the method with 
        // the return value (the product of the two numbers) on the 
        // execution stack.
        methIL.Emit(OpCodes.Ldarg_0);
        methIL.Emit(OpCodes.Ldfld, fbNumber);
        methIL.Emit(OpCodes.Ldarg_1);
        methIL.Emit(OpCodes.Mul);
        methIL.Emit(OpCodes.Ret);

        // Finish the type.
        Type t = tb.CreateType();
     
        // The following line saves the single-module assembly. This
        // requires AssemblyBuilderAccess to include Save. You can now
        // type "ildasm MyDynamicAsm.dll" at the command prompt, and 
        // examine the assembly. You can also write a program that has
        // a reference to the assembly, and use the MyDynamicType type.
        // 
        ab.Save(aName.Name + ".dll");

        // Because AssemblyBuilderAccess includes Run, the code can be
        // executed immediately. Start by getting reflection objects for
        // the method and the property.
        MethodInfo mi = t.GetMethod("MyMethod");
        PropertyInfo pi = t.GetProperty("Number");
  
        // Create an instance of MyDynamicType using the default 
        // constructor. 
        object o1 = Activator.CreateInstance(t);

        // Display the value of the property, then change it to 127 and 
        // display it again. Use null to indicate that the property
        // has no index.
        Console.WriteLine("o1.Number: {0}", pi.GetValue(o1, null));
        pi.SetValue(o1, 127, null);
        Console.WriteLine("o1.Number: {0}", pi.GetValue(o1, null));

        // Call MyMethod, passing 22, and display the return value, 22
        // times 127. Arguments must be passed as an array, even when
        // there is only one.
        object[] arguments = { 22 };
        Console.WriteLine("o1.MyMethod(22): {0}", 
            mi.Invoke(o1, arguments));

        // Create an instance of MyDynamicType using the constructor
        // that specifies m_Number. The constructor is identified by
        // matching the types in the argument array. In this case, 
        // the argument array is created on the fly. Display the 
        // property value.
        object o2 = Activator.CreateInstance(t, 
            new object[] { 5280 });
        Console.WriteLine("o2.Number: {0}", pi.GetValue(o2, null));
    }
}

/* This code produces the following output:

o1.Number: 42
o1.Number: 127
o1.MyMethod(22): 2794
o2.Number: 5280
 */
Imports System.Reflection
Imports System.Reflection.Emit

Class DemoAssemblyBuilder

    Public Shared Sub Main()

        ' An assembly consists of one or more modules, each of which
        ' contains zero or more types. This code creates a single-module
        ' assembly, the most common case. The module contains one type,
        ' named "MyDynamicType", that has a private field, a property 
        ' that gets and sets the private field, constructors that 
        ' initialize the private field, and a method that multiplies
        ' a user-supplied number by the private field value and returns 
        ' the result. The code might look like this in Visual Basic:
        '
        'Public Class MyDynamicType
        '    Private m_number As Integer
        '
        '    Public Sub New()
        '        Me.New(42)
        '    End Sub
        '
        '    Public Sub New(ByVal initNumber As Integer)
        '        m_number = initNumber
        '    End Sub
        '
        '    Public Property Number As Integer
        '        Get
        '            Return m_number
        '        End Get
        '        Set
        '            m_Number = Value
        '        End Set
        '    End Property
        '
        '    Public Function MyMethod(ByVal multiplier As Integer) As Integer
        '        Return m_Number * multiplier
        '    End Function
        'End Class
      
        Dim aName As New AssemblyName("DynamicAssemblyExample")
        Dim ab As AssemblyBuilder = _
            AppDomain.CurrentDomain.DefineDynamicAssembly( _
                aName, _
                AssemblyBuilderAccess.RunAndSave)

        ' For a single-module assembly, the module name is usually
        ' the assembly name plus an extension.
        Dim mb As ModuleBuilder = ab.DefineDynamicModule( _
            aName.Name, _
            aName.Name & ".dll")
      
        Dim tb As TypeBuilder = _
            mb.DefineType("MyDynamicType", TypeAttributes.Public)

        ' Add a private field of type Integer (Int32).
        Dim fbNumber As FieldBuilder = tb.DefineField( _
            "m_number", _
            GetType(Integer), _
            FieldAttributes.Private)

        ' Define a constructor that takes an integer argument and 
        ' stores it in the private field. 
        Dim parameterTypes() As Type = { GetType(Integer) }
        Dim ctor1 As ConstructorBuilder = _
            tb.DefineConstructor( _
                MethodAttributes.Public, _
                CallingConventions.Standard, _
                parameterTypes)

        Dim ctor1IL As ILGenerator = ctor1.GetILGenerator()
        ' For a constructor, argument zero is a reference to the new
        ' instance. Push it on the stack before calling the base
        ' class constructor. Specify the default constructor of the 
        ' base class (System.Object) by passing an empty array of 
        ' types (Type.EmptyTypes) to GetConstructor.
        ctor1IL.Emit(OpCodes.Ldarg_0)
        ctor1IL.Emit(OpCodes.Call, _
            GetType(Object).GetConstructor(Type.EmptyTypes))
        ' Push the instance on the stack before pushing the argument
        ' that is to be assigned to the private field m_number.
        ctor1IL.Emit(OpCodes.Ldarg_0)
        ctor1IL.Emit(OpCodes.Ldarg_1)
        ctor1IL.Emit(OpCodes.Stfld, fbNumber)
        ctor1IL.Emit(OpCodes.Ret)

        ' Define a default constructor that supplies a default value
        ' for the private field. For parameter types, pass the empty
        ' array of types or pass Nothing.
        Dim ctor0 As ConstructorBuilder = tb.DefineConstructor( _
            MethodAttributes.Public, _
            CallingConventions.Standard, _
            Type.EmptyTypes)

        Dim ctor0IL As ILGenerator = ctor0.GetILGenerator()
        ' For a constructor, argument zero is a reference to the new
        ' instance. Push it on the stack before pushing the default
        ' value on the stack, then call constructor ctor1.
        ctor0IL.Emit(OpCodes.Ldarg_0)
        ctor0IL.Emit(OpCodes.Ldc_I4_S, 42)
        ctor0IL.Emit(OpCodes.Call, ctor1)
        ctor0IL.Emit(OpCodes.Ret)

        ' Define a property named Number that gets and sets the private 
        ' field.
        '
        ' The last argument of DefineProperty is Nothing, because the
        ' property has no parameters. (If you don't specify Nothing, you must
        ' specify an array of Type objects. For a parameterless property,
        ' use the built-in array with no elements: Type.EmptyTypes)
        Dim pbNumber As PropertyBuilder = tb.DefineProperty( _
            "Number", _
            PropertyAttributes.HasDefault, _
            GetType(Integer), _
            Nothing)
      
        ' The property Set and property Get methods require a special
        ' set of attributes.
        Dim getSetAttr As MethodAttributes = _
            MethodAttributes.Public Or MethodAttributes.SpecialName _
                Or MethodAttributes.HideBySig

        ' Define the "get" accessor method for Number. The method returns
        ' an integer and has no arguments. (Note that Nothing could be 
        ' used instead of Types.EmptyTypes)
        Dim mbNumberGetAccessor As MethodBuilder = tb.DefineMethod( _
            "get_Number", _
            getSetAttr, _
            GetType(Integer), _
            Type.EmptyTypes)
      
        Dim numberGetIL As ILGenerator = mbNumberGetAccessor.GetILGenerator()
        ' For an instance property, argument zero is the instance. Load the 
        ' instance, then load the private field and return, leaving the
        ' field value on the stack.
        numberGetIL.Emit(OpCodes.Ldarg_0)
        numberGetIL.Emit(OpCodes.Ldfld, fbNumber)
        numberGetIL.Emit(OpCodes.Ret)
        
        ' Define the "set" accessor method for Number, which has no return
        ' type and takes one argument of type Integer (Int32).
        Dim mbNumberSetAccessor As MethodBuilder = _
            tb.DefineMethod( _
                "set_Number", _
                getSetAttr, _
                Nothing, _
                New Type() { GetType(Integer) })
      
        Dim numberSetIL As ILGenerator = mbNumberSetAccessor.GetILGenerator()
        ' Load the instance and then the numeric argument, then store the
        ' argument in the field.
        numberSetIL.Emit(OpCodes.Ldarg_0)
        numberSetIL.Emit(OpCodes.Ldarg_1)
        numberSetIL.Emit(OpCodes.Stfld, fbNumber)
        numberSetIL.Emit(OpCodes.Ret)
      
        ' Last, map the "get" and "set" accessor methods to the 
        ' PropertyBuilder. The property is now complete. 
        pbNumber.SetGetMethod(mbNumberGetAccessor)
        pbNumber.SetSetMethod(mbNumberSetAccessor)

        ' Define a method that accepts an integer argument and returns
        ' the product of that integer and the private field m_number. This
        ' time, the array of parameter types is created on the fly.
        Dim meth As MethodBuilder = tb.DefineMethod( _
            "MyMethod", _
            MethodAttributes.Public, _
            GetType(Integer), _
            New Type() { GetType(Integer) })

        Dim methIL As ILGenerator = meth.GetILGenerator()
        ' To retrieve the private instance field, load the instance it
        ' belongs to (argument zero). After loading the field, load the 
        ' argument one and then multiply. Return from the method with 
        ' the return value (the product of the two numbers) on the 
        ' execution stack.
        methIL.Emit(OpCodes.Ldarg_0)
        methIL.Emit(OpCodes.Ldfld, fbNumber)
        methIL.Emit(OpCodes.Ldarg_1)
        methIL.Emit(OpCodes.Mul)
        methIL.Emit(OpCodes.Ret)

        ' Finish the type.
        Dim t As Type = tb.CreateType()
     
        ' The following line saves the single-module assembly. This
        ' requires AssemblyBuilderAccess to include Save. You can now
        ' type "ildasm MyDynamicAsm.dll" at the command prompt, and 
        ' examine the assembly. You can also write a program that has
        ' a reference to the assembly, and use the MyDynamicType type.
        ' 
        ab.Save(aName.Name & ".dll") 

        ' Because AssemblyBuilderAccess includes Run, the code can be
        ' executed immediately. Start by getting reflection objects for
        ' the method and the property.
        Dim mi As MethodInfo = t.GetMethod("MyMethod")
        Dim pi As PropertyInfo = t.GetProperty("Number")
  
        ' Create an instance of MyDynamicType using the default 
        ' constructor. 
        Dim o1 As Object = Activator.CreateInstance(t)

        ' Display the value of the property, then change it to 127 and 
        ' display it again. Use Nothing to indicate that the property
        ' has no index.
        Console.WriteLine("o1.Number: {0}", pi.GetValue(o1, Nothing))
        pi.SetValue(o1, 127, Nothing)
        Console.WriteLine("o1.Number: {0}", pi.GetValue(o1, Nothing))

        ' Call MyMethod, passing 22, and display the return value, 22
        ' times 127. Arguments must be passed as an array, even when
        ' there is only one.
        Dim arguments() As Object = { 22 }
        Console.WriteLine("o1.MyMethod(22): {0}", _
            mi.Invoke(o1, arguments))

        ' Create an instance of MyDynamicType using the constructor
        ' that specifies m_Number. The constructor is identified by
        ' matching the types in the argument array. In this case, 
        ' the argument array is created on the fly. Display the 
        ' property value.
        Dim o2 As Object = Activator.CreateInstance(t, _
            New Object() { 5280 })
        Console.WriteLine("o2.Number: {0}", pi.GetValue(o2, Nothing))
      
    End Sub  
End Class

' This code produces the following output:
'
'o1.Number: 42
'o1.Number: 127
'o1.MyMethod(22): 2794
'o2.Number: 5280

備註

動態元件是使用反映發出 Api 建立的元件。A dynamic assembly is an assembly that is created using the Reflection Emit APIs. 使用 Save 方法儲存動態元件時,會儲存元件中的動態模組。The dynamic modules in the assembly are saved when the dynamic assembly is saved using the Save method. 若要產生可執行檔,必須呼叫 SetEntryPoint 方法,以識別屬於元件之進入點的方法。To generate an executable, the SetEntryPoint method must be called to identify the method that is the entry point to the assembly. 元件預設會儲存為 Dll,除非 SetEntryPoint 方法會要求產生主控台應用程式或以 Windows 為基礎的應用程式。Assemblies are saved as DLLs by default, unless the SetEntryPoint method requests the generation of a console application or a Windows-based application.

如果動態元件包含一個以上的動態模組,則元件的資訊清單檔案名應該與指定為 DefineDynamicModule 方法之第一個引數的模組名稱相符。If a dynamic assembly contains more than one dynamic module, the assembly's manifest file name should match the module's name that is specified as the first argument to the DefineDynamicModule method.

當從 AssemblyBuilder 物件呼叫時,基底 Assembly 類別上的某些方法(例如 GetModulesGetLoadedModules)將無法正確運作。Some methods on the base Assembly class, such as GetModules and GetLoadedModules, will not work correctly when called from AssemblyBuilder objects. 您可以載入定義的動態元件,並在載入的元件上呼叫方法。You can load the defined dynamic assembly and call the methods on the loaded assembly. 例如,若要確保資源模組包含在傳回的模組清單中,請在載入的 Assembly 物件上呼叫 GetModulesFor example, to ensure that resource modules are included in the returned module list, call GetModules on the loaded Assembly object.

在元件儲存至磁片之前,使用 KeyPair 來簽署動態元件是不正確。The signing of a dynamic assembly using KeyPair is not effective until the assembly is saved to disk. 因此,強式名稱不會與暫時性動態元件搭配使用。So, strong names will not work with transient dynamic assemblies.

若要取得 AssemblyBuilder 物件,請使用 AppDomain.DefineDynamicAssembly 方法。To get an AssemblyBuilder object, use the AppDomain.DefineDynamicAssembly method.

反映發出動態元件案例Reflection emit dynamic assembly scenarios

以下是使用 AssemblyBuilder 類別來建立動態元件時要考慮的一些事項。The following are some things to consider when using the AssemblyBuilder class to create dynamic assemblies.

動態模組Dynamic modules

反映發出支援建立動態元件中定義的動態模組。Reflection emit supports the creation of dynamic modules defined in dynamic assemblies. 在動態元件中建立的動態模組可以是暫時性或永久性的。A dynamic module created in a dynamic assembly can be transient or persistable.

類型參考的限制Restrictions on type references

元件可以參考另一個元件中定義的類型。Assemblies can reference types defined in another assembly. 暫時性動態元件可以安全地參考另一個暫時性動態元件、永久性動態元件或靜態元件中定義的類型。A transient dynamic assembly can safely reference types defined in another transient dynamic assembly, a persistable dynamic assembly, or a static assembly. 不過,common language runtime 不允許永久性動態模組參考暫時性動態模組中定義的類型。However, the common language runtime does not allow a persistable dynamic module to reference a type defined in a transient dynamic module. 這是因為當保存的動態模組在儲存至磁片之後載入時,執行時間無法解析暫時性動態模組中定義之類型的參考。This is because when the persisted dynamic module is loaded after being saved to disk, the runtime cannot resolve the references to types defined in the transient dynamic module.

發出至遠端應用程式域的限制Restrictions on emitting to remote application domains

某些案例需要在遠端應用程式域中建立和執行動態元件。Some scenarios require a dynamic assembly to be created and executed in a remote application domain. 反映發出不允許將動態元件直接發出至遠端應用程式域。Reflection emit does not allow a dynamic assembly to be emitted directly to a remote application domain. 解決方法是在目前的應用程式域中發出動態元件、將發出的動態元件儲存到磁片,然後將動態元件載入遠端應用程式域中。The solution is to emit the dynamic assembly in the current application domain, save the emitted dynamic assembly to disk, and then load the dynamic assembly into the remote application domain.

動態元件存取模式Dynamic assembly access modes

您可以使用下列其中一種存取模式來建立動態元件:Dynamic assemblies can be created using one of the following access modes:

  • AssemblyBuilderAccess.Run

    AssemblyBuilder 物件所表示的動態元件是暫時性的。The dynamic assembly represented by an AssemblyBuilder object is transient. 動態元件只能用來執行發出的程式碼。The dynamic assembly can only be used to execute the emitted code.

  • AssemblyBuilderAccess.Save

    AssemblyBuilder 物件所表示的動態元件是永久性的,但在從磁片載入儲存的可移植執行檔(PE)檔案之前無法執行。The dynamic assembly represented by an AssemblyBuilder object is persistable but cannot be executed until the saved portable executable (PE) file has been loaded from disk.

  • AssemblyBuilderAccess.RunAndSave

    AssemblyBuilder 物件所表示的動態元件是永久性的,但也可以在元件儲存至磁片之前和/或之後執行。The dynamic assembly represented by an AssemblyBuilder object is persistable, but can also be executed before and/or after the assembly is saved to disk.

您必須在定義動態元件時,于 AssemblyBuilder.DefineDynamicAssembly 方法的呼叫中提供適當的 AssemblyBuilderAccess 值來指定存取模式,而且稍後無法變更。The access mode must be specified by providing the appropriate AssemblyBuilderAccess value in the call to the AssemblyBuilder.DefineDynamicAssembly method when the dynamic assembly is defined and cannot be changed later. 執行時間會使用動態元件的存取模式,將元件的內部標記法優化。The runtime uses the access mode of a dynamic assembly to optimize the assembly's internal representation.

建構函式

AssemblyBuilder()

屬性

CodeBase

取得原先指定的組件位置 (例如在 AssemblyName 物件中指定的位置)。Gets the location of the assembly, as specified originally (such as in an AssemblyName object).

CustomAttributes

取得包含此組件之自訂屬性的集合。Gets a collection that contains this assembly's custom attributes.

(繼承來源 Assembly)
DefinedTypes
EntryPoint

傳回這個組件的進入點。Returns the entry point of this assembly.

EscapedCodeBase

取得代表基礎碼的 URI,包括逸出字元。Gets the URI, including escape characters, that represents the codebase.

(繼承來源 Assembly)
Evidence

取得這個組件的辨識碼。Gets the evidence for this assembly.

ExportedTypes

取得在這個組件中定義的公用類型集合,而這些類型在組件外部是可見的。Gets a collection of the public types defined in this assembly that are visible outside the assembly.

(繼承來源 Assembly)
FullName

取得目前動態組件的顯示名稱。Gets the display name of the current dynamic assembly.

GlobalAssemblyCache

取得值,這個值表示組件是否從全域組件快取載入。Gets a value that indicates whether the assembly was loaded from the global assembly cache.

HostContext

取得建立動態組件所在的主機內容。Gets the host context where the dynamic assembly is being created.

ImageRuntimeVersion

取得將要儲存在含有資訊清單之檔案中的 Common Language Runtime 版本。Gets the version of the common language runtime that will be saved in the file containing the manifest.

IsCollectible

取得指出此組件是否在可回收 AssemblyLoadContext 中保存的值。Gets a value that indicates whether this assembly is held in a collectible AssemblyLoadContext.

(繼承來源 Assembly)
IsDynamic

取得值,表示目前的組件是動態組件。Gets a value that indicates that the current assembly is a dynamic assembly.

IsFullyTrusted

取得值,這個值表示目前組件是否以完全信任來載入。Gets a value that indicates whether the current assembly is loaded with full trust.

(繼承來源 Assembly)
Location

如果不是陰影複製,則取得包含資訊清單之已載入檔案的位置 (程式碼基底格式)。Gets the location, in codebase format, of the loaded file that contains the manifest if it is not shadow-copied.

ManifestModule

在目前的 AssemblyBuilder 中取得模組,其中包含組件資訊清單。Gets the module in the current AssemblyBuilder that contains the assembly manifest.

Modules
PermissionSet

取得目前動態組件的授權集。Gets the grant set of the current dynamic assembly.

ReflectionOnly

取得值,其會表示此動態組件是否位於僅限反射內容中。Gets a value indicating whether the dynamic assembly is in the reflection-only context.

SecurityRuleSet

取得值,這個值指出應針對此組件強制執行的 Common Language Runtime (CLR) 安全性規則組合。Gets a value that indicates which set of security rules the common language runtime (CLR) enforces for this assembly.

方法

AddResourceFile(String, String)

將現有的資源檔加入這個組件。Adds an existing resource file to this assembly.

AddResourceFile(String, String, ResourceAttributes)

將現有的資源檔加入這個組件。Adds an existing resource file to this assembly.

CreateInstance(String)

從這個組件找出指定類型,並使用系統啟動項,利用區分大小寫的搜尋,建立它的執行個體。Locates the specified type from this assembly and creates an instance of it using the system activator, using case-sensitive search.

(繼承來源 Assembly)
CreateInstance(String, Boolean)

從這個組件找出指定類型,並使用系統啟動項,利用選擇性區分大小寫的搜尋,建立它的執行個體。Locates the specified type from this assembly and creates an instance of it using the system activator, with optional case-sensitive search.

(繼承來源 Assembly)
CreateInstance(String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])

從這個組件找出指定類型,並使用系統啟動項,利用選擇性區分大小寫的搜尋,以及取得指定文化特性 (Culture)、引數和繫結和啟動屬性,建立它的執行個體。Locates the specified type from this assembly and creates an instance of it using the system activator, with optional case-sensitive search and having the specified culture, arguments, and binding and activation attributes.

(繼承來源 Assembly)
DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess)

定義具有指定名稱和存取模式的動態組件。Defines a dynamic assembly that has the specified name and access rights.

DefineDynamicAssembly(AssemblyName, AssemblyBuilderAccess, IEnumerable<CustomAttributeBuilder>)

定義具有指定名稱、存取權和屬性的新組件。Defines a new assembly that has the specified name, access rights, and attributes.

DefineDynamicModule(String)

在此組件中定義具名的暫時性動態模組。Defines a named transient dynamic module in this assembly.

DefineDynamicModule(String, Boolean)

定義這個組件中的具名暫時性動態模組,並指定是否應該發出符號資訊。Defines a named transient dynamic module in this assembly and specifies whether symbol information should be emitted.

DefineDynamicModule(String, String)

以會儲存在指定檔案的指定名稱定義永久性動態模組。Defines a persistable dynamic module with the given name that will be saved to the specified file. 沒有發出任何符號資訊。No symbol information is emitted.

DefineDynamicModule(String, String, Boolean)

定義永久性動態模組,指定模組名稱、要儲存該模組的檔案名稱,以及是否應使用預設符號寫入器發出符號資訊。Defines a persistable dynamic module, specifying the module name, the name of the file to which the module will be saved, and whether symbol information should be emitted using the default symbol writer.

DefineResource(String, String, String)

使用預設公用資源屬性定義這個組件的獨立受管理資源。Defines a standalone managed resource for this assembly with the default public resource attribute.

DefineResource(String, String, String, ResourceAttributes)

定義這個組件的獨立受管理資源。Defines a standalone managed resource for this assembly. Managed 資源可指定屬性。Attributes can be specified for the managed resource.

DefineUnmanagedResource(Byte[])

將這個組件的 Unmanaged 資源定義為不透明的 Blob (位元組)。Defines an unmanaged resource for this assembly as an opaque blob of bytes.

DefineUnmanagedResource(String)

定義指定資源檔名稱的這個組件之 Unmanaged 資源檔。Defines an unmanaged resource file for this assembly given the name of the resource file.

DefineVersionInfoResource()

使用組件 AssemblyName 物件和組件自訂屬性中指定的資訊,定義 Unmanaged Version 資訊資源。Defines an unmanaged version information resource using the information specified in the assembly's AssemblyName object and the assembly's custom attributes.

DefineVersionInfoResource(String, String, String, String, String)

請使用指定的規格定義這個組件的 Unmanaged Version 資訊資源。Defines an unmanaged version information resource for this assembly with the given specifications.

Equals(Object)

傳回值,指出此執行個體是否等於指定的物件。Returns a value that indicates whether this instance is equal to the specified object.

GetCustomAttributes(Boolean)

傳回已套用至目前 AssemblyBuilder 的所有自訂屬性。Returns all the custom attributes that have been applied to the current AssemblyBuilder.

GetCustomAttributes(Type, Boolean)

傳回已套用至目前 AssemblyBuilder 的所有自訂屬性,以及衍生自指定屬性類型的所有自訂屬性。Returns all the custom attributes that have been applied to the current AssemblyBuilder, and that derive from a specified attribute type.

GetCustomAttributesData()

傳回 CustomAttributeData 物件,其中包含已套用至目前 AssemblyBuilder 的屬性之相關資訊。Returns CustomAttributeData objects that contain information about the attributes that have been applied to the current AssemblyBuilder.

GetDynamicModule(String)

傳回具有指定名稱的動態模組。Returns the dynamic module with the specified name.

GetExportedTypes()

取得這個組件中定義的匯出類型。Gets the exported types defined in this assembly.

GetFile(String)

取得指定之檔案的 FileStream,這個指定之檔案位於這個組件的資訊清單檔案表中。Gets a FileStream for the specified file in the file table of the manifest of this assembly.

GetFiles()

取得組件資訊清單檔案表中的檔案。Gets the files in the file table of an assembly manifest.

(繼承來源 Assembly)
GetFiles(Boolean)

取得組件資訊清單檔案表中的檔案,指定是否要包含資源模組。Gets the files in the file table of an assembly manifest, specifying whether to include resource modules.

GetForwardedTypes() (繼承來源 Assembly)
GetHashCode()

傳回此執行個體的雜湊碼。Returns the hash code for this instance.

GetLoadedModules()

取得做為這個組件部分的所有載入模組。Gets all the loaded modules that are part of this assembly.

(繼承來源 Assembly)
GetLoadedModules(Boolean)

傳回所有屬於這個組件的載入模組,並選擇性地包含資源模組。Returns all the loaded modules that are part of this assembly, and optionally includes resource modules.

GetManifestResourceInfo(String)

傳回指定資源已保存方式的資訊。Returns information about how the given resource has been persisted.

GetManifestResourceNames()

載入來自這個組件的指定資訊清單資源。Loads the specified manifest resource from this assembly.

GetManifestResourceStream(String)

載入來自這個組件的指定資訊清單資源。Loads the specified manifest resource from this assembly.

GetManifestResourceStream(Type, String)

從這個組件載入指定資訊清單資源,由指定類型的命名空間限定範圍。Loads the specified manifest resource, scoped by the namespace of the specified type, from this assembly.

GetModule(String)

取得這個組件中的指定模組。Gets the specified module in this assembly.

GetModules()

取得所有屬於這個組件的模組。Gets all the modules that are part of this assembly.

(繼承來源 Assembly)
GetModules(Boolean)

取得所有屬於這個組件的模組,並選擇性地包含資源模組。Gets all the modules that are part of this assembly, and optionally includes resource modules.

GetName()

取得這個組件的 AssemblyNameGets an AssemblyName for this assembly.

(繼承來源 Assembly)
GetName(Boolean)

取得目前動態組件建立時指定的 AssemblyName,且依指定方式設定程式碼基底。Gets the AssemblyName that was specified when the current dynamic assembly was created, and sets the code base as specified.

GetObjectData(SerializationInfo, StreamingContext)

使用要重新執行個體化這個組件所需的所有資料,取得序列化 (Serialization) 資訊。Gets serialization information with all of the data needed to reinstantiate this assembly.

(繼承來源 Assembly)
GetReferencedAssemblies()

取得這個 AssemblyName 所參考組件之 AssemblyBuilder 物件的不完整清單。Gets an incomplete list of AssemblyName objects for the assemblies that are referenced by this AssemblyBuilder.

GetSatelliteAssembly(CultureInfo)

取得指定文化特性的附屬組件。Gets the satellite assembly for the specified culture.

GetSatelliteAssembly(CultureInfo, Version)

取得指定文化特性的附屬組件指定版本。Gets the specified version of the satellite assembly for the specified culture.

GetType() (繼承來源 Assembly)
GetType(String)

取得具有組件執行個體中指定名稱的 Type 物件。Gets the Type object with the specified name in the assembly instance.

(繼承來源 Assembly)
GetType(String, Boolean)

取得具有組件執行個體中指定之名稱的 Type 物件,並在找不到類型時選擇性地擲回例外狀況。Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.

(繼承來源 Assembly)
GetType(String, Boolean, Boolean)

由在目前 AssemblyBuilder 中定義和建立的類型取得指定的類型。Gets the specified type from the types that have been defined and created in the current AssemblyBuilder.

GetTypes()

取得定義於這個組件中的類型。Gets the types defined in this assembly.

(繼承來源 Assembly)
IsDefined(Type, Boolean)

傳回值,這個值表示指定屬性類型的一或多個執行個體是否套用至這個成員。Returns a value that indicates whether one or more instances of the specified attribute type is applied to this member.

LoadModule(String, Byte[])

使用通用物件檔案格式 (COFF) 為基礎的映像 (包含發出的模組) 或是資源檔,載入組件的內部模組。Loads the module, internal to this assembly, with a common object file format (COFF)-based image containing an emitted module, or a resource file.

(繼承來源 Assembly)
LoadModule(String, Byte[], Byte[])

使用通用物件檔案格式 (COFF) 為基礎的映像 (包含發出的模組) 或是資源檔,載入組件的內部模組。Loads the module, internal to this assembly, with a common object file format (COFF)-based image containing an emitted module, or a resource file. 表示模組符號的未經處理位元組也載入。The raw bytes representing the symbols for the module are also loaded.

(繼承來源 Assembly)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
Save(String)

將此動態組件儲存到磁碟。Saves this dynamic assembly to disk.

Save(String, PortableExecutableKinds, ImageFileMachine)

將這個動態組件儲存至磁碟,指定在組件的可執行檔和目標平台的程式碼本質。Saves this dynamic assembly to disk, specifying the nature of code in the assembly's executables and the target platform.

SetCustomAttribute(ConstructorInfo, Byte[])

使用指定的自訂屬性 Blob 來設定這個組件上的自訂屬性。Set a custom attribute on this assembly using a specified custom attribute blob.

SetCustomAttribute(CustomAttributeBuilder)

使用自訂屬性產生器來設定這個組件上的自訂屬性。Set a custom attribute on this assembly using a custom attribute builder.

SetEntryPoint(MethodInfo)

設定這個動態組件的進入點,並假設正在建立主控台應用程式。Sets the entry point for this dynamic assembly, assuming that a console application is being built.

SetEntryPoint(MethodInfo, PEFileKinds)

設定這個組件的進入點和定義正在建置之可攜式執行檔 (PE 檔) 的類型。Sets the entry point for this assembly and defines the type of the portable executable (PE file) being built.

ToString()

傳回組件的完整名稱,也稱為顯示名稱。Returns the full name of the assembly, also known as the display name.

(繼承來源 Assembly)

事件

ModuleResolve

發生於 Common Language Runtime 類別載入器無法經由一般方法解析對組件內部模組的參考時。Occurs when the common language runtime class loader cannot resolve a reference to an internal module of an assembly through normal means.

(繼承來源 Assembly)

明確介面實作

_Assembly.GetType()

傳回目前執行個體的類型。Returns the type of the current instance.

(繼承來源 Assembly)
_AssemblyBuilder.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。Maps a set of names to a corresponding set of dispatch identifiers.

_AssemblyBuilder.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。Retrieves the type information for an object, which can then be used to get the type information for an interface.

_AssemblyBuilder.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。Retrieves the number of type information interfaces that an object provides (either 0 or 1).

_AssemblyBuilder.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。Provides access to properties and methods exposed by an object.

ICustomAttributeProvider.GetCustomAttributes(Boolean) (繼承來源 Assembly)
ICustomAttributeProvider.GetCustomAttributes(Type, Boolean) (繼承來源 Assembly)
ICustomAttributeProvider.IsDefined(Type, Boolean) (繼承來源 Assembly)

擴充方法

GetExportedTypes(Assembly)
GetModules(Assembly)
GetTypes(Assembly)
GetCustomAttribute(Assembly, Type)

擷取指定型別的自訂屬性,此屬性套用至指定組件。Retrieves a custom attribute of a specified type that is applied to a specified assembly.

GetCustomAttribute<T>(Assembly)

擷取指定型別的自訂屬性,此屬性套用至指定組件。Retrieves a custom attribute of a specified type that is applied to a specified assembly.

GetCustomAttributes(Assembly)

擷取套用至指定組件的自訂屬性集合。Retrieves a collection of custom attributes that are applied to a specified assembly.

GetCustomAttributes(Assembly, Type)

擷取指定型別的自訂屬性集合,此集合套用至指定組件。Retrieves a collection of custom attributes of a specified type that are applied to a specified assembly.

GetCustomAttributes<T>(Assembly)

擷取指定型別的自訂屬性集合,此集合套用至指定組件。Retrieves a collection of custom attributes of a specified type that are applied to a specified assembly.

IsDefined(Assembly, Type)

指出是否將所指定型別的自訂屬性套用至指定的組件。Indicates whether custom attributes of a specified type are applied to a specified assembly.

TryGetRawMetadata(Assembly, Byte*, Int32)

適用於

另請參閱