-
Антон773 (22.11.05 16:08) [0]Здравствуйте! Подкиньте пожалуйста примерчик применения рефлексии.Желательно попроще .
-
Lamer@fools.ua © (22.11.05 22:19) [1]using System;
using System.Reflection;
using System.Reflection.Emit;
// Create a class having two public methods and one protected method.
public class MyTypeClass
{
public void MyMethods()
{
}
public int MyMethods1()
{
return 3;
}
protected String MyMethods2()
{
return "hello";
}
}
public class TypeMain
{
public static void Main()
{
Type myType =(typeof(MyTypeClass));
// Get the public methods.
MethodInfo[] myArrayMethodInfo = myType.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly);
Console.WriteLine("\nThe number of public methods is {0}.", myArrayMethodInfo.Length);
// Display all the methods.
DisplayMethodInfo(myArrayMethodInfo);
// Get the nonpublic methods.
MethodInfo[] myArrayMethodInfo1 = myType.GetMethods(BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.DeclaredOnly);
Console.WriteLine("\nThe number of protected methods is {0}.", myArrayMethodInfo1.Length);
// Display information for all methods.
DisplayMethodInfo(myArrayMethodInfo1);
}
public static void DisplayMethodInfo(MethodInfo[] myArrayMethodInfo)
{
// Display information for all methods.
for(int i=0;i<myArrayMethodInfo.Length;i++)
{
MethodInfo myMethodInfo = (MethodInfo)myArrayMethodInfo[i];
Console.WriteLine("\nThe name of the method is {0}.", myMethodInfo.Name);
}
}
} -
Антон773 (30.11.05 17:20) [2]Спасибо! А еще можно примерчик.
-
DiamondShark © (30.11.05 18:55) [3]В справке в описании каждого класса и метода есть примерчик.
-
VoidX (06.12.05 16:23) [4]procedure TWinForm3.Button1_Click(sender: System.Object; e: System.EventArgs)
var
Ass : Assembly;
BFlag : BindingFlags;
WF : System.Windows.Forms.Form;
obj : System.Object;
t: System.Type;
begin
BFlag := BindingFlags.DeclaredOnly or BindingFlags.Public or
BindingFlags.Instance or BindingFlags.InvokeMethod or BindingFlags.NonPublic;
Ass := Assembly.LoadFrom('DocShow.dll');
t := Ass.GetType('WinForm9.TWinForm9');
WF := System.Windows.Forms.Form (t.InvokeMember('.ctor',BindingFlags.CreateInstance,nil,nil,nil));
WF.Show;
end;