Map Interface Methods To Class Methods : Interface « Reflection « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Reflection » Interface 
19.9.2.Map Interface Methods To Class Methods
using System;
using System.Reflection;

public interface IFaceOne
{
  void MethodA();
}

public interface IFaceTwo
{
  void MethodB();
}

public class MyClass: IFaceOne, IFaceTwo
{
  public enum MyNestedEnum{}
  
  public int myIntField;
  public string myStringField;

  public void myMethod(int p1, string p2)
  {
  }

  public int MyProp
  {
    get return myIntField; }
    set myIntField = value; }
  }

  void IFaceOne.MethodA(){}
  void IFaceTwo.MethodB(){}
}

public class MainClass
{
  public static void Main(string[] args)
  {
    MyClass f = new MyClass();

    Type t = f.GetType();
    
    Type[] iFaces = t.GetInterfaces();

    for(int i = 0; i < iFaces.Length; i ++)
    {
      Console.WriteLine("Info on Interface named: {0}", iFaces[i]);

      MethodInfo[] classMethodNames = t.GetInterfaceMap(iFaces[i]).TargetMethods;
      MethodInfo[] interfaceMethodNames = t.GetInterfaceMap(iFaces[i]).InterfaceMethods;

      for(int j = 0; j < classMethodNames.Length; j++)
      {
        Console.WriteLine("Interface method: {0}",  interfaceMethodNames[j].Name);
        Console.WriteLine("is implemented by class method: {0}", classMethodNames[j].Name);
      }
      Console.WriteLine();
    }
  }
}
Info on Interface named: IFaceOne
Interface method: MethodA
is implemented by class method: IFaceOne.MethodA

Info on Interface named: IFaceTwo
Interface method: MethodB
is implemented by class method: IFaceTwo.MethodB
19.9.Interface
19.9.1.Get all implemented interface and their methods
19.9.2.Map Interface Methods To Class Methods
19.9.3.Type: IsNestedPrivate, IsNestedPublic
19.9.4.Return Type Inference
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.