C#:派生类上具有不同返回类型的属性

alo*_*oko 7 c# polymorphism abstract-class overriding

我试图寻找这个问题的答案,但找不到太多,很可能是因为我不知道如何正确地寻找它,所以这就是它.非常感谢所有帮助.

使用看起来像的基类

abstract public class Property
{
    private String name;

    public Property(String propertyName)
    { 
        name = propertyName; 
    }

    public String Name
    {
        get { return name; }
    }

    abstract public override String ToString();
}
Run Code Online (Sandbox Code Playgroud)

和派生类看起来像

public class StringProperty : Property
{
    private String value; // different properties for different types

    public StringProperty(String propertyName, String value) : base(propertyName)
    {
        this.value = value;
    }

    public String Value // different signature for different properties
    {
        get { return value; }
    }

    public override String ToString()
    {
        return base.Name + ": " + value;
    }
}
Run Code Online (Sandbox Code Playgroud)

在运行时,该函数接收"Property"对象的集合.我需要做些什么来获得每个的"价值"?我是否需要有一个大的if语句来查询每个"Property"对象的类型?如果没有,是否有更优雅的解决方案?

我试图定义一个抽象的"Value"属性来覆盖,但由于返回类型不同,它不起作用.我也试过玩"价值"属性,但我不能让它工作.使用类似COM的变体的想法也听起来不太合适.

非常感谢提前.

编辑:

我应该补充一下我要做什么的细节.属性显示在Winforms应用程序中.不同的"TextBox"表示不同的属性,并且过滤以获得适当的输入(取决于类型).读回并存储更新的值.容器对象将被序列化为JSON并在Android和iPhone客户端上反序列化,最终这些值将传递到运行OpenGL内容的本机C++代码的层中.我事先并不知道所有需要的属性,所以作为中间人,我想让我的代码尽可能健壮,同时能够提供OpenGL引擎.

seh*_*ehe 13

您可以使用泛型类:

public class AnyProperty<T> : Property
{
    private T value;
    // ... etc
Run Code Online (Sandbox Code Playgroud)

我真的建议现在将基类作为接口:

public interface IProperty
{
    public String Name { get; }
}

public class Property<T> : IProperty
{
    public Property(String name, T value)
    {
        Name = name;
        Value = value;
    }

    public String Name { get; private set; }
    public T Value { get; private set; }

    public override String ToString()
    {
        return string.Format("{0}: {1}", Name, Value)
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是示例用法:

var intProp = new Property<int>       ("age", 32);    
var strProp = new Property<string>    ("name", "Earl");    
var enumProp = new Property<ColorEnum> ("eye color", ColorEnum.Magenta);    
Run Code Online (Sandbox Code Playgroud)

为了使构造更简单,您可以使用工厂方法:

public static Property<T> MakeProperty(string name, T value)
{
    return new Property<T>(name,value);
}

var intProp = MakeProperty("age", 32);    
var strProp = MakeProperty("name", "Earl");    
var enumProp = MakeProperty("eye color", ColorEnum.Magenta);    
Run Code Online (Sandbox Code Playgroud)

不一定推荐,有点OT:

你可以使用扩展方法使它变得更加有趣:

public static Property<T> AsProp<T>(this T value, string name)
{
    return new Property<T>(name,value);
}

var intProp = 32.AsProp("age");
var strProp = "Earl".AsProp("name");
var enumProp = ColorEnum.Magenta.AsProp("eye color");
Run Code Online (Sandbox Code Playgroud)


Ry-*_*Ry- 4

您必须简单地使用该object类型。你想达到什么目的?这里的问题不是类的结构,而是接收对象集合的函数Property。甚至无法将某些内容转换为未知类型,因为您不知道它需要存储在什么类型的变量中。

所以基本上,您的Property.Value财产需要属于类型object。在使用对象的方法中Property,您需要对它们做一些事情,而您所做的事情将决定它应该如何构造。您正在打印值吗?有一个*Value从抽象PropertyValue类继承的类并重写ToString()以返回适当的字符串表示形式。