TypeBuilder.DefineMethod Metod

Definition

Lägger till en metod i typen.

Överlagringar

Name Description
DefineMethod(String, MethodAttributes)

Lägger till en ny metod i typen med det angivna namnet och metodattributen.

DefineMethod(String, MethodAttributes, CallingConventions)

Lägger till en ny metod i typen med det angivna namnet, metodattributen och anropskonventionen.

DefineMethod(String, MethodAttributes, Type, Type[])

Lägger till en ny metod i typen med det angivna namnet, metodattributen och metodsignaturen.

DefineMethod(String, MethodAttributes, CallingConventions, Type, Type[])

Lägger till en ny metod i typen med angivet namn, metodattribut, anropande konvention och metodsignatur.

DefineMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])

Lägger till en ny metod i typen med angivet namn, metodattribut, anropande konvention, metodsignatur och anpassade modifierare.

DefineMethod(String, MethodAttributes)

Lägger till en ny metod i typen med det angivna namnet och metodattributen.

public:
 System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes);
public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes);
member this.DefineMethod : string * System.Reflection.MethodAttributes -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes) As MethodBuilder

Parametrar

name
String

Namnet på metoden. name kan inte innehålla inbäddade null-värden.

attributes
MethodAttributes

Metodens attribut.

Returer

En MethodBuilder som representerar den nyligen definierade metoden.

Undantag

Längden name på är noll.

-eller-

Typen av överordnad metod är ett gränssnitt och den här metoden är inte virtuell (Overridable i Visual Basic).

name är null.

Typen skapades tidigare med .CreateType()

-eller-

För den aktuella dynamiska typen är trueegenskapen IsGenericType , men egenskapen IsGenericTypeDefinition är false.

Exempel

I följande kodexempel definieras en allmän metod med namnet DemoMethod vars parametertyp och returtyp anges av dess allmänna typparametrar. Metoden definieras utan signatur med hjälp av standardanropskonventionen. Metoden MethodBuilder.DefineGenericParameters används för att skapa DemoMethod en generisk metod, och de nyligen definierade typparametrarna används sedan för signatur- och returtypen.

Det här kodexemplet är en del av ett större exempel för DefineGenericParameters metoden.

// Define a Shared, Public method with standard calling
// conventions. Do not specify the parameter types or the
// return type, because type parameters will be used for
// those types, and the type parameters have not been
// defined yet.
MethodBuilder demoMethod = demoType.DefineMethod(
    "DemoMethod",
    MethodAttributes.Public | MethodAttributes.Static
);
' Define a Shared, Public method with standard calling
' conventions. Do not specify the parameter types or the
' return type, because type parameters will be used for 
' those types, and the type parameters have not been
' defined yet.
Dim demoMethod As MethodBuilder = _
    demoType.DefineMethod("DemoMethod", _
        MethodAttributes.Public Or MethodAttributes.Static)
// Defining generic parameters for the method makes it a
// generic method. By convention, type parameters are
// single alphabetic characters. T and U are used here.
//
string[] typeParamNames = {"T", "U"};
GenericTypeParameterBuilder[] typeParameters =
    demoMethod.DefineGenericParameters(typeParamNames);

// The second type parameter is constrained to be a
// reference type.
typeParameters[1].SetGenericParameterAttributes(
    GenericParameterAttributes.ReferenceTypeConstraint);
' Defining generic parameters for the method makes it a
' generic method. By convention, type parameters are 
' single alphabetic characters. T and U are used here.
'
Dim typeParamNames() As String = {"T", "U"}
Dim typeParameters() As GenericTypeParameterBuilder = _
    demoMethod.DefineGenericParameters(typeParamNames)

' The second type parameter is constrained to be a 
' reference type.
typeParameters(1).SetGenericParameterAttributes( _
    GenericParameterAttributes.ReferenceTypeConstraint)
// Set parameter types for the method. The method takes
// one parameter, and its type is specified by the first
// type parameter, T.
Type[] parms = {typeParameters[0]};
demoMethod.SetParameters(parms);

// Set the return type for the method. The return type is
// specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters[1]);
' Set parameter types for the method. The method takes
' one parameter, and its type is specified by the first
' type parameter, T.
Dim params() As Type = {typeParameters(0)}
demoMethod.SetParameters(params)

' Set the return type for the method. The return type is
' specified by the second type parameter, U.
demoMethod.SetReturnType(typeParameters(1))

Kommentarer

Använd den här metodens överlagring när du inte känner till metodsignaturen när du definierar metoden. Parametertyperna och returtypen för en generisk metod kan till exempel anges av metodens parametrar av allmän typ, som måste definieras när metoden har lagts till i typen. Parametrarna och returtypen för metoden kan anges senare med hjälp av MethodBuilder.SetSignature metoden.

Den här metodens överlagring definierar en metod med CallingConventions.Standard. Om du behöver definiera en metod utan signatur, med en annan anropskonvention, använder du DefineMethod(String, MethodAttributes, CallingConventions) metodens överlagring.

Se även

Gäller för

DefineMethod(String, MethodAttributes, CallingConventions)

Lägger till en ny metod i typen med det angivna namnet, metodattributen och anropskonventionen.

public:
 System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention);
public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention);
member this.DefineMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions) As MethodBuilder

Parametrar

name
String

Namnet på metoden. name kan inte innehålla inbäddade null-värden.

attributes
MethodAttributes

Metodens attribut.

callingConvention
CallingConventions

Metodens anropskonvention.

Returer

En MethodBuilder som representerar den nyligen definierade metoden.

Undantag

Längden name på är noll.

-eller-

Typen av överordnad metod är ett gränssnitt och den här metoden är inte virtuell (Overridable i Visual Basic).

name är null.

Typen skapades tidigare med .CreateType()

-eller-

För den aktuella dynamiska typen är trueegenskapen IsGenericType , men egenskapen IsGenericTypeDefinition är false.

Kommentarer

Använd den här metodens överlagring när du inte känner till metodsignaturen när du definierar metoden. Parametertyperna och returtypen för en generisk metod kan till exempel anges av metodens parametrar av allmän typ, som måste definieras när metoden har lagts till i typen. Parametrarna och returtypen för metoden kan anges senare med hjälp av MethodBuilder.SetSignature metoden.

Se även

Gäller för

DefineMethod(String, MethodAttributes, Type, Type[])

Lägger till en ny metod i typen med det angivna namnet, metodattributen och metodsignaturen.

public:
 System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes, Type returnType, Type[] parameterTypes);
member this.DefineMethod : string * System.Reflection.MethodAttributes * Type * Type[] -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes, returnType As Type, parameterTypes As Type()) As MethodBuilder

Parametrar

name
String

Namnet på metoden. name kan inte innehålla inbäddade null-värden.

attributes
MethodAttributes

Metodens attribut.

returnType
Type

Metodens returtyp.

parameterTypes
Type[]

Typerna av parametrarna för metoden.

Returer

Den definierade metoden.

Undantag

Längden name på är noll.

-eller-

Typen av överordnad metod är ett gränssnitt och den här metoden är inte virtuell (Overridable i Visual Basic).

name är null.

Typen skapades tidigare med .CreateType()

-eller-

För den aktuella dynamiska typen är trueegenskapen IsGenericType , men egenskapen IsGenericTypeDefinition är false.

Exempel

Följande kodexempel visar användningen av DefineMethod för att ange en konstruktors specifika signatur och attribut på en dynamisk typ och för att returnera en motsvarande MethodBuilder för MSIL-populationen.

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;

public interface IMyInterface
{
   String HelloMethod(String parameter);
}

public class Example
{
   public static void Main()
   {
      Type myNestedClassType = CreateCallee(Thread.GetDomain());
      // Cretae an instance of 'MyNestedClass'.
      IMyInterface myInterface =
         (IMyInterface)Activator.CreateInstance(myNestedClassType);
      Console.WriteLine(myInterface.HelloMethod("Bill"));
   }

