GetParameters () , . .
Parameterlnf[] GetParameters();
Parameter Inf . Parameterlnf , . : Name , , a ParameterType , .
, , MyClass. , , .
// .
Using System;
Using System.Reflection;
class MyClass { int x; int y;
public MyClass(int i, int j) { x = i;
= 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;
= b;
}
public void Set(double a, double b) { x = (int) a; = (int) b;
}
public void Show() {
Console.WriteLine(" x: {0}, : {1}", x, y);
}
}
class ReflectDemo { static void Main() {
Type t = typeof(MyClass); // Type,
// MyClass
Console.WriteLine(" , " +
" " + t.Name);
Console.WriteLine ();
Console.WriteLine(" : ");
MethodInfo[] mi = t.GetMethods();
// , MyClass. foreach(Methodlnfo m in mi) {
// .
Console.Write(" " + m.ReturnType.Name + " " + m.Name + "(");
// .
Parameterlnfo[] pi = m.GetParameters(); for(int i=0; i < pi.Length; i++) {
Console.Write(pi[i].ParameterType.Name + " " + pi[i].Name); if(i+l < pi.Length) Console.Write(", ");
}
Console.WriteLine(")");
Console.WriteLine();
}
}
}
.
:
Int32 Sum()
Boolean IsBetween (Int32 i)
Void Set(Int32 a, Int32 b)
Void Set (Double a, Double b)'
Void Show()
String ToString()
Boolean Equals(Object ob j)
Int32 GetHashCode()
Type GetType()
, , MyClass, , ob j ect, C# ob j ect. , .NET. , Set () , . int, double.
|
|
. , MyClass , Set ().
, MyClass, Main () .
Type t = typeof(MyClass); // ,
// MyClass
, typeof , ( MyClass).
t Reflection API , MyClass. .
MethodInfo[] mi = t.GetMethods();
foreach mi. , , .
foreach(Methodlnfo m in mi) {
// .
Console.Write(" " + m.ReturnType.Name + " " + m.Name + "(");
// .
Parameterlnfo[] 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(", ");
, , GetParameters () pi. for pin . , , MyClass.
GetMethods ()
GetMethods (), . GetMethods ().
Methodlnfo[] GetMethods(BindingFlags )
, . BindingFlags . .
DeclaredOnly
, .
Instance
NonPublic
,
Public
Static
. Instance Static Public Non Pub lie. .
BindingFlags GetMethods () , , . , , . GetMethods () .
|
|
// ,
// MyClass.
Methodlnfo[] mi = t.GetMethods(BindingFlags.DeclaredOnly |
BindingFlags.Instance |
BindingFlags.Public);