c# 动态加载dll时 有参构造 和 方法重载问题 本帖最后由 tommir3 于 2010-01-19 09:31:13 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 object tmpobj = ass.CreateInstance(tp, true, BindingFlags.CreateInstance, null, new object[] { 参数1, 参数N }, null, null); 谢谢,我参数传错一个.....传成BindingFlags.Instance这个了.....是创建实例....谢谢你能告诉下调用重载方法怎么调用吗? string path = System.IO.Directory.GetCurrentDirectory(); //获得当前项目所在的据对路径Assembly ass = Assembly.LoadFile(path + "\\lassLibrary1Demo.dll"); //获取DLL的集合Type tp = ass.GetType("lassLibrary1Demo.Sample1"); //获得DLL中的指定类型object tmpobj = ass.CreateInstance("lassLibrary1Demo.Sample1", false, BindingFlags.CreateInstance, null, new object[] { "12345" }, null, null); //获得DLL中指定类的实例string classAttribute = (string)tp.InvokeMember("Str1", BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, tmpobj, null); Console.WriteLine(classAttribute);//这里在调用重载方法时出错MethodInfo mi = tp.GetMethod("Display"); //取的方法描述string result = (string)mi.Invoke(tmpobj, new object[] { "abc" }); //调用的对象,以及参数Console.WriteLine(result); //把演示结果打印出来Console.Read(); public class Sample1 { private string _str1; public Sample1(string s) { _str1 = s; } /// <summary> /// 显示 /// </summary> /// <param name="s"></param> /// <returns></returns> public string Display() { return "默认方法"; } /// <summary> /// 显示 /// </summary> /// <param name="s"></param> /// <returns></returns> public string Display(string s) { return s + "————添加后的元素"; } } 楼上写的很清楚了;这个是msdn 的 AppDomain用例,可以参考看看using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Reflection; namespace UnloadDll { class Program { static void Main(string[] args) { string callingDomainName = AppDomain.CurrentDomain.FriendlyName;//Thread.GetDomain().FriendlyName; Console.WriteLine(callingDomainName); AppDomain ad = AppDomain.CreateDomain("DLL Unload test"); ProxyObject obj = (ProxyObject)ad.CreateInstanceFromAndUnwrap(@"UnloadDll.exe", "UnloadDll.ProxyObject"); obj.LoadAssembly(); obj.Invoke("TestDll.Class1", "Test", "It's a test"); AppDomain.Unload(ad); obj = null; Console.ReadLine(); } } class ProxyObject : MarshalByRefObject { Assembly assembly = null; public void LoadAssembly() { assembly = Assembly.LoadFile(@"TestDLL.dll"); } public bool Invoke(string fullClassName, string methodName, params Object[] args) { if(assembly == null) return false; Type tp = assembly.GetType(fullClassName); if (tp == null) return false; MethodInfo method = tp.GetMethod(methodName); if (method == null) return false; Object obj = Activator.CreateInstance(tp); method.Invoke(obj, args); return true; } } } object tmpobj = ass.CreateInstance("lassLibrary1Demo.Sample1", false, BindingFlags.CreateInstance, null, new object[] { },null,null); 结贴了调用重载方法的问题一直没人会现在又碰到个问题,如果调用DLL中的方法中,返回类型为类或者借口,如何解决。。我另开一贴子问,结贴给分先 C#中实时获取数据的问题 用记事本存储信息的通讯录(菜鸟求助) 讨论请进:关于对话状态管理方式与序列化的问题 读取html的文件内容 c# winform 按enter 进入下一个控件。。。在线等 .net实现生成序列号 自定义分页链接问题 虚基类的限制问题================在线=============== 求助分组统计问题,商品表里有所属分类,但是末级,要求按他所属大类统计 把数据源控件内的数据截取出来,高手请指点 最后分请教一个比较难的选择问题 c# 从数据库获取值并右截取怎么型????????
谢谢,我参数传错一个.....
传成BindingFlags.Instance这个了.....
是创建实例....
谢谢你能告诉下调用重载方法怎么调用吗?
string path = System.IO.Directory.GetCurrentDirectory(); //获得当前项目所在的据对路径
Assembly ass = Assembly.LoadFile(path + "\\lassLibrary1Demo.dll"); //获取DLL的集合
Type tp = ass.GetType("lassLibrary1Demo.Sample1"); //获得DLL中的指定类型
object tmpobj = ass.CreateInstance("lassLibrary1Demo.Sample1", false, BindingFlags.CreateInstance, null, new object[] { "12345" }, null, null); //获得DLL中指定类的实例
string classAttribute = (string)tp.InvokeMember("Str1", BindingFlags.DeclaredOnly | BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.GetProperty, null, tmpobj, null);
Console.WriteLine(classAttribute);
//这里在调用重载方法时出错
MethodInfo mi = tp.GetMethod("Display"); //取的方法描述
string result = (string)mi.Invoke(tmpobj, new object[] { "abc" }); //调用的对象,以及参数
Console.WriteLine(result); //把演示结果打印出来
Console.Read(); public class Sample1
{
private string _str1; public Sample1(string s)
{
_str1 = s;
} /// <summary>
/// 显示
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public string Display()
{
return "默认方法";
} /// <summary>
/// 显示
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public string Display(string s)
{
return s + "————添加后的元素";
}
}
这个是msdn 的 AppDomain用例,可以参考看看
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
namespace UnloadDll
{
class Program
{
static void Main(string[] args)
{
string callingDomainName = AppDomain.CurrentDomain.FriendlyName;//Thread.GetDomain().FriendlyName;
Console.WriteLine(callingDomainName);
AppDomain ad = AppDomain.CreateDomain("DLL Unload test");
ProxyObject obj = (ProxyObject)ad.CreateInstanceFromAndUnwrap(@"UnloadDll.exe", "UnloadDll.ProxyObject");
obj.LoadAssembly();
obj.Invoke("TestDll.Class1", "Test", "It's a test");
AppDomain.Unload(ad);
obj = null;
Console.ReadLine();
}
}
class ProxyObject : MarshalByRefObject
{
Assembly assembly = null;
public void LoadAssembly()
{
assembly = Assembly.LoadFile(@"TestDLL.dll");
}
public bool Invoke(string fullClassName, string methodName, params Object[] args)
{
if(assembly == null)
return false;
Type tp = assembly.GetType(fullClassName);
if (tp == null)
return false;
MethodInfo method = tp.GetMethod(methodName);
if (method == null)
return false;
Object obj = Activator.CreateInstance(tp);
method.Invoke(obj, args);
return true;
}
}
}
调用重载方法的问题一直没人会
现在又碰到个问题,
如果调用DLL中的方法中,返回类型为类或者借口,
如何解决。。
我另开一贴子问,
结贴给分先