Activator.CreateInstance()烦恼 [英] Activator.CreateInstance() troubles

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

问题描述

我有一个工厂,它应该创建在运行时继承自Foo类的对象。我认为System.Activator.CreateInstance的返回类型与它创建的对象的类型相同,但从以下错误消息判断,其返回类型是Object。



< blockquote>

错误1无法将类型object隐式转换为cs_sandbox.Foo。显式转换存在(是否缺少转换?)F:\projects\cs_sandbox\Form1.cs 46 24 cs_sandbox


OK,所以也许我缺少一个转换,但

  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 {}

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);
}
}
}

如何修复此代码?

解决方案

或者,如果不可修复,还有什么其他方法来创建在运行时继承自特定类的对象?您需要将返回的对象转换为 Foo 类型。将它转换为在变量中定义的类型没有意义。它应该是编译器知道的,因为通过继承层次结构的整个点通过满足编译器的静态类型检查。

  return (Foo)System.Activator.CreateInstance(t); 

有一个通用版本, System.Activator.CreateInstance< T& code>,它创建一个已知的类型(不是类型变量,而是类型参数或静态已知的类型,在后一种情况下,它没有什么意义):

  return System.Activator.CreateInstance< FooChild1>(); 


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?

解决方案

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.

return (Foo)System.Activator.CreateInstance(t);

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):

return System.Activator.CreateInstance<FooChild1>();

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

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