共用方式為


Type.GetInterfaceMap Method

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

Returns an interface mapping for the specified interface type.

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

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public Overridable Function GetInterfaceMap ( _
    interfaceType As Type _
) As InterfaceMapping
[ComVisibleAttribute(true)]
public virtual InterfaceMapping GetInterfaceMap(
    Type interfaceType
)

Parameters

  • interfaceType
    Type: System.Type
    The Type of the interface of which to retrieve a mapping.

Return Value

Type: System.Reflection.InterfaceMapping
An InterfaceMapping object representing the interface mapping for interfaceType.

Exceptions

Exception Condition
ArgumentException

The interfaceType parameter does not refer to an interface.

-or-

interfaceType is a generic interface, and the current type is an array type.

ArgumentNullException

interfaceType is nulla null reference (Nothing in Visual Basic).

InvalidOperationException

The current Type represents a generic type parameter; that is, IsGenericParameter is true.

NotSupportedException

The invoked method is not supported in the base class. Derived classes must provide an implementation.

Remarks

The interface map denotes how an interface is mapped into the actual methods on a class that implements that interface.

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.

Examples

The following example uses the GetInterfaceMap method to show the methods of the generic Dictionary<TKey, TValue> class that implement the methods of the generic IDictionary<TKey, TValue> interface.

Imports System.Reflection
Imports System.Collections.Generic

Class Example

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

        Dim obj As New Dictionary(Of Integer, String)

        If obj.GetType().GetInterface("IDictionary`2", False) IsNot Nothing Then

            ' Get the mapping between the interface methods of the 
            ' generic IDictionary interface and the Dictionary methods
            ' that implement them.
            Dim idictType As Type = GetType(IDictionary(Of Integer, String))
            Dim map As InterfaceMapping = _
                obj.GetType().GetInterfaceMap(idictType)

            Dim interfaceMethods() As MethodInfo = map.InterfaceMethods
            Dim implementationMethods() As MethodInfo = map.TargetMethods

            For i As Integer = 0 To interfaceMethods.Length - 1
                outputBlock.Text &= interfaceMethods(i).ToString() & _
                    " is implemented by:" & vbLf & "       " & _
                    implementationMethods(i).ToString() & vbLf
            Next
        End If
    End Sub 
End Class 

' This example produces the following output:
'
'System.String get_Item(Int32) is implemented by:
'       System.String get_Item(Int32)
'System.String set_Item(Int32, System.String) is implemented by:
'       Void set_Item(Int32, System.String)
'System.Collections.Generic.ICollection`1[System.Int32] get_Keys() is implemented by:
'       System.Collections.Generic.ICollection`1[System.Int32] System.Collections.Generic.IDictionary<TKey,TValue>.get_Keys()
'System.Collections.Generic.ICollection`1[System.String] get_Values() is implemented by:
'       System.Collections.Generic.ICollection`1[System.String] System.Collections.Generic.IDictionary<TKey,TValue>.get_Values()
'Boolean ContainsKey(Int32) is implemented by:
'       Boolean ContainsKey(Int32)
'Void Add(Int32, System.String) is implemented by:
'       Void Add(Int32, System.String)
'Boolean Remove(Int32) is implemented by:
'       Boolean Remove(Int32)
'Boolean TryGetValue(Int32, System.String ByRef) is implemented by:
'       Boolean TryGetValue(Int32, System.String ByRef)
using System;
using System.Reflection;
using System.Collections.Generic;

class Example
{

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

        Dictionary<int, string> obj = new Dictionary<int, string>();

        // Get the mapping between the interface methods of the 
        // generic IDictionary interface and the Dictionary methods
        // that implement them.
        if (obj.GetType().GetInterface("IDictionary`2", false) != null)
        {
            Type idictType = typeof(IDictionary<int, string>);
            InterfaceMapping map = obj.GetType().GetInterfaceMap(idictType);

            MethodInfo[] interfaceMethods = map.InterfaceMethods;
            MethodInfo[] implementationMethods = map.TargetMethods;

            for(int i=0; i<interfaceMethods.Length; i++)
            {
                outputBlock.Text += interfaceMethods[i].ToString() + 
                    " is implemented by:\n       " + 
                    implementationMethods[i].ToString() + "\n";
            }
        }
    }
}

/* This example produces the following output:

System.String get_Item(Int32) is implemented by:
       System.String get_Item(Int32)
System.String set_Item(Int32, System.String) is implemented by:
       Void set_Item(Int32, System.String)
System.Collections.Generic.ICollection`1[System.Int32] get_Keys() is implemented by:
       System.Collections.Generic.ICollection`1[System.Int32] System.Collections.Generic.IDictionary<TKey,TValue>.get_Keys()
System.Collections.Generic.ICollection`1[System.String] get_Values() is implemented by:
       System.Collections.Generic.ICollection`1[System.String] System.Collections.Generic.IDictionary<TKey,TValue>.get_Values()
Boolean ContainsKey(Int32) is implemented by:
       Boolean ContainsKey(Int32)
Void Add(Int32, System.String) is implemented by:
       Void Add(Int32, System.String)
Boolean Remove(Int32) is implemented by:
       Boolean Remove(Int32)
Boolean TryGetValue(Int32, System.String ByRef) is implemented by:
       Boolean TryGetValue(Int32, System.String 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.