net技术中"反射"的使用方法
2009-09-15 09:35
513 查看
本文主要介绍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);
相关文章推荐
- net技术中"反射"的使用方法
- "ASP.NET 管理实用工具"使用方法
- 在asp.net中使用ajax时出现的"'Sys' 未定义"的解决方法
- 关于在英创em9161板上使用ADO.net连接远程数据库提示 “无法找到 PInvoke dll"dbnetlib.dll"”问题解决方法
- 使用反射技术实现的导入Excel文件到数据库的公共方法
- Xcode中git的使用方法介绍与"Please tell me who you are"问题的解决方案
- "单击以激活及使用该控件"的解决方法
- ASP.NET对路径"xxxxx"的访问被拒绝的解决方法
- 使用控件时提示"未声明标识符"的解决方法
- asp.net中使用fckeditor时,提示“this connector is disabled Please check the"editor/filemanager/connectors/aspx/config.aspx”解决办法
- Windows 7中使用Microsoft File Transfer Manager报"Access Denied to temp file"错误的解决方法
- 使用CString的方法(以及用VC写DLL中"error LNK2005: _DllMain@12 already defined"的解决办法)
- 使用反射技术,为DataGridView增加一个自动填充的扩展方法
- ASP.net中"名称以无效字符开头"问题解决方法
- 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法
- 使用Putty登录Ubuntu后得到"Server refused our key"错误的一种解决方法
- 使用反射技术在asp.net页间传递对象
- Gradle 使用Jetty插件启动web项目时出现"java.lang.OutOfMemoryError: PermGen space"解决方法
- js_html_input中autocomplete="off"在chrom中失效的解决办法 使用JS模拟锚点跳转 js如何获取url参数 C#模拟httpwebrequest请求_向服务器模拟cookie发送 实习期学到的技术(一) LinqPad的变量比较功能 ASP.NET EF 使用LinqPad 快速学习Linq
- 使用rs.next()提示"Unhandled exception type SQLException"两种解决方法