我在调用DLL时出现这样的问题 请看代码:
aaa.DLL:
using System;
using System.Reflection; //程序集
using System.Threading;namespace CheShidll
{
public Class1(Object send)
{
//省略代码
}
public Obj ReceiveMoMessage(Object obj)
{
//省略代码
}}
调用
dss = Assembly.LoadFile(@"D:、aaa.DLL);//读取程序集
foreach(Type dsstype in dss.GetTypes())
{
Type[] chanshu=new Type[1]{typeof(Object)};
ConstructorInfo ci=dsstype.GetConstructor(chanshu);
Object[] ts=new object[1];
ts[0]=new SendSms();
user=ci.Invoke(ts);
MethodInfo m=user.GetType().GetMethod("ReceiveMoMessage");
m.Invoke(Activator.CreateInstance(user.GetType()),new object[] {momsg});
}
在 m.Invoke(Activator.CreateInstance(user.GetType()),new object[] {momsg});
这句出现 "没有为该对象定义无参数的构造函数。"提示
请问我应该怎么做?
aaa.DLL:
using System;
using System.Reflection; //程序集
using System.Threading;namespace CheShidll
{
public Class1(Object send)
{
//省略代码
}
public Obj ReceiveMoMessage(Object obj)
{
//省略代码
}}
调用
dss = Assembly.LoadFile(@"D:、aaa.DLL);//读取程序集
foreach(Type dsstype in dss.GetTypes())
{
Type[] chanshu=new Type[1]{typeof(Object)};
ConstructorInfo ci=dsstype.GetConstructor(chanshu);
Object[] ts=new object[1];
ts[0]=new SendSms();
user=ci.Invoke(ts);
MethodInfo m=user.GetType().GetMethod("ReceiveMoMessage");
m.Invoke(Activator.CreateInstance(user.GetType()),new object[] {momsg});
}
在 m.Invoke(Activator.CreateInstance(user.GetType()),new object[] {momsg});
这句出现 "没有为该对象定义无参数的构造函数。"提示
请问我应该怎么做?
以上是MSDN中的,你明白你那里错了吧!
若类里面要外部传进去的变量,只有在类里面在加一个公用的方法或写属性,
然后调用 invoke 方法执行传进去.
如下:
Assembly _Assembly = Assembly.Load("程序集名");
Type _Type = _Assembly.GetType("完整类名");
Form _Form = (Form)Activator.CreateInstance(_Type);
System.Reflection.EventInfo _EventInfo = _Type.GetEvent("RemoveFormHandler");_EventInfo.AddEventHandler(_Form, new Com.Psi.UIBase.RemoveFormDelegate(RemoveForm));System.Reflection.FieldInfo _FieldInfoSys = _Type.GetField("m_SysDataSet");
_FieldInfoSys.SetValue(_Form, this.m_TableDataSet);_Form.Show()
根据名称来查找所要的方法不是什么好做法。为什么不用接口?
如果构造函数必须要有参数的话,怎么可能不给参数而创建一个对象呢?该怎么办只有你自己最清楚。