CallingConventions 枚举

定义

定义方法的有效调用约定。Defines the valid calling conventions for a method.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class CallingConventions
[System.Flags]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum CallingConventions
type CallingConventions = 
Public Enum CallingConventions
继承
CallingConventions
属性

字段

Any 3

指定可以使用 Standard 调用约定或 VarArgs 调用约定。Specifies that either the Standard or the VarArgs calling convention may be used.

ExplicitThis 64

指定该签名是函数指针签名,它表示对实例或虚方法(不是静态方法)的调用。Specifies that the signature is a function-pointer signature, representing a call to an instance or virtual method (not a static method). 如果设置了 ExplicitThis,则还须设置 HasThisIf ExplicitThis is set, HasThis must also be set. 传递到被调用方法的第一个参数仍然是 this 指针,但第一个参数的类型现在未知。The first argument passed to the called method is still a this pointer, but the type of the first argument is now unknown. 因此,应将描述 this 指针的类型(或类)的标记显式存储到其元数据签名中。Therefore, a token that describes the type (or class) of the this pointer is explicitly stored into its metadata signature.

HasThis 32

指定一个实例或虚方法(不是静态方法)。Specifies an instance or virtual method (not a static method). 运行时,向被调用方法传递一个指向目标对象的指针作为此方法的第一个参数(this 指针)。At run-time, the called method is passed a pointer to the target object as its first argument (the this pointer). 存储在元数据中的签名不包括此第一个参数的类型,因为此方法是已知的,并且其所有者类能够从元数据中发现。The signature stored in metadata does not include the type of this first argument, because the method is known and its owner class can be discovered from metadata.

Standard 1

指定公共语言运行时确定的默认调用约定。Specifies the default calling convention as determined by the common language runtime. 对静态方法使用此调用约定。Use this calling convention for static methods. 对实例或虚方法使用 HasThisFor instance or virtual methods use HasThis.

VarArgs 2

指定包含变量参数的方法的调用约定。Specifies the calling convention for methods with variable arguments.

示例

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the public instance constructor that takes an integer parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, CallingConventions::HasThis, types, nullptr );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: " );
         Console::WriteLine( constructorInfoObj );
      }
      else
      {
         Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available." );
      }
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e->Message );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( "ArgumentException: {0}", e->Message );
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
using System;
using System.Reflection;
using System.Security;

public class MyClass1
{
    public MyClass1(int i){}
    public static void Main()
    {
        try
        {
            Type  myType = typeof(MyClass1);
            Type[] types = new Type[1];
            types[0] = typeof(int);
            // Get the public instance constructor that takes an integer parameter.
            ConstructorInfo constructorInfoObj = myType.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public, null,
                CallingConventions.HasThis, types, null);
            if(constructorInfoObj != null)
            {
                Console.WriteLine("The constructor of MyClass1 that is a public " +
                    "instance method and takes an integer as a parameter is: ");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of MyClass1 that is a public instance " +
                    "method and takes an integer as a parameter is not available.");
            }
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch(ArgumentException e)
        {
            Console.WriteLine("ArgumentException: " + e.Message);
        }
        catch(SecurityException e)
        {
            Console.WriteLine("SecurityException: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}
Public Class MyClass1
    Public Sub New(ByVal i As Integer)
    End Sub
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Integer)
            ' Get the public instance constructor that takes an integer parameter.
            Dim constructorInfoObj As ConstructorInfo = _
                        myType.GetConstructor(BindingFlags.Instance Or _
                        BindingFlags.Public, Nothing, _
                        CallingConventions.HasThis, types, Nothing)
            If Not (constructorInfoObj Is Nothing) Then
                Console.WriteLine("The constructor of MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is: ")
                Console.WriteLine(constructorInfoObj.ToString())
            Else
                Console.WriteLine("The constructor MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is not available.")
            End If
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException: " + e.Message)
        Catch e As ArgumentException
            Console.WriteLine("ArgumentException: " + e.Message)
        Catch e As SecurityException
            Console.WriteLine("SecurityException: " + e.Message)
        Catch e As Exception
            Console.WriteLine("Exception: " + e.Message)
        End Try
    End Sub
End Class

注解

本机调用约定是一组规则, 用于管理传递到编译的方法的参数的顺序和布局。The native calling convention is the set of rules governing the order and layout of arguments passed to compiled methods. 它还控制如何传递返回值、用于参数的寄存器, 以及调用方法是否从堆栈中移除参数。It also governs how to pass the return value, what registers to use for arguments, and whether the called or the calling method removes arguments from the stack.

适用于