Csharp/C Sharp/Reflection/MethodInfo — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:38, 26 мая 2010
Содержание
- 1 Analyze methods using reflection
- 2 dump the public methods of a class
- 3 dynamic invocation is demonstrated with MethodInfo.Invoke and Type.InvokeMember
- 4 Finding the class that contains a method in an assembly.
- 5 Invoke methods using reflection
- 6 List Assembly Info
- 7 MethodInfo: Name
- 8 Type.GetMethods
Analyze methods using reflection
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
// Analyze methods using reflection.
using System; using System.Reflection;
class MyClass {
int x; int y; public MyClass(int i, int j) { x = i; y = j; } public int sum() { return x+y; } public bool isBetween(int i) { if(x < i && i < y) return true; else return false; } public void set(int a, int b) { x = a; y = b; } public void set(double a, double b) { x = (int) a; y = (int) b; } public void show() { Console.WriteLine(" x: {0}, y: {1}", x, y); }
}
public class ReflectDemo {
public static void Main() { Type t = typeof(MyClass); // get a Type object representing 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(); } }
}
</source>
dump the public methods of a class
<source lang="csharp">
using System; using System.Reflection; class DumpType {
static void Main(string[] argv) { targetType = LoadAssembly(argv[0], argv[1]); DumpReportHeader(); DumpMethods(); } static public Type LoadAssembly(string t, string a) { return Type.ReflectionOnlyGetType(t + "," + a, false, true); } static void DumpReportHeader() { Console.WriteLine("\n{0} type of {1} assembly",targetType.Name, targetType.Assembly.GetName().Name); Console.WriteLine("\n{0,22}\n", "[ METHODS ]"); } static void DumpMethods() { string dashes = new string("-", 50); foreach (MethodInfo method in targetType.GetMethods()) { Console.WriteLine("{0,12}{1,-12}", " ", method.Name + " " + "<" + method.ReturnParameter.ParameterType.Name + ">"); int count = 1; foreach (ParameterInfo parameter in method.GetParameters()) { Console.WriteLine("{0, 35}{1, -12}"," ", (count++).ToString() + " " + parameter.Name + " (" + parameter.ParameterType.Name + ")"); } Console.WriteLine("{0,12}{1}", " ", dashes); } } private static Type targetType;
}
</source>
dynamic invocation is demonstrated with MethodInfo.Invoke and Type.InvokeMember
<source lang="csharp"> using System; using System.Reflection;
class Starter {
static void Main() { MyClass obj = new MyClass(); Type tObj = obj.GetType(); MethodInfo method = tObj.GetMethod("MethodA"); method.Invoke(obj, null); tObj.InvokeMember("MethodA", BindingFlags.InvokeMethod, null, obj, null); }
} class MyClass {
public void MethodA() { Console.WriteLine("MyClass.Method invoked"); }
}
</source>
Finding the class that contains a method in an assembly.
<source lang="csharp">
using System; using System.Reflection; using System.Collections; class MainClass {
public static void Main(string[] args) { SearchForMethod(args[0], args[1]); } public static void SearchForMethod(string AssemblyName,string MethodName) { Assembly assembly = Assembly.LoadFrom(AssemblyName); if (assembly == null) { Console.WriteLine(AssemblyName); return; } foreach (Type t in assembly.GetTypes()) { if (t.IsClass == false) continue; foreach (MethodInfo m in t.GetMethods()) { if (m.Name == MethodName) { Console.WriteLine("Class {0} contains method", t.FullName); } } } }
}
</source>
Invoke methods using reflection
<source lang="csharp"> /* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852
- /
// Invoke methods using reflection.
using System; using System.Reflection;
class MyClass {
int x; int y; public MyClass(int i, int j) { x = i; y = j; } public int sum() { return x+y; } public bool isBetween(int i) { if((x < i) && (i < y)) return true; else return false; } public void set(int a, int b) { Console.Write("Inside set(int, int). "); x = a; y = b; show(); } // Overload set. public void set(double a, double b) { Console.Write("Inside set(double, double). "); x = (int) a; y = (int) b; show(); } public void show() { Console.WriteLine("Values are x: {0}, y: {1}", x, y); }
}
public class InvokeMethDemo {
public static void Main() { Type t = typeof(MyClass); MyClass reflectOb = new MyClass(10, 20); int val; Console.WriteLine("Invoking methods in " + t.Name); Console.WriteLine(); MethodInfo[] mi = t.GetMethods(); // Invoke each method. foreach(MethodInfo m in mi) { // Get the parameters ParameterInfo[] pi = m.GetParameters(); if(m.Name.rupareTo("set")==0 && pi[0].ParameterType == typeof(int)) { object[] args = new object[2]; args[0] = 9; args[1] = 18; m.Invoke(reflectOb, args); } else if(m.Name.rupareTo("set")==0 && pi[0].ParameterType == typeof(double)) { object[] args = new object[2]; args[0] = 1.12; args[1] = 23.4; m.Invoke(reflectOb, args); } else if(m.Name.rupareTo("sum")==0) { val = (int) m.Invoke(reflectOb, null); Console.WriteLine("sum is " + val); } else if(m.Name.rupareTo("isBetween")==0) { object[] args = new object[1]; args[0] = 14; if((bool) m.Invoke(reflectOb, args)) Console.WriteLine("14 is between x and y"); } else if(m.Name.rupareTo("show")==0) { m.Invoke(reflectOb, null); } } }
}
</source>
List Assembly Info
<source lang="csharp"> using System; using System.Collections; using System.Reflection; public class MainClass{
public static void Main(){ Assembly LoadedAsm = Assembly.LoadFrom("System.String"); Console.WriteLine(LoadedAsm.FullName); if (LoadedAsm.GlobalAssemblyCache == true) { Console.WriteLine("\tThis is a Global Assembly"); } else { Console.WriteLine("\tThis is a Local Assembly"); } MethodInfo entry = LoadedAsm.EntryPoint; if (entry != null) { Console.WriteLine("\tAssembly EntryPoint:\t{0}", entry.Name); } else { Console.WriteLine("\tThis Assembly has no Entry point."); } object[] attrs = LoadedAsm.GetCustomAttributes(true); if (attrs.Length == 0) { Console.WriteLine("\tNo Custom Attributes Found"); } else { Console.WriteLine("\tFound Attributes"); foreach (object o in attrs) { Console.WriteLine("\t\t{0}", o.ToString()); } } }
}
</source>
MethodInfo: Name
<source lang="csharp"> using System; using System.Collections; using System.Reflection;
public class Starter {
public static void Main() { Object obj = new Object(); Type t = obj.GetType(); foreach (MethodInfo m in t.GetMethods()) { Console.WriteLine(m.Name); } }
}
</source>
Type.GetMethods
<source lang="csharp"> using System; using System.Reflection; public class Class1 {
public static int Main() { Type t = typeof(MyClass); Console.WriteLine("Type of class: " + t); Console.WriteLine("Namespace: " + t.Namespace); MethodInfo[] mi = t.GetMethods(); Console.WriteLine("Methods are:"); foreach (MethodInfo i in mi) { Console.WriteLine("Name: " + i.Name); ParameterInfo[] pif = i.GetParameters(); foreach (ParameterInfo p in pif) { Console.WriteLine("Type: " + p.ParameterType + " parameter name: " + p.Name); } } return 0; }
public class MyClass { public int pubInteger; private int _privValue; public MyClass() { } public MyClass(int IntegerValueIn) { pubInteger = IntegerValueIn; } public int Add10(int IntegerValueIn) { Console.WriteLine(IntegerValueIn); return IntegerValueIn + 10; } public int TestProperty { get { return _privValue; } set { _privValue = value; } } }
}
</source>