c# - 使用通用属性创建基本属性类 'value'

标签 c#

我所做的是在 C# 中创建“Attribute”的基类。从那里我创建了其他类,它们继承属性并根据需要添加任何其他属性。然而,当我尝试创建包含所有这些不同属性的可观察集合时,我在这里得到下划线

private ObservableCollection<Attribute> _attributes;

在“属性”下说:使用泛型类型“Attribute< TValue >”需要一个类型参数。属性基类的原因是这样我可以创建多个属性,如下所示。

属性类

using System.Collections.Generic;

namespace ExampleTool.Model
{
    public class Attribute<TValue>
    {
        public string Key { get; set; }
        public TValue Value { get; set; }
    }

    public class FloatAttr : Attribute<float>
    {
        public string Label { get; set; }
        private float minValue { get; set; }
        private float maxValue { get; set; }
    }

    public class IntAttr : Attribute<int>
    {
        public string Label { get; set; }
        private float minValue { get; set; }
        private float maxValue { get; set; }
    }

    public class StringAttr : Attribute<string>
    {
        public string Label { get; set; }
    }

    public class BoolAttr : Attribute<bool>
    {
        public string Label { get; set; }
    }

    public class ListStringAttr : List<string>
    {
        public string Label { get; set; }
    }
}

ViewModel - 发生错误的地方...

using System.Collections.Generic;
using System.Collections.ObjectModel;
using ExampleTool.Model;
using ExampleTool.Helper;

namespace ExampleTool.ViewModel
{
    public class AttributeViewModel : ObservableObject
    {
        private ObservableCollection<Attribute> _attributes;
        public ObservableCollection<Attribute> Attributes
        {
            get { return _attributes; }
            set
            {
                _attributes = value;
                NotifyPropertyChanged("Attributes");
            }
        }

        public AttributeViewModel()
        {
            //hard coded data for testing
            Attributes = new ObservableCollection<Attribute>();

            FloatAttr floatAttr = new FloatAttr();
            Attributes.Add(floatAttr);

            IntAttr intAttr = new IntAttr();
            Attributes.Add(intAttr);

            StringAttr stringAttr = new StringAttr();
            Attributes.Add(stringAttr);

            BoolAttr boolAttr = new BoolAttr();
            Attributes.Add(boolAttr);

            ListStringAttr listStringAttr = new ListStringAttr();
            Attributes.Add(listStringAttr);
        }
    }
}

解决方案想法#1 - 只需从基类中删除 value 属性并在每个子类中定义它即可。

public class Attribute
    {
        public string Key { get; set; }
    }

    public class FloatAttr : Attribute
    {
        public float Value { get; set; }
        public string Label { get; set; }
        private float minValue { get; set; }
        private float maxValue { get; set; }
    }

    public class IntAttr : Attribute
    {
        public int Value { get; set; }
        public string Label { get; set; }
        private float minValue { get; set; }
        private float maxValue { get; set; }
    }

    public class StringAttr : Attribute
    {
        public string Value { get; set; }
        public string Label { get; set; }
    }

    public class BoolAttr : Attribute
    {
        public bool Value { get; set; }
        public string Label { get; set; }
    }

    public class ListStringAttr : Attribute
    {
        public List<string> Value { get; set; }
        public string Label { get; set; }
    }

最佳答案

您的基本属性类是泛型类型,那么您必须在其用法中添加类型参数。但你不能只添加 T:

private ObservableCollection<Attribute<T>> _attributes;

因为 T 不是您的类型参数。您应该添加新的非泛型基类:

public class AttributeBase
{
    public string Key { get; set; }
}

public class Attribute<TValue> : AttributeBase
{
    public TValue Value { get; set; }
}

并像 this question 那样实现 AttributeRetriever :

public Attribute<T> GetAttribute<T>() where T: DatabaseItem, new()
{
    return _attributes.OfType(typeof(Attribute<T>)).FirstOrDefault as Attribute<T>;
}

好消息是,您的 WPF View 无需类型参数即可正常工作,因为绑定(bind)使用反射。然后,如果您不需要在代码中访问属性,则也不需要实现检索器。

关于c# - 使用通用属性创建基本属性类 'value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34310922/

相关文章:

c# - 获取 IP 地址而不是 MAC 地址

c# - 此SqlTransaction已完成;它不再可用。 - 无法弄清楚出了什么问题

c# - ViewModel 的 POST 和 DomainModel 上的验证

c# - 用于下载大文件的 Asmx Web 服务

c# - 将文件写入响应后关闭弹出窗口

c# - Linq OrderBy 子列表

c# 从 xsi :schemaLocation attribute value 获取 .xsd 的本地路径

c# - Excel DNA 打包问题

c# - !(x is null) 和 x is object 之间有区别吗

c# - 如何选择对包含多行 LIST 的条目使用 LINQ?