다음을 통해 공유


CallingConventions Enumeration

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines the valid calling conventions for a method.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<FlagsAttribute> _
Public Enumeration CallingConventions
[ComVisibleAttribute(true)]
[FlagsAttribute]
public enum CallingConventions

Members

Member name Description
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Standard Specifies the default calling convention as determined by the common language runtime. Use this calling convention for static methods. For instance or virtual methods use HasThis.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 VarArgs Specifies the calling convention for methods with variable arguments.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Any Specifies that either the Standard or the VarArgs calling convention may be used.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 HasThis Specifies an instance or virtual method (not a static method). 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.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 ExplicitThis Specifies that the signature is a function-pointer signature, representing a call to an instance or virtual method (not a static method). If ExplicitThis is set, HasThis must also be set. The first argument passed to the called method is still a this pointer, but the type of the first argument is now unknown. Therefore, a token that describes the type (or class) of the this pointer is explicitly stored into its metadata signature.

Remarks

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.

Examples

The following example finds specific overloads of MethodA, specifying binding constraints, calling conventions, and a variety of argument types.


Imports System.Reflection
Imports System.Runtime.InteropServices

Class Example

   ' Methods to get:

   Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

   End Sub

   Public Overloads Sub MethodA(ByVal i() As Integer)

   End Sub

   Public Overloads Sub MethodA(ByRef r As Integer)

   End Sub

   ' Method that takes an integer and an out parameter. Note that an
   ' Imports reference is needed to System.Runtime.InteropServices
   ' for the <OutAttribute>, which can be shortened to <Out>.
   Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
      o = 100
   End Sub

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim mInfo As MethodInfo

      ' Get MethodA(ByVal i As Integer, ByVal l As Long)
      mInfo = GetType(Example).GetMethod("MethodA", _
          BindingFlags.Public Or BindingFlags.Instance, _
          Nothing, _
          CallingConventions.Any, _
          New Type() {GetType(System.Int32), _
          GetType(System.Int64)}, _
          Nothing)
      outputBlock.Text += String.Format("Found method: {0}", mInfo) & vbCrLf

      ' Get  MethodA(ByVal i() As Integer)
      mInfo = GetType(Example).GetMethod("MethodA", _
          BindingFlags.Public Or BindingFlags.Instance, _
          Nothing, _
          CallingConventions.Any, _
          New Type() {GetType(System.Int32())}, _
          Nothing)
      outputBlock.Text += String.Format("Found method: {0}", mInfo) & vbCrLf

      ' Get MethodA(ByRef r As Integer)
      mInfo = GetType(Example).GetMethod("MethodA", _
      BindingFlags.Public Or BindingFlags.Instance, _
      Nothing, _
      CallingConventions.Any, _
      New Type() {GetType(System.Int32).MakeByRefType}, _
      Nothing)
      outputBlock.Text += String.Format("Found method: {0}", mInfo) & vbCrLf

      ' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
      mInfo = GetType(Example).GetMethod("MethodA", _
      BindingFlags.Public Or BindingFlags.Instance, _
      Nothing, _
      CallingConventions.Any, _
      New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
      Nothing)
      outputBlock.Text += String.Format("Found method: {0}", mInfo) & vbCrLf

   End Sub
End Class

' This method produces the following output:
'
'Found method: Void MethodA(Int32, Int32)
'Found method: Void MethodA(Int32[])
'Found method: Void MethodA(Int32 ByRef)
'Found method: Void MethodA(Int32, Int32 ByRef)

using System;
using System.Reflection;

class Example
{
   // Methods to get:

   public void MethodA(int i, int j) { }

   public void MethodA(int[] i) { }

   public void MethodA(ref int r) { }

   // Method that takes an out parameter:
   public void MethodA(int i, out int o) { o = 100; }


   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      MethodInfo mInfo;

      // Get MethodA(int i, int j)
      mInfo = typeof(Example).GetMethod("MethodA",
          BindingFlags.Public | BindingFlags.Instance,
          null,
          CallingConventions.Any,
          new Type[] { typeof(int), typeof(int) },
          null);
      outputBlock.Text += String.Format("Found method: {0}", mInfo) + "\n";

      // Get MethodA(int[] i)
      mInfo = typeof(Example).GetMethod("MethodA",
          BindingFlags.Public | BindingFlags.Instance,
          null,
          CallingConventions.Any,
          new Type[] { typeof(int[]) },
          null);
      outputBlock.Text += String.Format("Found method: {0}", mInfo) + "\n";

      // Get MethodA(ref int r)
      mInfo = typeof(Example).GetMethod("MethodA",
          BindingFlags.Public | BindingFlags.Instance,
          null,
          CallingConventions.Any,
          new Type[] { typeof(int).MakeByRefType() },
          null);
      outputBlock.Text += String.Format("Found method: {0}", mInfo) + "\n";

      // Get MethodA(int i, out int o)
      mInfo = typeof(Example).GetMethod("MethodA",
          BindingFlags.Public | BindingFlags.Instance,
          null,
          CallingConventions.Any,
          new Type[] { typeof(int), typeof(int).MakeByRefType() },
          null);
      outputBlock.Text += String.Format("Found method: {0}", mInfo) + "\n";

   }
}

/* This method produces the following output:

Found method: Void MethodA(Int32, Int32)
Found method: Void MethodA(Int32[])
Found method: Void MethodA(Int32 ByRef)
Found method: Void MethodA(Int32, Int32 ByRef)
  */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference