避免使用泛型和自定义数据列表进行转换

时间:2013-04-06 21:01:02

标签: c# .net generics interface

我有以下代码支持不同类型的列表:

public enum eType
{
    tInt,
    tString,
    tDateTime
}

public interface ICustomType<out T>
{
    T Value { get; }
}

public abstract class DifferentType
{
    protected DifferentType(eType type, string mnemonic)
    {
        Type = type;
        Mnemonic = mnemonic;
    }

    public string Mnemonic { get; private set; }

    public eType Type { get; private set; }
}

public class DateTimeType : DifferentType, ICustomType<DateTime>
{
    public DateTimeType(DateTime value, string mnemonic)
        : base(eType.tDateTime, mnemonic)
    {
        Value = value;
    }

    public DateTime Value { get; private set; }
}

public class IntType : DifferentType, ICustomType<int>
{
    public IntType(int value, string mnemonic)
        : base(eType.tInt, mnemonic)
    {
        Value = value;
    }

    public int Value { get; private set; }
}

public class StringType : DifferentType, ICustomType<string>
{
    public StringType(string value, string mnemonic)
        : base(eType.tString, mnemonic)
    {
        Value = value;
    }

    public string Value { get; private set; }
}

public static class UtilValue
{
    public static T GetValue<T>(DifferentType customType)
    {
        return ((ICustomType<T>)customType).Value;
    }
}

public class testTypes2
{
    public testTypes2()
    {
        var values = new List<DifferentType> { GetInt(), GetString(), GetDate() };

        foreach (var i in values)
        {
            switch (i.Type)
            {
                case eType.tInt:
                    int resInt = UtilValue.GetValue<int>(i);
                    break;

                case eType.tString:
                    string resString = UtilValue.GetValue<string>(i);
                    break;

                case eType.tDateTime:
                    DateTime resDateTime = UtilValue.GetValue<DateTime>(i);
                    break;
            }
        }
    }

    private DateTimeType GetDate()
    {
        return new DateTimeType(new DateTime(2000, 1, 1), "MnemonicDate");
    }

    private IntType GetInt()
    {
        return new IntType(5, "MnemonicInt");
    }

    private StringType GetString()
    {
        return new StringType("ok", "MnemonicString");
    }
}

并且想避免return ((ICustomType<T>)customType).Value;课程中UtilValue行的演员表,我还知道如何在保持设计的同时摆脱它?

我甚至不确定这种演员阵容是否昂贵?我的猜测肯定是

1 个答案:

答案 0 :(得分:1)

访客模式示例:

interface IDifferentTypeVisitor
{
    void Visit(DateTimeType dt);
    void Visit(StringType st);
}

class DifferentType
{
    public abstract void Accept(IDifferentTypeVisitor visitor);
}

class DateTimeType : DifferentType
{
    public void Accept(IDifferentTypeVisitor visitor)
    {
        visitor.Visit(this);
    }
}

class StringType : DifferentType
{
    public void Accept(IDifferentTypeVisitor visitor)
    {
        visitor.Visit(this);
    }
}

class SomeVisitor : IDifferentTypeVisitor
{
    public void Visit(DateTimeType dt)
    {
        //DateTime resDateTime = dt.Value; Or similar
    }

    public void Visit(StringType st)
    {
        //string resString = st.Value; Or similar
    }
}

public class testTypes2
{
    public testTypes2()
    {
        var values = new List<DifferentType> { /* Content */ };
        var visitor = new SomeVisitor();

        foreach (var i in values)
        {
            i.Accept(visitor);
        }
    }
}

在使用dynamic的C#4中,可以通过将此代码添加到DifferentType来保存一些代码:

public void Accept(IDifferentTypeVisitor visitor)
{
    visitor.Visit((dynamic)this);
}

然后删除所有其他Accept方法。它会伤害性能,但看起来更好; - )