Csharp/CSharp Tutorial/Reflection/Interface

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Get all implemented interface and their methods

<source lang="csharp">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;
   }
 }

}</source>

Info on Interface named: IFaceOne
Info on Interface named: IFaceTwo

Map Interface Methods To Class Methods

<source lang="csharp">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();
   }
 }

}</source>

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

Return Type Inference

<source lang="csharp">using System; using System.ruponentModel;

   delegate T MyFunc<T>();
   class MainClass
   {
       static void WriteResult<T>(MyFunc<T> function)
       {
           Console.WriteLine(function());
       }
       static void Main()
       {
           WriteResult(delegate { return 5; });
       }
   }</source>

Type: IsNestedPrivate, IsNestedPublic

<source lang="csharp">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 theMyClass = new MyClass();
   Type t = Type.GetType("MyClass");
   Console.WriteLine("Enum name? {0}", t.Name);
   Console.WriteLine("Is enum nested private? {0}", t.IsNestedPrivate);
   Console.WriteLine("Is enum nested public? {0}", t.IsNestedPublic);
 }

}</source>

Enum name? MyClass
Is enum nested private? False
Is enum nested public? False