   // Create the callee transient dynamic assembly.
   private static Type CreateCallee(AppDomain myAppDomain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "Example";
      // Create the callee dynamic assembly.
      AssemblyBuilder myAssembly =
         myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
      // Create a dynamic module in the callee assembly.
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
      // Define a public class named "MyHelloWorld".
      TypeBuilder myHelloWorldType =
         myModule.DefineType("MyHelloWorld", TypeAttributes.Public);
      // Define a public nested class named 'MyNestedClass'.
      TypeBuilder myNestedClassType =
         myHelloWorldType.DefineNestedType("MyNestedClass",
            TypeAttributes.NestedPublic, typeof(Example),
            new Type[]{typeof(IMyInterface)});
      // Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(typeof(IMyInterface));
      // Define 'HelloMethod' of 'IMyInterface'.
      MethodBuilder myHelloMethod =
         myNestedClassType.DefineMethod("HelloMethod",
            MethodAttributes.Public | MethodAttributes.Virtual,
            typeof(String), new Type[]{typeof(String)});
      // Generate IL for 'GetGreeting' method.
      ILGenerator myMethodIL = myHelloMethod.GetILGenerator();
      myMethodIL.Emit(OpCodes.Ldstr, "Hi! ");
      myMethodIL.Emit(OpCodes.Ldarg_1);
      MethodInfo infoMethod =
         typeof(String).GetMethod("Concat",new Type[]{typeof(string),typeof(string)});
      myMethodIL.Emit(OpCodes.Call, infoMethod);
      myMethodIL.Emit(OpCodes.Ret);

      MethodInfo myHelloMethodInfo =
         typeof(IMyInterface).GetMethod("HelloMethod");
      // Implement 'HelloMethod' of 'IMyInterface'.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo);
      // Create 'MyHelloWorld' type.
      Type myType = myHelloWorldType.CreateType();
      // Create 'MyNestedClass' type.
      return myNestedClassType.CreateType();
   }
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions

Public Interface IMyInterface
   Function HelloMethod(parameter As String) As String
End Interface 'IMyInterface

Public Class Example
   <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
   Public Shared Sub Main()
      Dim myNestedClassType As Type = CreateCallee(Thread.GetDomain())
      ' Create an instance of 'MyNestedClass'.
      Dim myInterface As IMyInterface = _
            CType(Activator.CreateInstance(myNestedClassType), IMyInterface)
      Console.WriteLine(myInterface.HelloMethod("Bill"))
   End Sub

   ' Create the callee transient dynamic assembly.
   Private Shared Function CreateCallee(myAppDomain As AppDomain) As Type
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "Example"
      ' Create the callee dynamic assembly.
      Dim myAssembly As AssemblyBuilder = _
               myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
      ' Create a dynamic module in the callee assembly.
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
      ' Define a public class named "MyHelloWorld".
      Dim myHelloWorldType As TypeBuilder = _
               myModule.DefineType("MyHelloWorld", TypeAttributes.Public)
      ' Define a public nested class named 'MyNestedClass'.
      Dim myNestedClassType As TypeBuilder = _
               myHelloWorldType.DefineNestedType("MyNestedClass", TypeAttributes.NestedPublic, _
               GetType(Example), New Type() {GetType(IMyInterface)})
      ' Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(GetType(IMyInterface))
      ' Define 'HelloMethod' of 'IMyInterface'.
      Dim myHelloMethod As MethodBuilder = _
               myNestedClassType.DefineMethod("HelloMethod", MethodAttributes.Public Or _
               MethodAttributes.Virtual, GetType(String), New Type() {GetType(String)})
      ' Generate IL for 'GetGreeting' method.
      Dim myMethodIL As ILGenerator = myHelloMethod.GetILGenerator()
      myMethodIL.Emit(OpCodes.Ldstr, "Hi! ")
      myMethodIL.Emit(OpCodes.Ldarg_1)
      Dim infoMethod As MethodInfo = _
               GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String)})
      myMethodIL.Emit(OpCodes.Call, infoMethod)
      myMethodIL.Emit(OpCodes.Ret)

      Dim myHelloMethodInfo As MethodInfo = GetType(IMyInterface).GetMethod("HelloMethod")
      ' Implement 'HelloMethod' of 'IMyInterface'.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo)
      ' Create 'MyHelloWorld' type.
      Dim myType As Type = myHelloWorldType.CreateType()
      ' Create 'MyNestedClass' type.
      Return myNestedClassType.CreateType()
   End Function 'CreateCallee
