Type.GetInterfaceMap(Type) Type.GetInterfaceMap(Type) Type.GetInterfaceMap(Type) Type.GetInterfaceMap(Type) Method

定義

傳回指定介面類型的介面對應。Returns an interface mapping for the specified interface type.

public:
 virtual System::Reflection::InterfaceMapping GetInterfaceMap(Type ^ interfaceType);
[System.Runtime.InteropServices.ComVisible(true)]
public virtual System.Reflection.InterfaceMapping GetInterfaceMap (Type interfaceType);
abstract member GetInterfaceMap : Type -> System.Reflection.InterfaceMapping
override this.GetInterfaceMap : Type -> System.Reflection.InterfaceMapping
Public Overridable Function GetInterfaceMap (interfaceType As Type) As InterfaceMapping

參數

interfaceType
Type Type Type Type

要擷取對應的介面類型。The interface type to retrieve a mapping for.

傳回

物件,表示 interfaceType 的介面對應。An object that represents the interface mapping for interfaceType.

實作

例外狀況

interfaceType 不會由目前類型實作。interfaceType is not implemented by the current type.

-或--or- interfaceType 引數未參考介面。The interfaceType argument does not refer to an interface.

-或--or-

目前的執行個體或 interfaceType 引數是開放式泛型型別,也就是 ContainsGenericParameters 屬性會傳回 trueThe current instance or interfaceType argument is an open generic type; that is, the ContainsGenericParameters property returns true.

-或--or-

interfaceType 是泛型介面,且目前的類型是陣列類型。interfaceType is a generic interface, and the current type is an array type.

目前 Type 代表泛型型別參數;也就是說, IsGenericParametertrueThe current Type represents a generic type parameter; that is, IsGenericParameter is true.

基底類別不支援叫用的方法。The invoked method is not supported in the base class. 衍生類別必須提供實作。Derived classes must provide an implementation.

範例

下列範例會呼叫GetInterfaceMap方法, 以判斷IFormatProvider介面如何對應至CultureInfo方法, 以及IAppDomainSetup介面如何對應至AppDomainSetup屬性。The following example calls the GetInterfaceMap method to determine how the IFormatProvider interface maps to CultureInfo methods, and how the IAppDomainSetup interface maps to AppDomainSetup properties. 請注意, 因為IAppDomainSetup介面會定義一組屬性, 所以傳回InterfaceMapping的物件會包含MethodInfo屬性之 get 和 set 存取子的個別物件。Note that, because the IAppDomainSetup interface defines a set of properties, the returned InterfaceMapping object includes separate MethodInfo objects for a property's get and set accessors.

using System;
using System.Globalization;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      Type[] interf = { typeof(IFormatProvider), typeof(IAppDomainSetup) };
      Type[] impl = { typeof(CultureInfo), typeof(AppDomainSetup) };

      for (int ctr = 0; ctr < interf.Length; ctr++)
         ShowInterfaceMapping(interf[ctr], impl[ctr]);
   }

   private static void ShowInterfaceMapping(Type intType, Type implType)
   {
      InterfaceMapping map = implType.GetInterfaceMap(intType);
      Console.WriteLine("Mapping of {0} to {1}: ", map.InterfaceType, map.TargetType);
      for (int ctr = 0; ctr < map.InterfaceMethods.Length; ctr++) {
         MethodInfo im = map.InterfaceMethods[ctr];
         MethodInfo tm = map.TargetMethods[ctr];
         Console.WriteLine("   {0} --> {1}", im.Name,tm.Name);
      }
      Console.WriteLine();
   }
}
// The example displays the following output:
//    Mapping of System.IFormatProvider to System.Globalization.CultureInfo:
//       GetFormat --> GetFormat
//
//    Mapping of System.IAppDomainSetup to System.AppDomainSetup:
//       get_ApplicationBase --> get_ApplicationBase
//       set_ApplicationBase --> set_ApplicationBase
//       get_ApplicationName --> get_ApplicationName
//       set_ApplicationName --> set_ApplicationName
//       get_CachePath --> get_CachePath
//       set_CachePath --> set_CachePath
//       get_ConfigurationFile --> get_ConfigurationFile
//       set_ConfigurationFile --> set_ConfigurationFile
//       get_DynamicBase --> get_DynamicBase
//       set_DynamicBase --> set_DynamicBase
//       get_LicenseFile --> get_LicenseFile
//       set_LicenseFile --> set_LicenseFile
//       get_PrivateBinPath --> get_PrivateBinPath
//       set_PrivateBinPath --> set_PrivateBinPath
//       get_PrivateBinPathProbe --> get_PrivateBinPathProbe
//       set_PrivateBinPathProbe --> set_PrivateBinPathProbe
//       get_ShadowCopyDirectories --> get_ShadowCopyDirectories
//       set_ShadowCopyDirectories --> set_ShadowCopyDirectories
//       get_ShadowCopyFiles --> get_ShadowCopyFiles
//       set_ShadowCopyFiles --> set_ShadowCopyFiles
Imports System.Globalization
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim int() As Type = { GetType(IFormatProvider), GetType(IAppDomainSetup) }
      Dim impl() As Type = { GetType(CultureInfo), GetType(AppDomainSetup) }
      
      For ctr As Integer = 0 To int.Length - 1
         ShowInterfaceMapping(int(ctr), impl(ctr))
      Next
   End Sub
   
   Private Sub ShowInterfaceMapping(intType As Type, implType As Type)
      Dim map As InterfaceMapping = implType.GetInterfaceMap(intType)
      Console.WriteLine("Mapping of {0} to {1}: ", map.InterfaceType, map.TargetType)
      For ctr As Integer = 0 To map.InterfaceMethods.Length - 1
         Dim im As MethodInfo = map.InterfaceMethods(ctr)
         Dim tm As MethodInfo = map.TargetMethods(ctr)
         Console.WriteLine("   {0} --> {1}", im.Name,tm.Name)
      Next
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'    Mapping of System.IFormatProvider to System.Globalization.CultureInfo:
'       GetFormat --> GetFormat
'
'    Mapping of System.IAppDomainSetup to System.AppDomainSetup:
'       get_ApplicationBase --> get_ApplicationBase
'       set_ApplicationBase --> set_ApplicationBase
'       get_ApplicationName --> get_ApplicationName
'       set_ApplicationName --> set_ApplicationName
'       get_CachePath --> get_CachePath
'       set_CachePath --> set_CachePath
'       get_ConfigurationFile --> get_ConfigurationFile
'       set_ConfigurationFile --> set_ConfigurationFile
'       get_DynamicBase --> get_DynamicBase
'       set_DynamicBase --> set_DynamicBase
'       get_LicenseFile --> get_LicenseFile
'       set_LicenseFile --> set_LicenseFile
'       get_PrivateBinPath --> get_PrivateBinPath
'       set_PrivateBinPath --> set_PrivateBinPath
'       get_PrivateBinPathProbe --> get_PrivateBinPathProbe
'       set_PrivateBinPathProbe --> set_PrivateBinPathProbe
'       get_ShadowCopyDirectories --> get_ShadowCopyDirectories
'       set_ShadowCopyDirectories --> set_ShadowCopyDirectories
'       get_ShadowCopyFiles --> get_ShadowCopyFiles
'       set_ShadowCopyFiles --> set_ShadowCopyFiles

備註

介面對應表示介面如何對應至實作為介面之類別上的實際成員。The interface map denotes how an interface is mapped into the actual members on a class that implements that interface.

如果目前Type的表示結構化的泛型型別, 則類型參數會由這個方法所傳回之的InterfaceMapping元素中的適當類型引數所取代。If the current Type represents a constructed generic type, type parameters are replaced by the appropriate type arguments in the elements of the InterfaceMapping returned by this method.

適用於

另請參閱