Type.GetInterfaceMap(Type) 方法

定义

返回指定接口类型的接口映射。

C#
public virtual System.Reflection.InterfaceMapping GetInterfaceMap(Type interfaceType);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public virtual System.Reflection.InterfaceMapping GetInterfaceMap(Type interfaceType);

参数

interfaceType
Type

要检索其映射的接口类型。

返回

表示 interfaceType 的接口映射的对象。

实现

属性

例外

当前类型未实现 interfaceType

- 或 -

interfaceType 参数未引用接口。

- 或 -

当前实例或 interfaceType 参数是开放式泛型类型;即,ContainsGenericParameters 属性将返回 true

- 或 -

interfaceType 是一个泛型接口,而当前类型是一个数组类型。

interfaceTypenull

当前 Type 表示泛型类型参数;即, IsGenericParametertrue

基类不支持调用的方法。 派生类必须提供一个实现。

示例

以下示例调用 GetInterfaceMap 方法以确定接口如何 IFormatProvider 映射到 CultureInfo 方法,以及接口如何 IAppDomainSetup 映射到 AppDomainSetup 属性。 请注意,由于 IAppDomainSetup 接口定义了一组属性,因此返回 InterfaceMapping 的对象包含属性的 get 和 set 访问器的独立 MethodInfo 对象。

C#
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

注解

接口映射表示如何将接口映射到实现该接口的类上的实际成员。

如果当前 Type 表示构造的泛型类型,则类型参数将替换为此方法返回的 元素 InterfaceMapping 中的相应类型参数。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另请参阅