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

泪湿孤枕 提交于 2019-12-03 22:46:39
leppie

I think you are dealing with a Type mismatch.

Likely the assembly is referenced in different places, or they are compiled against different versions.

I suggest you iterate through the ConstructorInfo's and do a paramtype == typeof(DelayComposite) on the appropriate parameter.

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.

VolkerK

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?

If you want to call this contructor...

public DelayCompositeDesigner(DelayComposite CompositeObject)

...just use this:

var designer = Activator.CreateInstance(typeof(DelayCompositeDesigner), new DelayComposite(4));

or

var designer = Activator.CreateInstance<DelayCompositeDesigner>(new DelayComposite(4));
Jason Jackson

I discovered another way of creating an instance of an object without calling the constructor at all while answering another question on SF.

In the System.Runtime.Serialization namespace there is a function FormatterServices.GetUninitializedObject(type) that will create an object without calling constructor.

If you look at that function in Reflector you will see it is making an external call. I don't know how black magic is actually happening under the hood. But I did prove to myself that the constructor was never called but the object was instantiated.

DevNull

I had a similar issue, however my problem was due to the visibility of the constructor. This stack overflow helped me:

Instantiating a constructor with parameters in an internal class with reflection

You can use the following overload on CreateInstance:

public static Object CreateInstance(
    Type type,
    Object[] args
)

And in your case it'd be (I think):

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

I found a solution to the problem, I was struggling with the same issue.

Here is my activator:

private void LoadTask(FileInfo dll)
    {
        Assembly assembly = Assembly.LoadFrom(dll.FullName);

        foreach (Type type in assembly.GetTypes())
        {
            var hasInterface = type.GetInterface("ITask") != null;

            if (type.IsClass && hasInterface)
            {
                var instance = Activator.CreateInstance(type, _proxy, _context);
                _tasks.Add(type.Name, (ITask)instance);
            }
        }
    }

And here is my class to activate, note that I had to change the constructor params to objects, the only way I could get it to work.

public class CalculateDowntimeTask : Task<CalculateDowntimeTask>
{
    public CalculateDowntimeTask(object proxy, object context) : 
        base((TaskServiceClient)proxy, (TaskDataDataContext)context) { }

    public override void Execute()
    {
        LogMessage(new TaskMessage() { Message = "Testing" });
        BroadcastMessage(new TaskMessage() { Message = "Testing" });
    }
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!