Activator.CreateInstance can't find the constructor (MissingMethodException)

后端 未结 9 2270
一个人的身影
一个人的身影 2021-02-18 15:55

I have a class which has the following constructor

public DelayCompositeDesigner(DelayComposite CompositeObject)
{
    InitializeComponent();

    compositeObjec         


        
相关标签:
9条回答
  • 2021-02-18 16:39

    I would think that your call would need to be:

    var designer = Activator.CreateInstance(designerAttribute.Designer, new object[] { new DelayComposite(4) });
    

    Unless, of course, it is that, in which case the answer is not immediately obvious.

    0 讨论(0)
  • 2021-02-18 16:45

    When I encountered this problem, I was using a method that returned the parameter list to plug in to Activator.CreateInstance and it had a different number of arguments than the constructor of the object I was trying to create.

    0 讨论(0)
  • 2021-02-18 16:47

    Though I hate printf-like debugging ...

    public static void foo(Type t, params object[] p)
    {
        System.Diagnostics.Debug.WriteLine("<---- foo");
        foreach(System.Reflection.ConstructorInfo ci in t.GetConstructors())
        {
            System.Diagnostics.Debug.WriteLine(t.FullName + ci.ToString());
        }
        foreach (object o in p)
        {
            System.Diagnostics.Debug.WriteLine("param:" + o.GetType().FullName);
        }
        System.Diagnostics.Debug.WriteLine("foo ---->");
    }
    // ...
    foo(designerAttribute.Designer, new DelayComposite(4));
    var designer = Activator.CreateInstance(designerAttribute.Designer, new DelayComposite(4));
    

    What does that print in the visual studio's output window?

    0 讨论(0)
提交回复
热议问题