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
)最后一个激活属性应当如何使用呢?

解决方案 »

  1.   

    createInstance有三个重载
    CreateInstance(String)
    CreateInstance(String, Boolean)
    CreateInstance(String, Boolean, BindingFlags, Binder, Object[], CultureInfo, Object[])
      

  2.   

    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. 
      

  3.   

    你是使用 Assembly.LoadFrom 加载的么
      

  4.   

    activationAttributes
    类型:System.Object[]
    包含一个或多个可以参与激活的属性的数组。通常为包含单个 UrlAttribute 对象的数组。UrlAttribute 指定激活远程对象所需的 URL。有关客户端激活的对象的详细说明,请参见客户端激活。 参考
    http://msdn.microsoft.com/zh-cn/library/ck6xe688(v=VS.90).aspx
      

  5.   

    我是使用动态编译后的结果来的:  Assembly objAssembly = objCompilerResults.CompiledAssembly;
      

  6.   

    谢谢大家的回答,我是windows应用程序,而不是web的,如何用UrlAttribute 呢?
      

  7.   

    http://support.microsoft.com/kb/828991/zh-cn看下这个,是否有帮助
      

  8.   

    谢谢,编译通过了,如果调用不带参数的构造函数呢,就不会抛出异常。if (objs == null)
        objNewObject = objAssembly.CreateInstance(objClassName); //不带参数构造,不会抛出异常
    else
    {
        objNewObject = objAssembly.CreateInstance(objClassName, false,
                         BindingFlags.CreateInstance,
                         null, objs, null, null);          //带参数构造,抛出异常
    }
      

  9.   

    是的。我不知Assembly.CreateInstance如何构造带参数的实例
      

  10.   

     CreateInstance(
        string assemblyName,
        string typeName,
        Object[] activationAttributes
    )activationAttributes = new object[]{你的参数}
      

  11.   

    public Object CreateInstance(
    string typeName,
    bool ignoreCase,
    BindingFlags bindingAttr,
    Binder binder,
    Object[] args,
    CultureInfo culture,
    Object[] activationAttributes
    )
      

  12.   

    http://msdn.microsoft.com/en-us/library/aa329908(v=VS.71).aspx