Csharp/CSharp Tutorial/Reflection/Parameter

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

Analyze method parameters using reflection

using System; 
using System.Reflection; 
 
class MyClass { 
  public MyClass(int i, int j) { 
  } 
 
  public int sum() { 
    return 0; 
  } 
 
  public bool isBetween(int i) { 
    return false; 
  } 
 
  public void set(int a, int b) { 
  } 
 
  public void set(double a, double b) { 
  } 
 
  public void show() { 
    Console.WriteLine("show"); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    Type t = typeof(MyClass);
 
    Console.WriteLine("Analyzing methods in " + t.Name);     
    Console.WriteLine(); 
 
    Console.WriteLine("Methods supported: "); 
 
    MethodInfo[] mi = t.GetMethods(); 
 
    // Display methods supported by MyClass. 
    foreach(MethodInfo m in mi) { 
      // Display return type and name. 
      Console.Write("   " + m.ReturnType.Name + 
                      " " + m.Name + "("); 
 
      // Display parameters. 
      ParameterInfo[] pi = m.GetParameters(); 
 
      for(int i=0; i < pi.Length; i++) { 
        Console.Write(pi[i].ParameterType.Name + 
                      " " + pi[i].Name); 
        if(i+1 < pi.Length) Console.Write(", "); 
      } 
 
      Console.WriteLine(")"); 
     
      Console.WriteLine(); 
    } 
  } 
}
Analyzing methods in MyClass
Methods supported:
   Int32 sum()
   Boolean isBetween(Int32 i)
   Void set(Int32 a, Int32 b)
   Void set(Double a, Double b)
   Void show()
   Type GetType()
   String ToString()
   Boolean Equals(Object obj)
   Int32 GetHashCode()

Generic parameter information

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
public class MainClass
{
    public static void Main()
    {
        PrintTypeParams(typeof(List<>));
        PrintTypeParams(typeof(List<int>));
        PrintTypeParams(typeof(Nullable<>));
    }
    private static void PrintTypeParams(Type t)
    {
        Console.WriteLine(t.FullName);
        foreach (Type ty in t.GetGenericArguments())
        {
            Console.WriteLine(ty.FullName);
            Console.WriteLine(ty.IsGenericParameter);
            if (ty.IsGenericParameter)
            {
                Type[] constraints = ty.GetGenericParameterConstraints();
                foreach (Type c in constraints)
                    Console.WriteLine(c.FullName);
            }
        }
    }
}
System.Collections.Generic.List`1
True
System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicK
eyToken=b77a5c561934e089]]
System.Int32
False
System.Nullable`1
True
System.ValueType

Invoke method with parameter type int

using System; 
using System.Reflection; 
 
class MyClass { 
  public MyClass(int i) { 
    Console.WriteLine("Constructing MyClass(int). "); 
  } 
 
  public MyClass(int i, int j) { 
    Console.WriteLine("Constructing MyClass(int, int). "); 
  } 
 
  public int sum() { 
    return 0; 
  } 
 
  public bool isBetween(int i) { 
    return false; 
  } 
 
  public void set(int a, int b) { 
    Console.Write("Inside set(int, int). "); 
  } 
 
  public void set(double a, double b) { 
    Console.Write("Inside set(double, double). "); 
  } 
 
  public void show() { 
    Console.WriteLine("Values"); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    Type t = typeof(MyClass); 
    int x; 
    // Find matching constructor. 
    ConstructorInfo[] ci = t.GetConstructors(); 
 
    for(x=0; x < ci.Length; x++) { 
      ParameterInfo[] pi =  ci[x].GetParameters(); 
      if(pi.Length == 2) break; 
    } 
 
    if(x == ci.Length) { 
      Console.WriteLine("No matching constructor found."); 
      return; 
    } 
    else 
      Console.WriteLine("Two-parameter constructor found.\n"); 

    // Construct the object.   
    object[] consargs = new object[2]; 
    consargs[0] = 10; 
    consargs[1] = 20; 
    object reflectOb = ci[x].Invoke(consargs);  
  } 
}
Two-parameter constructor found.
Constructing MyClass(int, int).