76

我有一个存储序列化值和类型的类。我想要一个属性/方法返回已经转换的值:

public String Value { get; set; }

public Type TheType { get; set; }

public typeof(TheType) CastedValue { get { return Convert.ChangeType(Value, typeof(_Type)); }

这在 C# 中可能吗?

4

3 回答 3

110

如果包含该属性的类是泛型的,并且您使用泛型参数声明该属性,则这是可能的:

class Foo<TValue> {
    public string Value { get; set; }
    public TValue TypedValue {
        get {
            return (TValue)Convert.ChangeType(Value, typeof(TValue));
        }
    }
}

另一种方法是使用通用方法:

class Foo {
    public string Value { get; set; }
    public Type TheType { get; set; }

    public T CastValue<T>() {
         return (T)Convert.ChangeType(Value, typeof(T));
    }
}

您还可以使用System.ComponentModel.TypeConverter类进行转换,因为它们允许类定义它自己的转换器。

编辑:请注意,调用泛型方法时,必须指定泛型类型参数,因为编译器无法推断它:

Foo foo = new Foo();
foo.Value = "100";
foo.Type = typeof(int);

int c = foo.CastValue<int>();

您必须在编译时知道类型。如果您在编译时不知道类型,则必须将其存储在 中object,在这种情况下,您可以将以下属性添加到Foo类中:

public object ConvertedValue {
    get {
        return Convert.ChangeType(Value, Type);
    }
}
于 2008-11-07T06:04:26.040 回答
60

属性、事件、构造函数等不能是通用的——只有方法和类型可以是通用的。大多数时候这不是问题,但我同意有时这很痛苦。布兰农的回答提供了两种合理的解决方法。

于 2008-11-07T06:25:33.580 回答
5

我不相信你在这里给出的例子是可能的。CastedValue 的类型必须在编译时定义,这意味着它不能依赖于运行时值(TheType 属性的值)。

编辑:布兰农的解决方案对于如何使用通用函数而不是属性来处理这个问题有一些好主意。

于 2008-11-07T06:05:05.397 回答