Activator.CreateInstance() 的麻烦
我有一个工厂,应该在运行时创建从类 Foo 继承的对象。我认为System.Activator.CreateInstance的返回类型与它创建的对象的类型相同,但从下面的错误消息来看,它的返回类型是Object。
错误 1 无法将类型“object”隐式转换为“cs_sandbox.Foo”。存在显式转换(是否缺少转换?) F:\projects\cs_sandbox\Form1.cs 46 24 cs_sandbox
好的,所以也许我缺少转换,但
return (t)System.Activator.CreateInstance(t);
会导致另一条错误消息,我必须承认,这对我来说毫无意义:
错误 1 找不到类型或命名空间名称“t”(是否缺少 using 指令或程序集引用?)F:\projects\cs_sandbox\Form1.cs 45 25 cs_sandbox
这是我的代码:
class Foo { }
class FooChild1 : Foo { }
class FooChild2 : Foo { }
class MyFactory
{
public static Foo CreateInstance(string s)
{
Type t;
if (s.StartsWith("abcdef"))
{
t = typeof(FooChild1);
return System.Activator.CreateInstance(t);
}
else
{
t = typeof(FooChild2);
return System.Activator.CreateInstance(t);
}
}
}
如何修复此问题代码?或者,如果它不可修复,那么在运行时创建从特定类继承的对象的其他方法是什么?
I have a factory that is supposed to create objects that inherit from class Foo at run-time. I would think that System.Activator.CreateInstance's return type was the same as the type of an object it's creating, but judging from the following error message, its return type is Object.
Error 1 Cannot implicitly convert type 'object' to 'cs_sandbox.Foo'. An explicit conversion exists (are you missing a cast?) F:\projects\cs_sandbox\Form1.cs 46 24 cs_sandbox
OK, so maybe I am missing a cast, but
return (t)System.Activator.CreateInstance(t);
results in yet another error message, which -- I must admit -- makes no sense to me:
Error 1 The type or namespace name 't' could not be found (are you missing a using directive or an assembly reference?) F:\projects\cs_sandbox\Form1.cs 45 25 cs_sandbox
And here's my code:
class Foo { }
class FooChild1 : Foo { }
class FooChild2 : Foo { }
class MyFactory
{
public static Foo CreateInstance(string s)
{
Type t;
if (s.StartsWith("abcdef"))
{
t = typeof(FooChild1);
return System.Activator.CreateInstance(t);
}
else
{
t = typeof(FooChild2);
return System.Activator.CreateInstance(t);
}
}
}
How can I fix this code? Or, if it's not fixable, what are other ways of creating objects that inherit from a specific class at run-time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要将返回的对象转换为 Foo 类型。将其转换为变量中定义的类型是没有意义的。编译器应该知道这一点,因为通过继承层次结构进行转换的全部目的是满足编译器的静态类型检查。
有一个通用版本,
System.Activator.CreateInstance
,它创建一个已知类型(不是类型变量,而是类型参数或静态已知类型,在后一种情况下,它不不过很有意义):You need to cast the returned object to
Foo
type. It doesn't make sense to cast it to a type defined in a variable. It should be known by the compiler, as the whole point of casting through the inheritance hierarchy is satisfying compiler's static type checking.There's a generic version,
System.Activator.CreateInstance<T>
, which creates a known type (not a type variable but a type argument or a statically known type, in the latter case, it doesn't make much sense though):激活后我必须使用 UnWrap() 方法,如 MSDN 上所述:
如所述 MSDN 上。此外,我必须使用这样的参数:
I had to use to UnWrap() method after activating, like described on MSDN:
As described on MSDN. Furthermore, I had to use parameters like this:
更改代码后,
只需将结果强制转换为
Foo
,如或
第一个更强大,因为如果无法强制转换,您不会收到异常。它只会返回
null
。但这取决于你想要什么。在更改代码之前
您尝试过泛型吗?但是您确实遇到了问题。在现有的静态方法中,您尝试返回与Foo
没有任何关系的Bar
和Meh
。这始终是一个例外,除非您的方法还返回一个对象或公共祖先类型(如强制转换)。要控制更多内部类型,您可以定义您的方法将使用的多个泛型类型内部。
After you changed code
Just cast your results to
Foo
likeor
The first one is a bit more robust because you won't get an exception in case cast is not possible. It will simply return
null
. But it depends what you want.Before you changed code
Have you tried generics?But you do have a problem. In your existing static method you are trying to returnBar
andMeh
that aren't related toFoo
in any way. This will always be an exception unless your method also returns an object or a common ancestor type (as in casting).To control even more internal types you could define more than one generic type that your method would use internally.
我认为您可能应该这样做:
我的观点是,在这段代码中,CreateInstance 方法没有直接引用包含 FooChild1 和 FooChild2 的程序集。在原始代码中,您通过显式命名 FooChild1 和 FooChild2 创建一个类型,因此您不妨新建它们而不是激活它们。
这对你来说有意义吗?
I think you should probably do this:
My point is that in this code, the CreateInstance method has no direct reference to the assembly or assemblies containing FooChild1 and FooChild2. In the original code, you create a type by explicitly naming FooChild1 and FooChild2, so you might as well just new them instead of activating them.
Does this make sense to you?
我认为正确的是:
希望能帮到你
I think the correct is:
Hope that can help you