C# 制作通用属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/271347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:58:12  来源:igfitidea点击:

Making a generic property

c#.netgenericsproperties

提问by

I have a class that stores a serialized value and a type. I want to have a property/method returning the value already casted:

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

public String Value { get; set; }

public Type TheType { get; set; }

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

Is this possible in C#?

这在 C# 中可能吗?

回答by Brannon

It's possible if the class containing the property is generic, and you declare the property using the generic parameter:

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

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

An alternative would be to use a generic method instead:

另一种方法是使用泛型方法:

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

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

You can also use the System.ComponentModel.TypeConverterclasses to convert, since they allow a class to define it's own converter.

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

Edit: note that when calling the generic method, you must specify the generic type parameter, since the compiler has no way to infer it:

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

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

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

You have to know the type at compile time. If you don't know the type at compile time then you must be storing it in an object, in which case you can add the following property to the Fooclass:

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

public object ConvertedValue {
    get {
        return Convert.ChangeType(Value, Type);
    }
}

回答by Charlie

I don't believe the example you've given here is possible. The type of CastedValue has to be defined at compile time, which means it can't depend on a runtime value (the value of the TheType property).

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

EDIT: Brannon's solution has some good ideas for how to handle this using a generic function rather than a property.

编辑:Brannon 的解决方案对于如何使用通用函数而不是属性来处理这个问题有一些很好的想法。

回答by Jon Skeet

Properties, events, constructors etc can't be generic - only methods and types can be generic. Most of the time that's not a problem, but I agree that sometimes it's a pain. Brannon's answer gives two reasonable workarounds.

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