您的位置:首页 > 编程语言 > ASP

【Asp.Net】反射技术

2017-03-26 22:36 253 查看
主程序



using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Test0326;

namespace WebApplication19
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Assembly myAssembly = Assembly.LoadFrom(Server.MapPath("bin/Test0326.dll"));
//获取类型信息
Type t = myAssembly.GetType("Test0326.TestClass");
//构造器的参数
object[] constuctParms = new object[] {"timmy"};
//根据类型创建对象
object dObj = Activator.CreateInstance(t, constuctParms);
//获取方法的信息
MethodInfo method = t.GetMethod("GetValue");
//调用方法的一些标志位,这里的含义是Public并且是实例方法,这也是默认的值
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
//GetValue方法的参数
object[] parameters = new object[] { "Hi" };
//调用方法,用一个object接收返回值
TextBox1.Text = method.Invoke(dObj, flag, Type.DefaultBinder, parameters, null) as string;
}
}
}


Test0326.dll 程序集代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test0326
{
public class TestClass
{
private string _value;
public TestClass()
{
}
public TestClass(string value)
{
_value = value;
}
public string GetValue(string prefix)
{
if (_value == null)
return "NULL";
else
return prefix + "  :  " + _value;
}
public string Value
{
set
{
_value = value;
}
get
{
if (_value == null)
return "NULL";
else
return _value;
}
}
}
}


参考链接:
http://www.cnblogs.com/wangshenhe/p/3256657.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  反射