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

后端 未结 9 2269
一个人的身影
一个人的身影 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:25

    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.

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

    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

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

    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));
    
    0 讨论(0)
  • 2021-02-18 16:34

    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.

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

    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) } 
    );
    
    0 讨论(0)
  • 2021-02-18 16:38

    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" });
        }
    }
    
    0 讨论(0)
提交回复
热议问题