【高分求救】用反射创建对类的实例,构造函数有参数,出现“调用的目标发生了异常。”

wonsoft 2010-04-23 10:56:56
Windows应用程序,使用Assembly.CreateInstance创建实例,构造函数中有参数,应当如何处理呢:
objNewObject = objAssembly.CreateInstance(objClassName, false, BindingFlags.CreateInstance, null, objs, null, null);


抛出以下异常,应当如何解决?最后一个激活属性我应当如何使用呢?
未处理 System.Reflection.TargetInvocationException
Message="调用的目标发生了异常。"
Source="mscorlib"
StackTrace:
在 System.RuntimeMethodHandle._InvokeConstructor(Object[] args, SignatureStruct& signature, IntPtr declaringType)
在 System.RuntimeMethodHandle.InvokeConstructor(Object[] args, SignatureStruct signature, RuntimeTypeHandle declaringType)
在 System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
在 System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
在 System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
在 System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
在 Wonsoft.SourceCompiler.CShapCompiler.CreateObject() 位置 I:\Wonsoft\WonsoftScriptManager\Wonsoft\SourceCompiler\CShapCompiler.cs:行号 106
在 Wonsoft.SourceCompiler.CShapCompiler.GetObject() 位置 I:\Wonsoft\WonsoftScriptManager\Wonsoft\SourceCompiler\CShapCompiler.cs:行号 120
在 WonsoftScriptManager.Form1.GeneratePropertiesByOptionsWithDriver() 位置 I:\Wonsoft\WonsoftScriptManager\WonsoftScriptManager\Form1.cs:行号 299
在 WonsoftScriptManager.Form1.button1_Click_1(Object sender, EventArgs e) 位置 I:\Wonsoft\WonsoftScriptManager\WonsoftScriptManager\Form1.cs:行号 410
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
在 System.Windows.Forms.Application.Run(Form mainForm)
在 WonsoftScriptManager.Program.Main() 位置 I:\Wonsoft\WonsoftScriptManager\WonsoftScriptManager\Program.cs:行号 18
InnerException: System.NullReferenceException
Message="未将对象引用设置到对象的实例。"
Source="WonsoftScriptManager"
StackTrace:
在 WonsoftScriptManager.Form1.OnGridPropertyChanged(String propertyname) 位置 I:\Wonsoft\WonsoftScriptManager\WonsoftScriptManager\Form1.cs:行号 69
在 Wonsoft.Options1.set_vlan(Int32 value)
在 Wonsoft.Options1..ctor(IGridPropertyDriver owner)
InnerException:



ObjectHandle CreateInstance(
string assemblyName,
string typeName,
Object[] activationAttributes
)

最后一个激活属性应当如何使用呢?
...全文
614 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
wonsoft 2011-04-24
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 zhuchuanbing520 的回复:]
急急急急急急急急急急急急急
[/Quote]

请你调试一下要创建新实例的那个类的构造函数,你调试时,跟踪一下,看问题出在哪儿。
zhuchuanbing520 2011-04-17
  • 打赏
  • 举报
回复
急急急急急急急急急急急急急
Peter200694013 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wonsoft 的回复:]
引用 11 楼 sandy945 的回复:
还是抛出之前的异常么

是的。我不知Assembly.CreateInstance如何构造带参数的实例
[/Quote]
http://msdn.microsoft.com/en-us/library/aa329908(v=VS.71).aspx
Peter200694013 2010-04-23
  • 打赏
  • 举报
回复
public Object CreateInstance(
string typeName,
bool ignoreCase,
BindingFlags bindingAttr,
Binder binder,
Object[] args,
CultureInfo culture,
Object[] activationAttributes
)
wonsoft 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 wodegege10 的回复:]
CreateInstance(
string assemblyName,
string typeName,
Object[] activationAttributes
)

activationAttributes = new object[]{你的参数}
[/Quote]

没这么简单,这样会抛出另一个异常。
wenbin 2010-04-23
  • 打赏
  • 举报
回复
CreateInstance(
string assemblyName,
string typeName,
Object[] activationAttributes
)

activationAttributes = new object[]{你的参数}
wonsoft 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 sandy945 的回复:]
还是抛出之前的异常么
[/Quote]
是的。我不知Assembly.CreateInstance如何构造带参数的实例
阿非 2010-04-23
  • 打赏
  • 举报
回复
还是抛出之前的异常么
lanfengsese 2010-04-23
  • 打赏
  • 举报
回复
up,学习
wonsoft 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sandy945 的回复:]
http://support.microsoft.com/kb/828991/zh-cn

看下这个,是否有帮助
[/Quote]
谢谢,编译通过了,如果调用不带参数的构造函数呢,就不会抛出异常。

if (objs == null)
objNewObject = objAssembly.CreateInstance(objClassName); //不带参数构造,不会抛出异常
else
{
objNewObject = objAssembly.CreateInstance(objClassName, false,
BindingFlags.CreateInstance,
null, objs, null, null); //带参数构造,抛出异常
}
阿非 2010-04-23
  • 打赏
  • 举报
回复
http://support.microsoft.com/kb/828991/zh-cn

看下这个,是否有帮助
wonsoft 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 liuyileneal 的回复:]
activationAttributes
类型:System.Object[]
包含一个或多个可以参与激活的属性的数组。通常为包含单个 UrlAttribute 对象的数组。UrlAttribute 指定激活远程对象所需的 URL。有关客户端激活的对象的详细说明,请参见客户端激活。

参考
http://msdn.microsoft.com/zh-cn/library/ck6xe688……
[/Quote]
谢谢大家的回答,我是windows应用程序,而不是web的,如何用UrlAttribute 呢?
wonsoft 2010-04-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sandy945 的回复:]
你是使用 Assembly.LoadFrom 加载的么
[/Quote]
我是使用动态编译后的结果来的:
[code=c#]
Assembly objAssembly = objCompilerResults.CompiledAssembly;
[/code]
liuyileneal 2010-04-23
  • 打赏
  • 举报
回复
activationAttributes
类型:System.Object[]
包含一个或多个可以参与激活的属性的数组。通常为包含单个 UrlAttribute 对象的数组。UrlAttribute 指定激活远程对象所需的 URL。有关客户端激活的对象的详细说明,请参见客户端激活。

参考
http://msdn.microsoft.com/zh-cn/library/ck6xe688(v=VS.90).aspx
阿非 2010-04-23
  • 打赏
  • 举报
回复
你是使用 Assembly.LoadFrom 加载的么
liuyileneal 2010-04-23
  • 打赏
  • 举报
回复
activationAttributes
Type: System.Object[]
An array of one or more attributes that can participate in activation. Typically, an array that contains a single UrlAttribute object. The UrlAttribute specifies the URL that is required to activate a remote object. For a detailed description of client-activated objects, see Client Activation.
liuyileneal 2010-04-23
  • 打赏
  • 举报
回复
createInstance有三个重载
CreateInstance(String)
CreateInstance(String, Boolean)
CreateInstance(String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])
lee_b 2010-04-23
  • 打赏
  • 举报
回复
沙发,,顶起再说。
sxiaohui8709253 2010-04-23
  • 打赏
  • 举报
回复
学习·~
wonsoft 2010-04-23
  • 打赏
  • 举报
回复
找到问题所在了,问题的根源不在创建进程,而发生在该构造函数内部。

110,533

社区成员

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

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

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