简体   繁体   English

如何为通用类创建工厂

[英]How to create Factory for Generic Class

I have next interface 我有下一个界面

public interface IProperty<T>
{       
    T Data { get; set; }
}

public abstract class SomeAbsProperty<T> : IProperty<T> where T : class
{
    protected SomeAbsProperty(int param1) {}
    public abstract T GetData();
    public I Data { get; set; }
}

And I have list of childres classes that based on SomeAbsProperty class they looks like (simple example) 我有一个基于它们看起来像SomeAbsProperty类的childres类的列表(简单示例)

public sealed class ChildrenProperties : SomeAbsProperty<SomeClasss>
{
    public ChildrenProperties(int param1):base(param1) {}
    public override object GetData()
    {
        return new SomeClasss()
    }
}

I would like to have some factory that would build specific class based on some type 我想有一些工厂可以根据某种类型建立特定的类

public static class MyFactory
{
    public static SomeAbsProperty<T> CreateObject<T>(PropertyName property) where T : class
    {
        switch (property)
        {
            case PropertyName.p1:
                return new ChildrenProperties1(siteSettings, packageDateContext);
            case PropertyName.p2:
                return new ChildrenProperties(siteSettings, packageDateContext);
            case PropertyName.p3:
                return new ChildrenProperties2(siteSettings, packageDateContext);
            case PropertyName.p4:
                return new ChildrenProperties3(siteSettings, packageDateContext);
            default:
                return null;
        }
    }
}

but compelator can't convert my clases to SomeAbsProperty what would be correct behavior here ? 但是强制程序无法将我的句柄转换为SomeAbsProperty,这是正确的行为吗?

You can use as casting to SomeAbsProperty<T> generic class, something like 您可以将其asSomeAbsProperty<T>泛型类的强制转换,例如

return new ChildrenProperties(10) as SomeAbsProperty<T>;

Of-course you must be sure that ChildrenProperties is indeed SomeAbsProperty (which you know it is if you wrote base classes and factory class). 当然,您必须确保ChildrenProperties确实是SomeAbsProperty(如果您编写了基类和工厂类,便知道该属性)。 You can not use explicit compile time casting. 您不能使用显式编译时强制转换。

Edit: Maybe its better if factory which creates instances only depends on generic parameter (this will work only if all specializations have different parameter T; I'm not sure if that is your situation). 编辑:如果创建实例的工厂仅依赖于通用参数,则可能会更好(这仅在所有专门化参数都具有不同参数T的情况下才有效;我不确定这是否是您的情况)。 Something like: 就像是:

        public static SomeAbsProperty<T> CreateObject<T>() where T : class
        {
            Type type = typeof(T);
            if (type == typeof(object))
            {
                return new ChildrenProperties() as SomeAbsProperty<T>;
            }
            else if (type == typeof(string))
            {
                return new ChildrenPropertiesString() as SomeAbsProperty<T>;
            }
            else
            {
                return null;
            }
        }

... then you can call factory with something like: ...然后您可以使用以下方式致电工厂:

SomeAbsProperty<object> h = MyFactory.CreateObject<object>();
Console.WriteLine(h.GetType().ToString());
SomeAbsProperty<string> h2 = MyFactory.CreateObject<string>();
Console.WriteLine(h2.GetType().ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM