Activator.CreateInstance找不到构造函数(的MissingMethodException) [英] Activator.CreateInstance can't find the constructor (MissingMethodException)

查看:1068
本文介绍了Activator.CreateInstance找不到构造函数(的MissingMethodException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有具有以下构造函数的类

I have a class which has the following constructor

public DelayCompositeDesigner(DelayComposite CompositeObject)
{
    InitializeComponent();

    compositeObject = CompositeObject;  
}

还有一个默认的构造函数不带参数。

along with a default constructor with no parameters.

接下来我试图创建一个实例,但它只是工作没有参数:

Next I'm trying to create an instance, but it only works without parameters:

var designer = Activator.CreateInstance(designerAttribute.Designer);

这工作得很好,但如果我想传递参数,它不会:

This works just fine, but if I want to pass parameters it does not:

var designer = Activator.CreateInstance(designerAttribute.Designer, new DelayComposite(4));

这将导致一个的MissingMethodException

构造VOOR类型   Vialis.LightLink.Controller.Scenarios.Composites.DelayCompositeDesigner   未找到

Constructor voor type Vialis.LightLink.Controller.Scenarios.Composites.DelayCompositeDesigner was not found

任何想法吗?

现在的问题是我真的需要在施工期间传递的对象。

The problem is I really need to pass an object during construction.

您看,我有一个设计,它加载的所有从 CompositeBase 继承类型。这些将被添加到一个列表,用户可将它们拖到设计师。在这样做的拖动一个实例添加到设计。每个类都对他们的定义的自定义属性:

You see I have a designer which loads all the types that inherit from the CompositeBase. These are then added to a list from which the users can drag them to a designer. Upon doing so an instance of the dragged is added to the designer. Each of these classes have custom properties defined on them:

[CompositeMetaData("Delay","Sets the delay between commands",1)]
[CompositeDesigner(typeof(DelayCompositeDesigner))]
public class DelayComposite : CompositeBase
{
}

当用户在设计中选择一个项目,它着眼于这些属性为了装载一个设计师的类型。例如,在的情况下,在 DelayComposite 它将加载了一个用户来控制,一个标签和一个滑动件,其允许用户设定的该延迟属性 DelayComposite 实例。

When the user selects an item in the designer, it looks at these attributes in order to load up a designer for that type. For example, in the case of the DelayComposite it would load up a user control which has a label and a slider which allow the user to set the "Delay" property of the DelayComposite instance.

到目前为止,这工作正常,如果我不传递任何参数的构造函数。设计者创建 DelayCompositeDesigner 的实例,并将其分配给一个WPF 内容presenter 的内容属性。

So far this works fine if I don't pass any parameters to the constructor. The designer creates an instance of the DelayCompositeDesigner and assigns it to the content property of a WPF ContentPresenter.

但由于该设计师需要修改的属性,选择 DelayComposite 在设计师,我要这个实例传递给它。这就是为什么在构造看起来撒谎的:

But since that designer needs to modify the properties of the selected DelayComposite in the designer, I have to pass this instance to it. That is why the constructor looks lie this:

public DelayCompositeDesigner(DelayComposite CompositeObject)
{
    InitializeComponent();

    compositeObject = CompositeObject;
}

建议,欢迎

@VolkerK

你的code的结果是这样的:

The result of your code is this:

< ----富   Vialis.LightLink.Controller.Scenarios.Composites.DelayCompositeDesignerVoid   .ctor()   Vialis.LightLink.Controller.Scenarios.Composites.DelayCompositeDesignerVoid   .ctor(Vialis.LightLink.Controller.Scenarios.Composites.DelayComposite)   参数:Vialis.LightLink.Controller.Scenarios.Composites.DelayComposite   FOO ---->

<---- foo Vialis.LightLink.Controller.Scenarios.Composites.DelayCompositeDesignerVoid .ctor() Vialis.LightLink.Controller.Scenarios.Composites.DelayCompositeDesignerVoid .ctor(Vialis.LightLink.Controller.Scenarios.Composites.DelayComposite) param:Vialis.LightLink.Controller.Scenarios.Composites.DelayComposite foo ---->

Leppie,你是正确的,我曾由于某种原因,引用的复合材料组件在我的UI应用程序......这是不是我应该做的,因为我是在运行时加载它。下面code工作:


Leppie, you were correct, I had for some reason referenced the Composites assembly in my UI application... which is not something I should have done as I was loading it at runtime. The following code works:

object composite = Activator.CreateInstance(item.CompositType,(byte)205);
                    var designer = Activator.CreateInstance(designerAttribute.Designer, composite);

正如你所看到的code没有知识的 DelayComposite 键入

这解决了当前的问题,但引入了许多新的什么,我想实现, 无论哪种方式,感谢你们,感谢大家谁曾在这里说。

This solves the current problem, but introduces many new ones for what I want to achieve, either way thank you and thank you to everyone who has replied here.

作为以下code,由多人建议:

As for the following code, suggested by multiple people:

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

Activator.CreateInstance 有看起来像这样的签名:

The Activator.CreateInstance has a signature that looks like this:

Activator.CreateInstance(Type type, params object[] obj)

因此​​,它应该接受我的code,但我会努力的建议code

So it should accept my code, but I will try the suggested code

更新:

我已经试过这样的建议:

I've tried this as suggested:

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

的结果是相同的。

The result is the same.

推荐答案

我想你正在处理一个类型不匹配。

I think you are dealing with a Type mismatch.

可能是装配引用在不同的地方,或者正在编译的不同版本。

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

我建议你通过ConstructorInfo的迭代,并做了 paramtype == typeof运算(DelayComposite)在适当的参数。

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

这篇关于Activator.CreateInstance找不到构造函数(的MissingMethodException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