End Class

Gäller för

DefineMethod(String, MethodAttributes, CallingConventions, Type, Type[])

Lägger till en ny metod i typen med angivet namn, metodattribut, anropande konvention och metodsignatur.

public:
 System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ parameterTypes);
public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] parameterTypes);
member this.DefineMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, parameterTypes As Type()) As MethodBuilder

Parametrar

name
String

Namnet på metoden. name kan inte innehålla inbäddade null-värden.

attributes
MethodAttributes

Metodens attribut.

callingConvention
CallingConventions

Metodens anropskonvention.

returnType
Type

Metodens returtyp.

parameterTypes
Type[]

Typerna av parametrarna för metoden.

Returer

En MethodBuilder som representerar den nyligen definierade metoden.

Undantag

Längden name på är noll.

-eller-

Typen av överordnad metod är ett gränssnitt och den här metoden är inte virtuell (Overridable i Visual Basic).

name är null.

Typen skapades tidigare med .CreateType()

-eller-

För den aktuella dynamiska typen är trueegenskapen IsGenericType , men egenskapen IsGenericTypeDefinition är false.

Exempel

Följande kodexempel visar användningen av DefineMethod för att ange en konstruktors specifika signatur och attribut på en dynamisk typ och för att returnera en motsvarande MethodBuilder för MSIL-populationen.

using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;

public interface IMyInterface
{
   String HelloMethod(String parameter);
}

public class Example
{
   public static void Main()
   {
      Type myNestedClassType = CreateCallee(Thread.GetDomain());
      // Cretae an instance of 'MyNestedClass'.
      IMyInterface myInterface =
         (IMyInterface)Activator.CreateInstance(myNestedClassType);
      Console.WriteLine(myInterface.HelloMethod("Bill"));
   }

   // Create the callee transient dynamic assembly.
   private static Type CreateCallee(AppDomain myAppDomain)
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "Example";
      // Create the callee dynamic assembly.
      AssemblyBuilder myAssembly =
         myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run);
      // Create a dynamic module in the callee assembly.
      ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
      // Define a public class named "MyHelloWorld".
      TypeBuilder myHelloWorldType =
         myModule.DefineType("MyHelloWorld", TypeAttributes.Public);
      // Define a public nested class named 'MyNestedClass'.
      TypeBuilder myNestedClassType =
         myHelloWorldType.DefineNestedType("MyNestedClass",
            TypeAttributes.NestedPublic, typeof(Example),
            new Type[]{typeof(IMyInterface)});
      // Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(typeof(IMyInterface));
      // Define 'HelloMethod' of 'IMyInterface'.
      MethodBuilder myHelloMethod =
         myNestedClassType.DefineMethod("HelloMethod",
            MethodAttributes.Public | MethodAttributes.Virtual,
            typeof(String), new Type[]{typeof(String)});
      // Generate IL for 'GetGreeting' method.
      ILGenerator myMethodIL = myHelloMethod.GetILGenerator();
      myMethodIL.Emit(OpCodes.Ldstr, "Hi! ");
      myMethodIL.Emit(OpCodes.Ldarg_1);
      MethodInfo infoMethod =
         typeof(String).GetMethod("Concat",new Type[]{typeof(string),typeof(string)});
      myMethodIL.Emit(OpCodes.Call, infoMethod);
      myMethodIL.Emit(OpCodes.Ret);

      MethodInfo myHelloMethodInfo =
         typeof(IMyInterface).GetMethod("HelloMethod");
      // Implement 'HelloMethod' of 'IMyInterface'.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo);
      // Create 'MyHelloWorld' type.
      Type myType = myHelloWorldType.CreateType();
      // Create 'MyNestedClass' type.
      return myNestedClassType.CreateType();
   }
}
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions

