Export (0) Print
Expand All

Type.GetInterface Method (String, Boolean)

When overridden in a derived class, searches for the specified interface, specifying whether to do a case-insensitive search for the interface name.

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

public abstract Type GetInterface (
	string name,
	bool ignoreCase
)
public abstract Type GetInterface (
	String name, 
	boolean ignoreCase
)
public abstract function GetInterface (
	name : String, 
	ignoreCase : boolean
) : Type
Not applicable.

Parameters

name

The String containing the name of the interface to get. For generic interfaces, this is the mangled name.

ignoreCase

true to ignore the case of that part of name that specifies the simple interface name (the part that specifies the namespace must be correctly cased).

-or-

false to perform a case-sensitive search for all parts of name.

Return Value

A Type object representing the interface with the specified name, implemented or inherited by the current Type, if found; otherwise, a null reference (Nothing in Visual Basic).

Exception typeCondition

ArgumentNullException

name is a null reference (Nothing in Visual Basic).

AmbiguousMatchException

The current Type represents a type that implements the same generic interface with different type arguments.

The ignoreCase parameter applies only to the simple interface name, not to the namespace. The portion of name that specifies the namespace must have the correct case, or the interface will not be found. For example, the string "System.icomparable" finds the IComparable interface, but the string "system.icomparable" does not.

If the current Type represents a constructed generic type, this method returns the Type with the type parameters replaced by the appropriate type arguments.

If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the interface constraints and any interfaces inherited from class or interface constraints.

NoteNote:

For generic interfaces, the name parameter is the mangled name, ending with a grave accent (`) and the number of type parameters. This is true for both generic interface definitions and constructed generic interfaces. For example, to find IExample<T> (IExample(Of T) in Visual Basic) or IExample<string> (IExample(Of String) in Visual Basic), search for "IExample`1".

The following code example uses the GetInterface(String,Boolean) method to perform a case-insensitive search of the Hashtable class for the IEnumerable interface.

The code example also demonstrates the GetInterface(String) method overload and the GetInterfaceMap method.

public static void Main()
{
    Hashtable hashtableObj = new Hashtable();
    Type objType = hashtableObj.GetType();
    MemberInfo[] arrayMemberInfo;
    MethodInfo[] arrayMethodInfo;
    try
    {   
        // Get the methods implemented in 'IDeserializationCallback' interface.
        arrayMethodInfo =objType.GetInterface("IDeserializationCallback").GetMethods();
        Console.WriteLine ("\nMethods of 'IDeserializationCallback' Interface :");
        for(int index=0;index < arrayMethodInfo.Length ;index++)
            Console.WriteLine (arrayMethodInfo[index].ToString() ); 

        // Get FullName for interface by using Ignore case search.
        Console.WriteLine ("\nMethods of 'IEnumerable' Interface");
        arrayMethodInfo = objType.GetInterface("ienumerable",true).GetMethods();
        for(int index=0;index < arrayMethodInfo.Length ;index++)
           Console.WriteLine (arrayMethodInfo[index].ToString()); 
       
        //Get the Interface methods for 'IDictionary' interface
        InterfaceMapping interfaceMappingObj;
        interfaceMappingObj = objType.GetInterfaceMap(typeof(IDictionary));
        arrayMemberInfo = interfaceMappingObj.InterfaceMethods;
        Console.WriteLine ("\nHashtable class Implements the following IDictionary Interface methods :");
        for(int index=0; index < arrayMemberInfo.Length; index++)
            Console.WriteLine (arrayMemberInfo[index].ToString() ); 
    }
    catch (Exception e)
    {
        Console.WriteLine ("Exception : " + e.ToString());            
    }                 
}

public static void main(String[] args)
{
    Hashtable hashtableObj = new Hashtable();
    Type objType = hashtableObj.GetType();
    MemberInfo arrayMemberInfo[];
    MethodInfo arrayMethodInfo[];
    try {
        // Get the methods implemented in 'IDeserializationCallback'
        // interface.
        arrayMethodInfo = objType.GetInterface("IDeserializationCallback").
            GetMethods();
        Console.WriteLine("\nMethods of 'IDeserializationCallback'"
            + " Interface :");
        for (int index = 0; index < arrayMethodInfo.length; index++) {
            Console.WriteLine(arrayMethodInfo.get_Item(index).ToString());
        }
        // Get FullName for interface by using Ignore case search.
        Console.WriteLine("\nMethods of 'IEnumerable' Interface");
        arrayMethodInfo = objType.GetInterface("ienumerable", true).
            GetMethods();
        for (int index = 0; index < arrayMethodInfo.length; index++) {
            Console.WriteLine(arrayMethodInfo.get_Item(index).ToString());
        }
        //Get the Interface methods for 'IDictionary' interface
        InterfaceMapping interfaceMappingObj;
        interfaceMappingObj =
            objType.GetInterfaceMap(IDictionary.class.ToType());
        arrayMemberInfo = interfaceMappingObj.InterfaceMethods;
        Console.WriteLine("\nHashtable class Implements the following"
            + " IDictionary Interface methods :");
        for (int index = 0; index < arrayMemberInfo.length; index++) {
            Console.WriteLine(arrayMemberInfo.get_Item(index).ToString());
        }
    }
    catch (System.Exception e) {
        Console.WriteLine("Exception : " + e.ToString());
    }
} //main

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft