Activator.CreateInstance(t, 42, args) 找不到构造函数

发布于 2024-07-23 12:21:22 字数 723 浏览 14 评论 0 原文

我在工厂模式样式函数中使用以下代码(稍微扩展的版本):

公共类 SingleItemNew :CheckoutContext { 公共 BookingContext 数据 { 获取; 放; } 公共 SingleItemNew(BookingContext 数据) { 数据=数据; } } 公共 CheckoutContext findContext(BookingContext 数据) { 类型 contextType = Type.GetType("CheckoutProcesses." + data.Case.ToString()); CheckoutContext 输出 = Activator.CreateInstance(contextType, BindingFlags.CreateInstance, new[] { data }) 作为 CheckoutContext; 返回输出; }

但是,它在运行时抛出一个构造函数未找到异常,我不明白为什么。

data.Case.ToString() 方法返回类的名称 SingleItemNew,该类具有采用单个参数的构造函数。

有谁知道问题是什么?

干杯,艾德

I am using (a slightly extended version of) the following code in a factory-pattern style function:

public class SingleItemNew : CheckoutContext { public BookingContext Data { get; set; } public SingleItemNew(BookingContext data) { Data = data; } } public CheckoutContext findContext(BookingContext data) { Type contextType = Type.GetType("CheckoutProcesses." + data.Case.ToString()); CheckoutContext output = Activator.CreateInstance(contextType, BindingFlags.CreateInstance, new[] { data }) as CheckoutContext; return output; }

however, it throws a constuctor not found exception when run, and I cannot figure out why.

The data.Case.ToString() method returns the name of a class, SingleItemNew, that has a constructor taking a single argument.

Does anyone have any idea what the problem is?

Cheers, Ed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

肤浅与狂妄 2024-07-30 12:21:22

试试这个:

  Type contextType = Type.GetType("CheckoutProcesses." + data.Case.ToString());
  CheckoutContext output = 
      (CheckoutContext)Activator.CreateInstance(contextType, data);

您的代码不起作用的原因是 Activator.CreateInstance 并没有真正具有您想要的重载。 所以您可能想知道为什么代码能够编译! 原因是,它有一个重载,需要 (Type type, params object[] args) ,它与您的方法调用匹配,因此它可以编译,但在运行时,它会在您的类型中搜索采用 BindingFlagsBookingContext[] 显然不是您的类型所具有的。

Try this one:

  Type contextType = Type.GetType("CheckoutProcesses." + data.Case.ToString());
  CheckoutContext output = 
      (CheckoutContext)Activator.CreateInstance(contextType, data);

The reason you code doesn't work is that Activator.CreateInstance doesn't really have the overload you want. So you might wonder why the code compiles at all! The reason is, it has an overload that takes (Type type, params object[] args) which matches your method call so it compiles but at runtime, it searches your type for a constructor taking a BindingFlags and a BookingContext[] which is clearly not what your type has.

惟欲睡 2024-07-30 12:21:22

构造函数是公共的吗?

BookingContext 类型的单个参数吗?

问题是,这显然是一个更大系统的一部分 - 如果您可以生成一个 /complete.html" rel="nofollow noreferrer">简短但完整的程序 演示了该问题。 然后我们可以修复该程序中的问题,并且您可以将修复移植回您的真实系统。 否则我们真的只是猜测:(

Is the constructor public?

Is the single parameter of type BookingContext?

The trouble is, this is clearly part of a bigger system - it would be much easier to help you if you could produce a short but complete program which demonstrated the problem. Then we could fix the problem in that program, and you could port your fix back to your real system. Otherwisewise we're really just guessing :(

山川志 2024-07-30 12:21:22

SingleItemNew 构造函数是否将 BookingContext 作为参数? 如果不完全匹配,则会失败:

class ParamType   {    }
class DerivedParamType : ParamType    {    }
class TypeToCreate
{
    public TypeToCreate(DerivedParamType data)
    {
        // do something
    }
}

ParamType args = new ParamType();
// this call will fail: "constructor not found"
object obj = Activator.CreateInstance(typeof(TypeToCreate), new object[] { args });

// this call would work, since the input type matches the constructor
DerivedParamType args = new DerivedParamType();
object obj = Activator.CreateInstance(typeof(TypeToCreate), new object[] { args });

Does the SingleItemNew constructor take BookingContext as parameter? If it does not match exatcly it will fail:

class ParamType   {    }
class DerivedParamType : ParamType    {    }
class TypeToCreate
{
    public TypeToCreate(DerivedParamType data)
    {
        // do something
    }
}

ParamType args = new ParamType();
// this call will fail: "constructor not found"
object obj = Activator.CreateInstance(typeof(TypeToCreate), new object[] { args });

// this call would work, since the input type matches the constructor
DerivedParamType args = new DerivedParamType();
object obj = Activator.CreateInstance(typeof(TypeToCreate), new object[] { args });
梦罢 2024-07-30 12:21:22

这对我有用。

Type.GetType("命名空间.class, 命名空间");

This worked for me.

Type.GetType("namespace.class, namespace");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文