Public Interface IMyInterface
   Function HelloMethod(parameter As String) As String
End Interface 'IMyInterface

Public Class Example
   <PermissionSetAttribute(SecurityAction.Demand, Name:="FullTrust")> _
   Public Shared Sub Main()
      Dim myNestedClassType As Type = CreateCallee(Thread.GetDomain())
      ' Create an instance of 'MyNestedClass'.
      Dim myInterface As IMyInterface = _
            CType(Activator.CreateInstance(myNestedClassType), IMyInterface)
      Console.WriteLine(myInterface.HelloMethod("Bill"))
   End Sub

   ' Create the callee transient dynamic assembly.
   Private Shared Function CreateCallee(myAppDomain As AppDomain) As Type
      Dim myAssemblyName As New AssemblyName()
      myAssemblyName.Name = "Example"
      ' Create the callee dynamic assembly.
      Dim myAssembly As AssemblyBuilder = _
               myAppDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
      ' Create a dynamic module in the callee assembly.
      Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
      ' Define a public class named "MyHelloWorld".
      Dim myHelloWorldType As TypeBuilder = _
               myModule.DefineType("MyHelloWorld", TypeAttributes.Public)
      ' Define a public nested class named 'MyNestedClass'.
      Dim myNestedClassType As TypeBuilder = _
               myHelloWorldType.DefineNestedType("MyNestedClass", TypeAttributes.NestedPublic, _
               GetType(Example), New Type() {GetType(IMyInterface)})
      ' Implement 'IMyInterface' interface.
      myNestedClassType.AddInterfaceImplementation(GetType(IMyInterface))
      ' Define 'HelloMethod' of 'IMyInterface'.
      Dim myHelloMethod As MethodBuilder = _
               myNestedClassType.DefineMethod("HelloMethod", MethodAttributes.Public Or _
               MethodAttributes.Virtual, GetType(String), New Type() {GetType(String)})
      ' Generate IL for 'GetGreeting' method.
      Dim myMethodIL As ILGenerator = myHelloMethod.GetILGenerator()
      myMethodIL.Emit(OpCodes.Ldstr, "Hi! ")
      myMethodIL.Emit(OpCodes.Ldarg_1)
      Dim infoMethod As MethodInfo = _
               GetType(String).GetMethod("Concat", New Type() {GetType(String), GetType(String)})
      myMethodIL.Emit(OpCodes.Call, infoMethod)
      myMethodIL.Emit(OpCodes.Ret)

      Dim myHelloMethodInfo As MethodInfo = GetType(IMyInterface).GetMethod("HelloMethod")
      ' Implement 'HelloMethod' of 'IMyInterface'.
      myNestedClassType.DefineMethodOverride(myHelloMethod, myHelloMethodInfo)
      ' Create 'MyHelloWorld' type.
      Dim myType As Type = myHelloWorldType.CreateType()
      ' Create 'MyNestedClass' type.
      Return myNestedClassType.CreateType()
   End Function 'CreateCallee
End Class

Gäller för

DefineMethod(String, MethodAttributes, CallingConventions, Type, Type[], Type[], Type[], Type[][], Type[][])

Lägger till en ny metod i typen med angivet namn, metodattribut, anropande konvention, metodsignatur och anpassade modifierare.

public:
 System::Reflection::Emit::MethodBuilder ^ DefineMethod(System::String ^ name, System::Reflection::MethodAttributes attributes, System::Reflection::CallingConventions callingConvention, Type ^ returnType, cli::array <Type ^> ^ returnTypeRequiredCustomModifiers, cli::array <Type ^> ^ returnTypeOptionalCustomModifiers, cli::array <Type ^> ^ parameterTypes, cli::array <cli::array <Type ^> ^> ^ parameterTypeRequiredCustomModifiers, cli::array <cli::array <Type ^> ^> ^ parameterTypeOptionalCustomModifiers);
