Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Type.GetInterfaceMap Method

July 28, 2014

Returns an interface mapping for the specified interface type.

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

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.

ExceptionCondition
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 null.

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.

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.

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.


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)
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft