Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type.GetInterfaceMap Method

Returns an interface mapping for the specified interface type.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[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.
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)
 */


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

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.