public System.Reflection.Emit.MethodBuilder DefineMethod(string name, System.Reflection.MethodAttributes attributes, System.Reflection.CallingConventions callingConvention, Type returnType, Type[] returnTypeRequiredCustomModifiers, Type[] returnTypeOptionalCustomModifiers, Type[] parameterTypes, Type[][] parameterTypeRequiredCustomModifiers, Type[][] parameterTypeOptionalCustomModifiers);
member this.DefineMethod : string * System.Reflection.MethodAttributes * System.Reflection.CallingConventions * Type * Type[] * Type[] * Type[] * Type[][] * Type[][] -> System.Reflection.Emit.MethodBuilder
Public Function DefineMethod (name As String, attributes As MethodAttributes, callingConvention As CallingConventions, returnType As Type, returnTypeRequiredCustomModifiers As Type(), returnTypeOptionalCustomModifiers As Type(), parameterTypes As Type(), parameterTypeRequiredCustomModifiers As Type()(), parameterTypeOptionalCustomModifiers As Type()()) As MethodBuilder

Parametrar

name
String

Namnet på metoden. name kan inte innehålla inbäddade null-värden.

attributes
MethodAttributes

Metodens attribut.

callingConvention
CallingConventions

Metodens anropskonvention.

returnType
Type

Metodens returtyp.

returnTypeRequiredCustomModifiers
Type[]

En matris med typer som representerar nödvändiga anpassade modifierare, till exempel IsConst, för metodens returtyp. Om returtypen inte har några nödvändiga anpassade modifierare anger du null.

returnTypeOptionalCustomModifiers
Type[]

En matris med typer som representerar valfria anpassade modifierare, till exempel IsConst, för metodens returtyp. Om returtypen inte har några valfria anpassade modifierare anger du null.

parameterTypes
Type[]

Typerna av parametrarna för metoden.

parameterTypeRequiredCustomModifiers
Type[][]

En matris med matriser av typer. Varje matris med typer representerar nödvändiga anpassade modifierare för motsvarande parameter, till exempel IsConst. Om en viss parameter inte har några nödvändiga anpassade modifierare anger du null i stället för en matris med typer. Om ingen av parametrarna har nödvändiga anpassade modifierare anger du null i stället för en matris med matriser.

parameterTypeOptionalCustomModifiers
Type[][]

En matris med matriser av typer. Varje matris med typer representerar valfria anpassade modifierare för motsvarande parameter, till exempel IsConst. Om en viss parameter inte har några valfria anpassade modifierare anger du null i stället för en matris med typer. Om ingen av parametrarna har valfria anpassade modifierare anger du null i stället för en matris med matriser.

Returer

Ett MethodBuilder objekt som representerar den nyligen tillagda metoden.

Undantag

Längden name på är noll.

-eller-

Typen av överordnad metod är ett gränssnitt och den här metoden är inte virtuell (Overridable i Visual Basic).

-eller-

Storleken på parameterTypeRequiredCustomModifiers eller parameterTypeOptionalCustomModifiers är inte lika med storleken på parameterTypes.

name är null.

Typen skapades tidigare med .CreateType()

-eller-

För den aktuella dynamiska typen är trueegenskapen IsGenericType , men egenskapen IsGenericTypeDefinition är false.

Kommentarer

Använd den här överlagringen om du behöver ange anpassade modifierare. Om du behöver ange anpassade modifierare när metoden har skapats, som du till exempel skulle göra med en allmän metod vars parametertyper anges av dess allmänna typparametrar, kan du använda DefineMethod(String, MethodAttributes) metoden eller DefineMethod(String, MethodAttributes, CallingConventions) överlagringar för att definiera metoden och sedan använda MethodBuilder.SetSignature metoden för att definiera parametern och returnera typer med anpassade modifierare.

Gäller för