如何在C#中创建/设计一个可以在运行时具有不同类型成员的类

时间:2013-02-06 21:08:20

标签: c#

我的情况是需要一个类,它需要包含有关在运行时变化的信息的信息,例如:

class Info<T>
{
    public T Max { get; set; }
    public T Min { get; set; }
    public T DefaultValue { get; set; }
    public T Step { get; set; }
    // Some other stuff
}

我必须在字典中存储此类的许多实例,但问题是使用字典我必须声明一种类型,例如。

Dictionary<string, Info<int>> dict = new Dictionary<string, Info<int>>();

在这种情况下,我无法添加其他类型的信息,例如Info<double>。 我想要类似的东西,我在下面的例子中删除了通用版本。

 {"Price", new Info{Min=100,Max=1000,DefaultValue=200,Step=50}}
 {"Adv", new Info{Min=10.50,Max=500.50,DefaultValue=20.50,Step=1.5}}
 {"Answer", new Info{Min=false,Max=false,DefaultValue=false,Step=false}}

我可以使用Dictionary<string, Object> dict = new Dictionary<string, Object>();

但是当我拿回dict项目时我不知道那是什么类型,我也需要知道类型,例如对于Price它是int而对于Adv它是双倍的,我将如何在运行时知道它?

其实我想创建一个验证器(我正在使用.Net Compact Framework 3.5 /不能使用任何内置系统,如果它存在的话)例如如果我有类似下面的类..

class Demo
{
    public int Price { get; set; }
    public float Adv { get; set; }

    public static bool Validate(Demo d)
    {
        List<string> err = new List<string>();
        // here I have to get Info about the Price
        // from dictionary, it can be any storage
        Info priceInfo = GetPriceInfo("Price");
        if (d.Price < priceInfo.Min)
        {
            d.Price = priceInfo.Min;
            err.Add("price is lower than Min Price");
        }
        if (d.Price > priceInfo.Max)
        {
            d.Price = priceInfo.Max;
            err.Add("price is above than Max Price");
        }
        // need to do similar for all kinds of properties in the class  
    }
}

所以想法是将验证信息存储在一个地方(在字典或其他地方),然后在验证时使用该信息,我也想知道我是否可以更好地设计上述场景?

也许有更好的方法可以做到这一点,任何指导方针都可以吗?

2 个答案:

答案 0 :(得分:4)

您可以使用非通用基类:

public abstract class Info {
}

public class Info<T> : Info {
}

现在所有不同的泛型类型都继承自相同的基类型,因此您可以在字典中使用它:

Dictionary<string, Info> dict = new Dictionary<string, Info>();

您可以定义接口不依赖于基类中的泛型类型的属性和方法,并在泛型类中实现它们。这样您就可以在不指定泛型类型的情况下使用它们。

对于需要类型的方法,您需要为每种类型指定特定代码。您可以使用isas运算符来检查类型:

Info<int> info = dict[name] as Info<int>;
if (info != null) {
  int max = info.Max;
}

答案 1 :(得分:0)

Keith Nicholas是对的 - 如果你希望你的词典支持多种类型,你需要一个界面,但它需要是一个generic one

尝试这样的事情(警告:未经测试的代码):

interface IInfo<T>
{
    T Max { get; set; }
    T Min { get; set; }
    T DefaultValue { get; set; }
    T Step { get; set; }
}

Dictionary<string, IInfo> dict = new Dictionary<string, IInfo>();

class AnswerInfo : IInfo<bool> { }
class PriceInfo : IInfo<int> { }
class AdvInfo : IInfo<double> { }

dict["Answer"] = new AnswerInfo() { Min = false, Max = false, DefaultValue = false, Step = false };
dict["Price"] = new PriceInfo() { Min = 100, Max = 1000, DefaultValue = 200, Step = 50 };
dict["Adv"] = new AdvInfo() { Min = 10.50, Max = 500.50, DefaultValue = 20.50 Step = 1.5 };