反射技术实例
我们可以在程序运行时动态实例化对象,获得对象的属性,并调用对象的方法
运行结果:
列出程序集所有类型
Class1
HelloWorld
列出HelloWorld中所有的方法
get_Name
SayHello
GetType
ToString
Equals
GetHashCode
实例化HelloWorld,并调用SayHello方法
Hello World!
Hello,ZhenLei
http://hi.baidu.com/zhiwei%5F117/blog/item/40182d95812e0514d31b707b.html
+展开
-C#
namespace ReflectionExample
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("列出程序集所有类型");
Assembly a = Assembly.LoadFrom("ConsoleApplication1.exe");
Type[] mytypes = a.GetTypes();//获取程序集所定义的类型
foreach (Type t in mytypes)
{
Console.WriteLine(t.Name);//列出当前类型成员的名字
}
Console.ReadLine();
Console.WriteLine("列出HelloWorld中所有的方法");
Type ht=typeof(HelloWorld);
MethodInfo []mif = ht.GetMethods();
foreach (MethodInfo mf in mif)
{
Console.WriteLine(mf.Name);//列出当前方法成员的名字
}
Console.ReadLine();
Console.WriteLine("实例化HelloWorld,并调用SayHello方法");
Object obj = Activator.CreateInstance(ht);
string[] s ={ "ZhenLei" };
Object objname = Activator.CreateInstance(ht, s);
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
MethodInfo msayhello = ht.GetMethod("SayHello");
msayhello.Invoke(obj,null);
msayhello.Invoke(objname, null);
Console.ReadLine();
}
}
public class HelloWorld
{
string myname = null;
public HelloWorld(string name)
{
myname = name;
}
public HelloWorld()
: this(null)
{
}
public string Name
{
get { return myname; }
}
public void SayHello()
{
if (myname == null)
{
Console.WriteLine("Hello World!");
}
else
{
Console.WriteLine("Hello,"+myname);
}
}
}
}
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("列出程序集所有类型");
Assembly a = Assembly.LoadFrom("ConsoleApplication1.exe");
Type[] mytypes = a.GetTypes();//获取程序集所定义的类型
foreach (Type t in mytypes)
{
Console.WriteLine(t.Name);//列出当前类型成员的名字
}
Console.ReadLine();
Console.WriteLine("列出HelloWorld中所有的方法");
Type ht=typeof(HelloWorld);
MethodInfo []mif = ht.GetMethods();
foreach (MethodInfo mf in mif)
{
Console.WriteLine(mf.Name);//列出当前方法成员的名字
}
Console.ReadLine();
Console.WriteLine("实例化HelloWorld,并调用SayHello方法");
Object obj = Activator.CreateInstance(ht);
string[] s ={ "ZhenLei" };
Object objname = Activator.CreateInstance(ht, s);
BindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
MethodInfo msayhello = ht.GetMethod("SayHello");
msayhello.Invoke(obj,null);
msayhello.Invoke(objname, null);
Console.ReadLine();
}
}
public class HelloWorld
{
string myname = null;
public HelloWorld(string name)
{
myname = name;
}
public HelloWorld()
: this(null)
{
}
public string Name
{
get { return myname; }
}
public void SayHello()
{
if (myname == null)
{
Console.WriteLine("Hello World!");
}
else
{
Console.WriteLine("Hello,"+myname);
}
}
}
}
运行结果:
列出程序集所有类型
Class1
HelloWorld
列出HelloWorld中所有的方法
get_Name
SayHello
GetType
ToString
Equals
GetHashCode
实例化HelloWorld,并调用SayHello方法
Hello World!
Hello,ZhenLei
http://hi.baidu.com/zhiwei%5F117/blog/item/40182d95812e0514d31b707b.html
加支付宝好友偷能量挖...