c#动态调用dll中的类方法

hafei168 2010-08-26 11:34:36
救命的作业!
...全文
158 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
ashley8979 2010-08-26
  • 打赏
  • 举报
回复
是c#,var 是c# 的关键字(.net2.0没有),编译器会自动推断类型
ZengHD 2010-08-26
  • 打赏
  • 举报
回复
不是使用下面这两个吧
LoadLibrary
GetProcAddress
wxm3630478 2010-08-26
  • 打赏
  • 举报
回复
Assembly

Load() 方法

-------------------去看反射的相关资料
hafei168 2010-08-26
  • 打赏
  • 举报
回复
请问你定义的var是什么类型啊
hafei168 2010-08-26
  • 打赏
  • 举报
回复
不是C#语言但是结构看懂啦!我试试能不能转化为C#
hafei168 2010-08-26
  • 打赏
  • 举报
回复
这个什么意思啊
ashley8979 2010-08-26
  • 打赏
  • 举报
回复
 //the instance method  in class1(ClassLibrary1.dll)
//public string Say()
//{
// long k = 0;
// for (int i = 0; i < 10000000; i++)
// {
// k += i;
// }
// return k.ToString();
//}
static void Main(string[] args)
{

string file = "ClassLibrary1";
var assembly = Assembly.Load(file);
var instance = assembly.CreateInstance("ClassLibrary1.Class1");
var method = instance.GetType().GetMethod("Say");
var result =method.Invoke(instance, null);
Console.WriteLine(result);
//print 49999995000000
Console.Read();

}
hafei168 2010-08-26
  • 打赏
  • 举报
回复
不用添加的那种!老是提示找不到主程序入口,也就是我指定要调用的方法。
是不是dll文件的问题啊,里边没有设置好
hafei168 2010-08-26
  • 打赏
  • 举报
回复
要动态引用的
huminghua 2010-08-26
  • 打赏
  • 举报
回复
添加引用就好啦 !
bios8086 2010-08-26
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20100812/10/d96fb8cd-4577-49a2-9322-5419c17b10a7.html
每天都有人来问这个问题 哈哈!
hafei168 2010-08-26
  • 打赏
  • 举报
回复
版主在哪里啊哭出来看看
hafei168 2010-08-26
  • 打赏
  • 举报
回复
赶紧来人看看啊!DLL动态调用获取主程序的类方法
急啊
shijiaokun 2010-08-26
  • 打赏
  • 举报
回复
DLLImport 是进入动态链接库,如C++开发的API等;
如果是.NET自己的dll,不行引用的话,则用反射直接加载(Assembly)
happyrain2010 2010-08-26
  • 打赏
  • 举报
回复
DLLImport不行吗
echoxue 2010-08-26
  • 打赏
  • 举报
回复
呵呵,就是反射啊,n年前的事情了
hafei168 2010-08-26
  • 打赏
  • 举报
回复
忙活啦一天终于实现啦哈哈
hafei168 2010-08-26
  • 打赏
  • 举报
回复
Assembly assembly = Assembly.LoadFrom("COMChild1.dll");
Type t = assembly.GetType("COMChild1.COMChild");
//string strReturn = (string)t.InvokeMember("ShowForm",BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,null,null,new object[] { 12 });

Object obj = t.InvokeMember(null,BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance,null,null,null);

ICOMChild opeatoe = obj as ICOMChild;
string str = "";
opeatoe.ShowForm(str).Show();
hafei168 2010-08-26
  • 打赏
  • 举报
回复
我试试

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