反射的使用方法(轉)
来源:互联网 发布:淘宝模特保持身材 编辑:程序博客网 时间:2024/05/18 09:20
Vincent.Q
重剑无锋,大巧不工 电话:13867408830;QQ:154538878;邮箱:xiyang-0@163.com
本文主要介绍net技术中"反射"的使用方法,包括如何对属性赋值,获取变量,属性,方法,事件的列表,如何设置属性及调用方法等.希望对新人有所帮助咯.
//反射的使用方法
public class TestProperty
{
private int i_seqnum = 0;
private string s_authorid = string.Empty;
public int SeqNum
{
get { return i_seqnum; }
set { i_seqnum = value; }
}
public string AuthorID
{
get { return s_authorid; }
set { s_authorid = value; }
}
public TestProperty()
{
}
public TestProperty(string as_authorid)
{
this.AuthorID = as_authorid;
}
public void GetAuthorID()
{
if (s_authorid == "" || s_authorid == string.Empty)
{
System.Console.WriteLine("Null");
}
else
{
System.Console.WriteLine(s_authorid);
}
}
}
//调用方式
TestProperty test = new TestProperty();
Type t = test.GetType();
//列出类型的结构信息
System.Console.WriteLine("列出类型的结构信息");
ConstructorInfo[] test_constructor = t.GetConstructors();
foreach (ConstructorInfo item_constructor in test_constructor)
{
System.Console.WriteLine(item_constructor.Name);
}
//列出TestProperty类中所有变量
System.Console.WriteLine("列出TestProperty类中所有变量");
FieldInfo[] test_field = t.GetFields();
foreach (FieldInfo item_field in test_field)
{
System.Console.WriteLine(item_field.Name);
}
//列出TestProperty类中所有属性
System.Console.WriteLine("列出TestProperty类中所有属性");
PropertyInfo[] list_property = t.GetProperties();
foreach (PropertyInfo item_property in list_property)
{
System.Console.WriteLine(item_property.Name);
}
//列出TestProperty类中所有方法
System.Console.WriteLine("列出TestProperty类中所有方法");
MethodInfo[] test_method = t.GetMethods();
foreach (MethodInfo item_method in test_method)
{
System.Console.WriteLine(item_method.Name);
}
//列出TestProperty类中所有事件
System.Console.WriteLine("列出TestProperty类中所有事件");
EventInfo[] test_event = t.GetEvents();
foreach (EventInfo item_event in test_event)
{
System.Console.WriteLine(item_event.Name);
}
//赋值
t.GetProperty("SeqNum").SetValue(test, 1, null);
t.GetProperty("AuthorID").SetValue(test, "ADB01", null);
//取值
int i_seqnum = Convert.ToInt32(t.GetProperty("SeqNum").GetValue(test, null));
string s_authorid = t.GetProperty("AuthorID").GetValue(test, null).ToString();
//使用反射调用方法
//test.GetAuthorID();
System.Console.WriteLine("使用反射调用方法");
string[] s = { "BC001" };
MethodInfo method_getauthorid = t.GetMethod("GetAuthorID");
//第1种方法
object obj_name = Activator.CreateInstance(t, s);
method_getauthorid.Invoke(obj_name, null);
//第2种方法
object obj = Activator.CreateInstance(t);
method_getauthorid.Invoke(obj, null);
- 反射的使用方法(轉)
- 反射的具体使用方法
- VS2005中反射的使用方法
- java invoke 反射的使用方法
- java invoke 反射的使用方法
- 反射使用方法
- net技术中"反射"的使用方法
- net技术中"反射"的使用方法
- java中反射的基本使用方法
- c# 反射的使用方法,不知道是不是最全的
- c# 反射的使用方法,不知道是不是最全的
- Filed 类的使用方法以及java反射机制的案例
- 实例讲解Java编程中数组反射的使用方法
- Java反射基本使用方法 示例
- 反射(构造方法Constructor的反射)
- Java反射(五)----- 方法的反射
- Java反射(二):反射的使用
- indexof()的使用方法
- velocity 手册
- c#泛型的使用(轉)
- CLI 语言一览表
- C/C++一些知识2(指针)
- the Differences of Fold-left and Fold-right
- 反射的使用方法(轉)
- 最适合初学液晶者参考的程序
- 随机散列的实现(VB2005)
- jffs2文件系统不支持export的问题
- 在VC里没有ActiveMovieControl Object这个控件
- 修改网址占位符后的参数
- 我眼中的微软大战略和中国软件之殇
- css+div布局
- javascript实现无刷新计算GridView里购物清单总价格