本文共 1741 字,大约阅读时间需要 5 分钟。
主程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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 程序集代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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; } } } } |
参考链接:
本文转自daniel8294 51CTO博客,原文链接:http://blog.51cto.com/acadia627/1910538,如需转载请自行联系原作者