繁体   English   中英

通用类工厂问题

[英]Generic class factory problem

贝娄是我的代码的简化版本:

public interface IControl<T>
{
    T Value { get; }
}

public class BoolControl : IControl<bool>
{
    public bool Value
    {
        get { return true; }
    }
}

public class StringControl : IControl<string>
{
    public string Value
    {
        get { return ""; }
    }
}
public class ControlFactory
{
    public IControl GetControl(string controlType)
    {
        switch (controlType)
        {
            case "Bool":
                return new BoolControl();
            case "String":
                return new StringControl();
        }
        return null;
    }
}

问题出在ControlFactory类的GetControl方法中。 因为它返回IControl而我只有IControl <T>这是一个通用接口。 我不能提供T,因为在Bool情况下它会bool而在String情况下它将是字符串。

知道我需要做些什么来使它工作?

只需从IControl派生IControl<T>

public interface IControl<T> : IControl
{
    T Value { get; }
}

UPDATE

如果我错过了你的理解,并且你不想要非泛型接口,那么你也必须使方法GetControl()通用。

public IControl<T> GetControl<T>()
{
    if (typeof(T) == typeof(Boolean))
    {
        return new BoolControl(); // Will not compile.
    }
    else if (typeof(T) == typeof(String))
    {
        return new StringControl(); // Will not compile.
    }
    else
    {
        return null;
    }
}

现在您遇到的问题是新控件无法隐式转换为IControl<T> ,您必须明确这一点。

public IControl<T> GetControl<T>()
{
    if (typeof(T) == typeof(Boolean))
    {
        return new (IControl<T>)BoolControl();
    }
    else if (typeof(T) == typeof(String))
    {
        return (IControl<T>)new StringControl();
    }
    else
    {
        return null;
    }
}

UPDATE

将演员as IControl<T>as IControl<T>更改为(IControl<T>) 这是首选,因为如果存在错误,它将导致异常,而as IControl<T>静默返回null

public IControl<T> GetControl<T>()
{
    switch (typeof(T).Name)
    {
        case "Bool":
            return (IControl<T>) new BoolControl();
        case "String":
            return (IControl<T>) new StringControl();
    }
    return null;
}

更新; 纠正了代码中的几个错误。 接下来打电话来上课:

IControl<bool> boolControl = GetControl<bool>();

返回类型必须是通用的,因为它确实是。 想想你将如何使用它。 返回强类型对象不需要通用工厂方法。

即使你能做到,也会有什么好处

IControl<bool> boolControl = controlFactory.GetControl("bool");

或者,那个可行的,

IControl<bool> boolControl = controlFactory.GetControl<bool>("bool");

特定的

IControl<bool> boolControl = controlFactory.GetBoolControl("bool");

无论哪种方式,您在客户端都有switch()。 返回一个对象,或者有一个非类型的IControl接口。

暂无
暂无

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

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