.net C# 反射技术使用例子(转自:http://www.programbbs.com/doc/5740.htm)
2011-05-06 18:44
477 查看
获得实例的方法
1.获得同一程序集内的类型的实例。
无参数构造函数
Type t = Type.GetType("AppCode.Employe");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
object emp = t.Assembly.CreateInstance("AppCode.Employe");
if (emp == null)
{
textBox1.Text = "Create Instance Error";
return;
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("Type:" + t.FullName);
sb.AppendLine("Context:");
sb.AppendLine(emp.ToString());
textBox1.Text = sb.ToString();
有参数构造函数
Type t = Type.GetType("AppCode.Employe");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
//构造函数参数列表如下
//public Employe(string userid, string username, int age)
//生成参数类型数组
Type[] ts = new Type[3] { typeof(System.String),typeof(System.String), typeof(System.Int16) };
//获得构造函数
ConstructorInfo mi = t.GetConstructor(ts);
if (mi == null)
{
textBox1.Text = "Create ConstructorInfo Error";
return;
}
//生成参数值数组
object[] ps=new object[3];
ps[0] = "420106780911326";
ps[1] = "wesker";
ps[2] = 36;
object emp=mi.Invoke(ps);
StringBuilder sb = new StringBuilder();
sb.AppendLine("Type:" + t.FullName);
sb.AppendLine("Context:");
sb.AppendLine(emp.ToString());
textBox1.Text = sb.ToString();
2.获得不同程序集内的类型的实例。
比如当前窗体程序要通过反射获得一个名为"compenent1"(文件名为compenent1.dll)的程序集中的类型的实例。
无参数构造函数
Type t = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "//compenent1.dll").
GetType("compenent1.User");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
object user = t.Assembly.CreateInstance("compenent1.User");
if (user == null)
{
textBox1.Text = "Create Instance Error";
return;
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("Type:" + t.FullName);
sb.AppendLine("Context:");
sb.AppendLine(user.ToString());
textBox1.Text = sb.ToString();
有参数的构造函数
Type t = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "//compenent1.dll").
GetType("compenent1.User");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
//构造函数参数列表如下
//public Employe(string userid, string username, int age)
Type[] ts = new Type[3] { typeof(System.String), typeof(System.String), typeof(System.Int16) };
ConstructorInfo mi = t.GetConstructor(ts);
if (mi == null)
{
textBox1.Text = "Create ConstructorInfo Error";
return;
}
object[] ps = new object[3];
ps[0] = "420106780911326";
ps[1] = "wesker";
ps[2] = 36;
object user = mi.Invoke(ps);
StringBuilder sb = new StringBuilder();
sb.AppendLine("Type:" + t.FullName);
sb.AppendLine("Context:");
sb.AppendLine(user.ToString());
textBox1.Text = sb.ToString();
3.使用反射调用方法
Type t = Type.GetType("AppCode.Employe");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
object emp = t.Assembly.CreateInstance("AppCode.Employe");
if (emp == null)
{
textBox1.Text = "Create Instance Error";
return;
}
MethodInfo mi = t.GetMethod("ToString");
string context = mi.Invoke(emp,null).ToString();
StringBuilder sb = new StringBuilder();
sb.AppendLine("Context:");
sb.AppendLine(context);
sb.AppendLine("Type:" + t.FullName);
textBox1.Text = sb.ToString();
4.获得类的信息
通过Type.GetMethods方法获得方法的信息。
通过Type.GetConstructors获得构造函数。
1.获得同一程序集内的类型的实例。
无参数构造函数
Type t = Type.GetType("AppCode.Employe");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
object emp = t.Assembly.CreateInstance("AppCode.Employe");
if (emp == null)
{
textBox1.Text = "Create Instance Error";
return;
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("Type:" + t.FullName);
sb.AppendLine("Context:");
sb.AppendLine(emp.ToString());
textBox1.Text = sb.ToString();
有参数构造函数
Type t = Type.GetType("AppCode.Employe");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
//构造函数参数列表如下
//public Employe(string userid, string username, int age)
//生成参数类型数组
Type[] ts = new Type[3] { typeof(System.String),typeof(System.String), typeof(System.Int16) };
//获得构造函数
ConstructorInfo mi = t.GetConstructor(ts);
if (mi == null)
{
textBox1.Text = "Create ConstructorInfo Error";
return;
}
//生成参数值数组
object[] ps=new object[3];
ps[0] = "420106780911326";
ps[1] = "wesker";
ps[2] = 36;
object emp=mi.Invoke(ps);
StringBuilder sb = new StringBuilder();
sb.AppendLine("Type:" + t.FullName);
sb.AppendLine("Context:");
sb.AppendLine(emp.ToString());
textBox1.Text = sb.ToString();
2.获得不同程序集内的类型的实例。
比如当前窗体程序要通过反射获得一个名为"compenent1"(文件名为compenent1.dll)的程序集中的类型的实例。
无参数构造函数
Type t = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "//compenent1.dll").
GetType("compenent1.User");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
object user = t.Assembly.CreateInstance("compenent1.User");
if (user == null)
{
textBox1.Text = "Create Instance Error";
return;
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("Type:" + t.FullName);
sb.AppendLine("Context:");
sb.AppendLine(user.ToString());
textBox1.Text = sb.ToString();
有参数的构造函数
Type t = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "//compenent1.dll").
GetType("compenent1.User");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
//构造函数参数列表如下
//public Employe(string userid, string username, int age)
Type[] ts = new Type[3] { typeof(System.String), typeof(System.String), typeof(System.Int16) };
ConstructorInfo mi = t.GetConstructor(ts);
if (mi == null)
{
textBox1.Text = "Create ConstructorInfo Error";
return;
}
object[] ps = new object[3];
ps[0] = "420106780911326";
ps[1] = "wesker";
ps[2] = 36;
object user = mi.Invoke(ps);
StringBuilder sb = new StringBuilder();
sb.AppendLine("Type:" + t.FullName);
sb.AppendLine("Context:");
sb.AppendLine(user.ToString());
textBox1.Text = sb.ToString();
3.使用反射调用方法
Type t = Type.GetType("AppCode.Employe");
if (t == null)
{
textBox1.Text = "Create Type Error";
return;
}
object emp = t.Assembly.CreateInstance("AppCode.Employe");
if (emp == null)
{
textBox1.Text = "Create Instance Error";
return;
}
MethodInfo mi = t.GetMethod("ToString");
string context = mi.Invoke(emp,null).ToString();
StringBuilder sb = new StringBuilder();
sb.AppendLine("Context:");
sb.AppendLine(context);
sb.AppendLine("Type:" + t.FullName);
textBox1.Text = sb.ToString();
4.获得类的信息
通过Type.GetMethods方法获得方法的信息。
通过Type.GetConstructors获得构造函数。
相关文章推荐
- .net C# 反射技术使用例子
- 在C#中使用MSHTML的高级支持接口(引自http://www.vckbase.com/document/viewdoc.asp?id=1018)
- sql语句完成数据的导入导出(转载自http://www.programbbs.com/doc/317.htm)
- C#使用命令行方式实现Ping简单功能 http://www.cnblogs.com/kevinton/archive/2007/06/28/798581.html
- XPath语法 在C#中使用XPath示例 【转http://www.cnblogs.com/yukaizhao/archive/2011/07/25/xpath.html】非常详细的文章
- 使用PowerDesigner 设计数据库(http://www.ianywhere.com/上的例子)
- .net使用DotNetCharting控件生成报表统计图总结 (http://www.cnblogs.com/dreamof/archive/2008/07/18/1245887.html)
- C# 反射(一) 转自http://www.cnblogs.com/zhusl118/archive/2008/11/21/1338373.html
- C#之玩转反射【转:http://www.cnblogs.com/yaozhenfa/p/CSharp_Reflection_1.html】
- 利用webservice和.net技术上传和下载文件--Sending files in chunks with MTOM Web Services and .NET 2.0 from:http://www.codeproject.com/soap/MTO
- ghostDoct 使用 (转 http://www.cnblogs.com/RockyMyx/archive/2010/04/20/Project-Route-Using-GhostDoc.html)
- C# BackgroundWorker的使用【转-http://www.cnblogs.com/tom-tong/archive/2012/02/22/2363965.html】
- 使用反射和特性构造自己的ORM框架 转载自:http://www.cnblogs.com/xdesigner/archive/2008/06/24/1228702.html
- .NET中使用Redis:http://www.cnblogs.com/yangecnu/p/Introduct-Redis-in-DotNET.html
- 使用c#在vs2005下写的换桌面图片的小程序(from: http://www.wangchao.net.cn/bbsdetail_26766.html)
- (转载),方便使用http://www.cocoachina.com/bbs/read.php?tid=128244
- C#技术教程:http://www.cftea.com/docs/asp.net/c-sharp/
- 分享一个技术博客http://www.wutongwei.com
- .Net -- www.willsft.com -- 中国最专业的DotNet技术社区!
- C# -- www.willsft.com -- 中国最专业的DotNet技术社区!