C# 是否可以拥有对象的集合<;T>;上课?

C# 是否可以拥有对象的集合<;T>;上课?,c#,C#,我有一个要放入列表或集合的对象。有没有一种方法可以在不指定T的情况下实现这一点 我想这样做:List 以下是供参考的对象: internal class CommonProperty<T> { public string Name { get; set; } public PropType Type { get; set; } public List<T> PossibleValues { get; set; } private T _va

我有一个要放入列表或集合的对象。有没有一种方法可以在不指定T的情况下实现这一点

我想这样做:
List

以下是供参考的对象:

internal class CommonProperty<T>
{
    public string Name { get; set; }
    public PropType Type { get; set; }
    public List<T> PossibleValues { get; set; }
    private T _value;
    public T Value
    {
        get { return _value; }
        set
        {
            if (!_value.Equals(value))
            {
                _value = value;
            }
        }
    }
}
内部类CommonProperty
{
公共字符串名称{get;set;}
公共PropType类型{get;set;}
公共列表可能值{get;set;}
私人T_值;
公共价值
{
获取{返回_值;}
设置
{
如果(!\u值等于(值))
{
_价值=价值;
}
}
}
}

不,不能使用这样的开放泛型类型

T
已经是类型参数的上下文中,可以有一个
列表

public class Foo<T>
{
    static void Bar()
    {
        // This is fine, but is not what you're looking for - it uses
        // the type parameter T as the type argument
        List<CommonProperty<T>> list = new List<CommonProperty<T>>();
    }
}
公共类Foo
{
静态空心条()
{
//这很好,但不是你想要的-它使用
//类型参数T作为类型参数
列表=新列表();
}
}
通常,这里的解决方案是具有非泛型基类或接口,泛型类或接口源自:

// Consider making it abstract
internal class CommonProperty
{
    public string Name { get; set; }
    public PropType Type { get; set; }
}

internal class CommonProperty<T> : CommonProperty
{
    public List<T> PossibleValues { get; set; }
    private T _value;
    public T Value
    {
        get => _value;
        set
        {
            // TODO: Is this really necessary?
            if (!_value.Equals(value))
            {
                _value = value;
            }
        }
    }
}
<代码> /考虑将其抽象化 内部类公共属性 { 公共字符串名称{get;set;} 公共PropType类型{get;set;} } 内部类CommonProperty:CommonProperty { 公共列表可能值{get;set;} 私人T_值; 公共价值 { 获取=>\u值; 设置 { //托多:这真的有必要吗? 如果(!\u值等于(值)) { _价值=价值; } } } } 然后,您可以创建一个
列表
。。。尽管要知道,您完全有可能最终得到一个不是
公共属性的元素


列表中
可以检索所有属性的名称和类型,但是如果不强制转换到特定类型,这些值将不可用。在基类中可以有一个
抽象对象值{get;set;}
属性,该属性随后在派生类中被重写,但不清楚这在您的用例中是必要的还是有用的。

不,您不能使用这样的开放泛型类型

T
已经是类型参数的上下文中,可以有一个
列表

public class Foo<T>
{
    static void Bar()
    {
        // This is fine, but is not what you're looking for - it uses
        // the type parameter T as the type argument
        List<CommonProperty<T>> list = new List<CommonProperty<T>>();
    }
}
公共类Foo
{
静态空心条()
{
//这很好,但不是你想要的-它使用
//类型参数T作为类型参数
列表=新列表();
}
}
通常,这里的解决方案是具有非泛型基类或接口,泛型类或接口源自:

// Consider making it abstract
internal class CommonProperty
{
    public string Name { get; set; }
    public PropType Type { get; set; }
}

internal class CommonProperty<T> : CommonProperty
{
    public List<T> PossibleValues { get; set; }
    private T _value;
    public T Value
    {
        get => _value;
        set
        {
            // TODO: Is this really necessary?
            if (!_value.Equals(value))
            {
                _value = value;
            }
        }
    }
}
<代码> /考虑将其抽象化 内部类公共属性 { 公共字符串名称{get;set;} 公共PropType类型{get;set;} } 内部类CommonProperty:CommonProperty { 公共列表可能值{get;set;} 私人T_值; 公共价值 { 获取=>\u值; 设置 { //托多:这真的有必要吗? 如果(!\u值等于(值)) { _价值=价值; } } } }
然后,您可以创建一个
列表
。。。尽管要知道,您完全有可能最终得到一个不是
公共属性的元素


列表中
可以检索所有属性的名称和类型,但是如果不强制转换到特定类型,这些值将不可用。在基类中可以有一个
抽象对象值{get;set;}
属性,该属性随后在派生类中被重写,但不清楚这在您的用例中是必要的还是有用的。

我认为最接近的方法是定义一个接口来匹配一个未类型化的(特定的)
CommonProperty
,使用
对象而不是
T
。然后让您的
CommonProperty
实现该接口。现在,您可以在列表中使用该界面


但这并不好。你会失去很多好的类型检查,并不得不做更多的铸造。如果这是使用这些对象的主要方式,那么拥有一个泛型类就没有什么意义了。

我认为最接近的方法是使用
对象而不是
T
定义一个接口来匹配一个未键入的(特定的)
CommonProperty
。然后让您的
CommonProperty
实现该接口。现在,您可以在列表中使用该界面


但这并不好。你会失去很多好的类型检查,并不得不做更多的铸造。如果这是使用这些对象的主要方式,那么拥有泛型类就没有什么意义了。

不可能将使用不同类型参数实例化的泛型混合放入同一个集合中。这样的集合无论如何都不会有用,因为调用者需要在编译时为每个项提供
T

假设你可以做你想做的事

// imagine that you could do this
List<CommonProperty<T>> mixedList = GetAllProperties(); 
换言之,这样的清单将无法使用

另一方面,特定类型的属性列表将非常有用:

List<CommonProperty<string>> stringPropList = GetPropertiesOfType<string>();
foreach (CommonProperty<string> prop in stringPropList ) {
    ...
}

不可能将使用不同类型参数实例化的泛型混合放入同一个集合中。这样的集合无论如何都不会有用,因为调用者需要在编译时为每个项提供
T

假设你可以做你想做的事

// imagine that you could do this
List<CommonProperty<T>> mixedList = GetAllProperties(); 
换言之,这样的清单将无法使用

另一方面,特定类型的属性列表将非常有用:

List<CommonProperty<string>> stringPropList = GetPropertiesOfType<string>();
foreach (CommonProperty<string> prop in stringPropList ) {
    ...
}

OrderedDictionary?OrderedDictionary?你好,Servy,我看到你还在享受你玩的愚蠢游戏。祝你好运,伙计,你好