public sealed class MethodBuilder : MethodInfo
|
Attributes | Read-only Overridden: Retrieves the attributes for this method. |
CallingConvention | Read-only Overridden: Returns the calling convention of the method. |
DeclaringType | Read-only Overridden: Returns the type that declares this method. |
InitLocals | Read-write Gets or sets whether the local variables in this method should be zero initialized. The default value of this property is true. |
IsAbstract (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsAbstract Gets a value indicating whether the method is abstract. |
IsAssembly (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsAssembly Gets a value indicating whether this method can be called by other classes in the same assembly. |
IsConstructor (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsConstructor Gets a value indicating whether the method is a constructor. |
IsFamily (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsFamily Gets a value indicating whether access to this method is restricted to members of the class and members of its derived classes. |
IsFamilyAndAssembly (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsFamilyAndAssembly Gets a value indicating whether this method can be called by derived classes if they are in the same assembly. |
IsFamilyOrAssembly (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsFamilyOrAssembly Gets a value indicating whether this method can be called by derived classes, wherever they are, and by all classes in the same assembly. |
IsFinal (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsFinal Gets a value indicating whether this method is final. |
IsHideBySig (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsHideBySig Gets a value indicating whether only a member of the same kind with exactly the same signature is hidden in the derived class. |
IsPrivate (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsPrivate Gets a value indicating whether this member is private. |
IsPublic (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsPublic Gets a value indicating whether this is a public method. |
IsSpecialName (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsSpecialName Gets a value indicating whether this method has a special name. |
IsStatic (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsStatic Gets a value indicating whether the method is static. |
IsVirtual (inherited from System.Reflection.MethodBase) |
Read-only See base class member description: System.Reflection.MethodBase.IsVirtual Gets a value indicating whether the method is virtual. |
MemberType (inherited from System.Reflection.MethodInfo) |
Read-only See base class member description: System.Reflection.MethodInfo.MemberType Gets a value indicating that this member is a method. |
MethodHandle | Read-only Overridden: Retrieves the internal handle for the method. Use this handle to access the underlying metadata handle. |
Name | Read-only Overridden: Retrieves the name of this method. |
ReflectedType | Read-only Overridden: Retrieves the class that was used in reflection to obtain this object. |
ReturnType | Read-only Overridden: Retrieves the type of this method's return value. |
ReturnTypeCustomAttributes | Read-only Overridden: Returns the custom attributes of the method's return type. |
Signature | Read-only Retrieves the signature of the field. |
AddDeclarativeSecurity | Adds declarative security to this method. |
CreateMethodBody | Creates the body of the method using the given array of Microsoft intermediate language (MSIL) instructions. |
DefineParameter | Defines a parameter of this method. |
Equals | Overridden: Determines whether the given object is equal to this instance. |
GetBaseDefinition | Overridden: Return the base implementation for a method. |
GetCustomAttributes | Overloaded:GetCustomAttributes(bool inherit) Overridden: Returns all the custom attributes defined for this method. |
GetCustomAttributes | Overloaded:GetCustomAttributes(Type attributeType, bool inherit) Overridden: Returns the custom attributes identified by the given type. |
GetHashCode | Overridden: Gets the hash code for this method. |
GetILGenerator | Overloaded:GetILGenerator() Returns an ILGenerator for this method with a default Microsoft intermediate language (MSIL) stream size of 64 bytes. |
GetILGenerator | Overloaded:GetILGenerator(int size) Returns an ILGenerator for this method with the specified Microsoft intermediate language (MSIL) stream size. |
GetMethodImplementationFlags | Overridden: Returns the implementation flags for the method. |
GetModule | Returns a reference to the module that contains this method. |
GetParameters | Overridden: Returns the parameters of this method. |
GetToken | Returns the MethodToken that represents the token for this method. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Invoke (inherited from System.Reflection.MethodBase) |
Overloaded:Invoke(object obj, object[] parameters) See base class member description: System.Reflection.MethodBase.InvokeInvokes the underlying method or constructor represented by this MethodInfo object with the specified parameters. |
Invoke | Overloaded:Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) Overridden: Dynamically invokes the method reflected by this instance on the given object, passing along the specified parameters, and under the constraints of the given binder. |
IsDefined | Overridden: Checks if the specified custom attribute type is defined. |
SetCustomAttribute | Overloaded:SetCustomAttribute(CustomAttributeBuilder customBuilder) Sets a custom attribute using a custom attribute builder. |
SetCustomAttribute | Overloaded:SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute) Sets a custom attribute using a specified custom attribute blob. |
SetImplementationFlags | Sets the implementation flags for this method. |
SetMarshal | Sets marshaling information for the return type of this method. |
SetSymCustomAttribute | Set a symbolic custom attribute using a blob. |
ToString | Overridden: Returns this MethodBuilder instance as a string. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public override MethodAttributes Attributes {get;}
|
public override CallingConventions CallingConvention {get;}
|
public override Type DeclaringType {get;}
|
public bool InitLocals {get; set;}
|
public bool IsAbstract {get;}
|
To get the MethodBase, first get the type. From the type, get the method. From the method, get the MethodBase. If the MethodBase or constructor is other than public, it is protected and cannot be readily accessed. To access a non-public method, set the BindingFlags mask to NonPublic in GetMethod.
class methodbase { public static int Main(string[] args) { Console.WriteLine ("\nReflection.MethodBase"); //Get the MethodBase of two methods. //Get the types Type MyType1 = Type.GetType("System.Runtime.Serialization.Formatter"); Type MyType2 = Type.GetType("System.Reflection.MethodBase"); //Get and display the methods MethodBase Mymethodbase1 = MyType1.GetMethod("WriteInt32", BindingFlags.NonPublic|BindingFlags.Instance); MethodBase Mymethodbase2 = MyType2.GetMethod("GetCurrentMethod", BindingFlags.Public|BindingFlags.Static); Console.Write("\nMymethodbase = " + Mymethodbase1.ToString()); if (Mymethodbase1.IsAbstract) Console.Write ("\nMymethodbase is an abstract method"); else Console.Write ("\nMymethodbase is not an abstract method"); Console.Write("\n\nMymethodbase = " + Mymethodbase2.ToString()); if (Mymethodbase2.IsAbstract) Console.Write ("\nMymethodbase is an abstract method"); else Console.Write ("\nMymethodbase is not an abstract method"); return 0; } } /* Produces the following output Reflection.MethodBase Mymethodbase = Void WriteInt32 (Int32, System.String) Mymethodbase is an abstract method Mymethodbase = System.Reflection.MethodBase GetCurrentMethod () Mymethodbase is not an abstract method */
public bool IsAssembly {get;}
|
To get the MethodBase, first get the type. From the type, get the method. From the method, get the MethodBase. If the MethodBase or constructor is other than public, it is protected and cannot be readily accessed. To access a non-public method, set the BindingFlags mask to NonPublic in GetMethod.
class methodbase { internal void f() { } public static int Main(string[] args) { Console.WriteLine ("\nReflection.MethodBase"); //Get the MethodBase of two methods. //Get the types Type MyType1 = Type.GetType("System.Runtime.Serialization.Formatter"); Type MyType2 = Type.GetType("methodbase"); //Get and display the methods and the IsAssembly MethodBase Mymethodbase1 = MyType1.GetMethod("WriteInt32",BindingFlags.NonPublic|BindingFlags.Instance); MethodBase Mymethodbase2 = MyType2.GetMethod("f", BindingFlags.NonPublic|BindingFlags.Instance); Console.Write("\nMymethodbase = " + Mymethodbase1.ToString()); if (Mymethodbase1.IsAssembly) Console.Write ("\nMymethodbase is an assembly method"); else Console.Write ("\nMymethodbase is not an assembly method"); Console.Write("\n\nMymethodbase = " + Mymethodbase2.ToString()); if (Mymethodbase2.IsAssembly) Console.Write ("\nMymethodbase is an assembly method"); else Console.Write ("\nMymethodbase is not an assembly method"); return 0; } } /* Produces the following output Reflection.MethodBase Mymethodbase = Void WriteInt32 (Int32, System.String) Mymethodbase is not an assembly method Mymethodbase = Void f() Mymethodbase is an assembly method */
public bool IsConstructor {get;}
|
public bool IsFamily {get;}
|
public bool IsFamilyAndAssembly {get;}
|
public bool IsFamilyOrAssembly {get;}
|
public bool IsFinal {get;}
|
To establish with certainty whether a method is overridable, use code such as this:
if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)
If IsVirtual is false or IsFinal is true, then the method cannot be overridden.
using System; using System.Reflection; public class MyClass { public void MyMethod() { } public static void Main() { MethodBase m = typeof(MyClass).GetMethod("MyMethod"); Console.WriteLine(m.IsFinal); } }
public bool IsHideBySig {get;}
|
public bool IsPrivate {get;}
|
public bool IsPublic {get;}
|
class methodbase { public static int Main(string[] args) { Console.WriteLine("\nReflection.MethodBase"); //Get the MethodBase of a method. //Get the type Type MyType = Type.GetType("System.MulticastDelegate"); //Get and display the method MethodBase Mymethodbase = MyType.GetMethod("RemoveImpl",BindingFlags.NonPublic); Console.Write("\nMymethodbase = " + Mymethodbase); bool Myispublic = Mymethodbase.IsPublic; if (Myispublic) Console.Write ("\nMymethodbase is a public method"); else Console.Write ("\nMymethodbase is not a public method"); return 0; } } /* Produces the following output Reflection.MethodBase Mymethodbase = System.Delegate RemoveImpl (System.Delegate) Mymethodbase is not a public method */
public bool IsSpecialName {get;}
|
public bool IsStatic {get;}
|
public bool IsVirtual {get;}
|
To determine if a method is overridable, it is not sufficient to check that IsVirtual is true. For a method to be overridable, IsVirtual must be true and MethodBase.IsFinal must be false. For example, a method might be non-virtual, but it implements an interface method. The common language runtime requires that all methods that implement interface members must be marked as virtual; therefore, the compiler marks the method virtualfinal. So there are cases where a method is marked as virtual but is still not overridable.
To establish with certainty whether a method is overridable, use code such as this:
if (MethodInfo.IsVirtual && !MethodInfo.IsFinal)
If IsVirtual is false or IsFinal is true, then the method cannot be overridden.
using System; using System.Reflection; public class MyClass { public void MyMethod() { } public static void Main() { MethodBase m = typeof(MyClass).GetMethod("MyMethod"); Console.WriteLine(m.IsFinal); } }
public override MemberTypes MemberType {get;}
|
To get the MemberType property, first get the class Type. From the Type, get the MethodInfo. From the MethodInfo, get the MemberType.
using System; using System.Reflection; class MyMethodInfo { public static int Main() { Console.WriteLine("Reflection.MethodInfo"); //Get the Type and MethodInfo. Type MyType = Type.GetType("System.Reflection.FieldInfo"); MethodInfo Mymethodinfo = MyType.GetMethod("GetValue"); Console.WriteLine(MyType.FullName + "." + Mymethodinfo.Name); //Get and display the MemberType property. MemberTypes Mymembertypes = Mymethodinfo.MemberType; if ( MemberTypes.Constructor == Mymembertypes ) { Console.WriteLine( "MemberType is of type All" ); } else if ( MemberTypes.Custom == Mymembertypes ) { Console.WriteLine( "MemberType is of type Custom" ); } else if ( MemberTypes.Event == Mymembertypes ) { Console.WriteLine( "MemberType is of type Event" ); } else if ( MemberTypes.Field == Mymembertypes ) { Console.WriteLine( "MemberType is of type Field" ); } else if ( MemberTypes.Method == Mymembertypes ) { Console.WriteLine( "MemberType is of type Method" ); } else if ( MemberTypes.Property == Mymembertypes ) { Console.WriteLine( "MemberType is of type Property" ); } else if ( MemberTypes.TypeInfo == Mymembertypes ) { Console.WriteLine( "MemberType is of type TypeInfo" ); } return 0; } } /* This code produces the following output: Reflection.MethodInfo System.Reflection.FieldInfo.GetValue MemberType is of type Method */
public override RuntimeMethodHandle MethodHandle {get;}
|
Exception Type | Condition |
---|---|
NotSupportedException | This method is not currently supported. Retrieve the method using Type.GetMethod and call MethodBase.MethodHandle on the returned MethodInfo. |
public override string Name {get;}
|
public override Type ReflectedType {get;}
|
public override Type ReturnType {get;}
|
public override ICustomAttributeProvider ReturnTypeCustomAttributes {get;}
|
public string Signature {get;}
|
public void AddDeclarativeSecurity( |
action
pset
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The action is invalid (RequestMinimum, RequestOptional, and RequestRefuse are invalid). |
InvalidOperationException | The containing type has been created using TypeBuilder.CreateType or if the permission set pset contains an action that was added earlier by AddDeclarativeSecurity. |
ArgumentNullException | pset is null. |
il
count
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The count is not within the range of indexes of the supplied MSIL instruction array and il is not null. |
InvalidOperationException | The containing type was previously created using TypeBuilder.CreateType. -or- This method was called previously on this MethodBuilder with a non- nullil argument. |
public ParameterBuilder DefineParameter( |
position
attributes
strParamName
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The method has no parameters. -or- position is less than or equal to zero. -or- position is greater than the number of the method's parameters. |
InvalidOperationException | The containing type was previously created using TypeBuilder.CreateType |
obj
~MethodBuilder(); |
public override MethodInfo GetBaseDefinition(); |
inherit
Exception Type | Condition |
---|---|
NotSupportedException | This method is not currently supported. Retrieve the method using Type.GetMethod and call MemberInfo.GetCustomAttributes on the returned MethodInfo. |
attributeType
inherit
Exception Type | Condition |
---|---|
NotSupportedException | This method is not currently supported. Retrieve the method using Type.GetMethod and call MemberInfo.GetCustomAttributes on the returned MethodInfo. |
public override int GetHashCode(); |
public ILGenerator GetILGenerator(); |
Exception Type | Condition |
---|---|
InvalidOperationException | MethodImplAttributes indicates that the method body is not managed MSIL. |
public ILGenerator GetILGenerator( |
size
Exception Type | Condition |
---|---|
InvalidOperationException | MethodImplAttributes indicates that the method body is not managed MSIL. |
public override MethodImplAttributes GetMethodImplementationFlags(); |
public Module GetModule(); |
public override ParameterInfo[] GetParameters(); |
Exception Type | Condition |
---|---|
NotSupportedException | This method is not currently supported. Retrieve the method using Type.GetMethod and call GetParameters on the returned MethodInfo. |
public MethodToken GetToken(); |
public Type GetType(); |
obj
parameters
If the method or constructor represented by this instance takes a ByRef parameter, there is no special attribute required for that parameter in order to invoke the method or constructor using this function. If the parameters contain an uninitialized object, it is treated as System.Empty, which, with the default binder, can be widened to 0, 0.0 or String.
An argument list for the invoked method or constructor. This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked. If there are no parameters, this should be null.If the method or constructor represented by this instance takes a ByRef parameter, there is no special attribute required for that parameter in order to invoke the method or constructor using this function. If the parameters contain an uninitialized object, it is treated as System.Empty, which, with the default binder, can be widened to 0, 0.0 or String.
Exception Type | Condition |
---|---|
TargetException | The obj parameter is null and the method is not static. -or- The method is not declared or inherited by the class of obj. |
ArgumentException | The type of the parameters parameter does not match the signature of the method or constructor reflected by this instance. |
MemberAccessException | The caller does not have permission to invoke the method or constructor. |
TargetInvocationException | The invoked method or constructor throws an exception. |
TargetParameterCountException | The parameters array does not have the correct number of arguments. |
MethodAccessException | The caller does not have permission to execute the constructor. |
For example, consider a method such as MyMethod(int x, float y = 2.0). To invoke this method with only the first argument as MyMethod(4), pass one of the above binding flags and pass two arguments, namely, 4 for the first argument and Missing.Value for the second argument. Unless you use Missing.Value, you may not omit optional parameters with the Invoke method. If you must do so, use Type.InvokeMember instead.
This is a convenience method that calls the following Invoke method, passing null in the other parameters. If the invoked method throws an exception, Exception.GetBaseException returns the exception.
To invoke a static method using its MethodInfo object, the first parameter should be null, as shown in the following call:
Object myReturnValue =
myMethodInfo.Invoke(null, myParametersArray);
public override object Invoke( |
obj
invokeAttr
binder
parameters
culture
Exception Type | Condition |
---|---|
NotSupportedException | This method is not currently supported. Retrieve the method using Type.GetMethod and call Type.InvokeMember on the returned MethodInfo. |
attributeType
inherit
Exception Type | Condition |
---|---|
NotSupportedException | This method is not currently supported. Retrieve the method using Type.GetMethod and call MemberInfo.IsDefined on the returned MethodInfo. |
protected object MemberwiseClone(); |
public void SetCustomAttribute( |
customBuilder
Exception Type | Condition |
---|---|
ArgumentNullException | customBuilder is null. |
public void SetCustomAttribute( |
con
binaryAttribute
Exception Type | Condition |
---|---|
ArgumentNullException | con or binaryAttribute is null. |
public void SetImplementationFlags( |
attributes
Exception Type | Condition |
---|---|
InvalidOperationException | The containing type was previously created using TypeBuilder.CreateType. |
public void SetMarshal( |
unmanagedMarshal
Exception Type | Condition |
---|---|
InvalidOperationException | The containing type was previously created using TypeBuilder.CreateType |
name
data
Exception Type | Condition |
---|---|
InvalidOperationException | The containing type was previously created using TypeBuilder.CreateType. -or- The module that contains this method is not a debug module. |
public override string ToString(); |