Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较基类中的类型_C#_Generics_Inheritance - Fatal编程技术网

C# 比较基类中的类型

C# 比较基类中的类型,c#,generics,inheritance,C#,Generics,Inheritance,我有一个部分基类 class Part { public PartType Type { get; set; } } 有许多实现 class Wire : Part { } 我的程序中有一个树视图。当我单击其中的一个元素时,我希望列表中填充我在树视图中单击的类型的所有部分 当我打开多个列表时,我只希望那些列表加载与我在树视图中单击的零件类型相同的零件 class BasePartListViewModel<T> : ListViewModel where T : Pa

我有一个
部分
基类

class Part {
    public PartType Type { get; set; }
}
有许多实现

class Wire : Part {   }
我的程序中有一个树视图。当我单击其中的一个元素时,我希望列表中填充我在树视图中单击的类型的所有部分

当我打开多个列表时,我只希望那些列表加载与我在树视图中单击的零件类型相同的零件

class BasePartListViewModel<T> : ListViewModel where T : Part {
    protected override void OnTreeSelectionChanged(PartType type)
        if (type == PartType.Wire) {
            //load the Wires from the DB and display them
        } 
        else {
            //ignore the changed type event
        }
    }
}
有点像

if (_type == T.Type)
但这当然行不通。还有其他方法吗?

如果执行此操作。GetType()将作为BasePartListViewModel`1[Wire]返回

理想情况下,您不应该在基类中引用它

如果执行此操作。GetType()将作为BasePartListViewModel`1[Wire]返回


理想情况下,您不应该在基类中引用它

由于零件类型是设计类类型的静态信息(对吗?),您可以使用属性来存储它:

[AttributeUsage(AttributeTargets.Class)]
public class PartTypeAttribute : Attribute
{
    public readonly PartType PartType;

    public PartTypeAttribute(PartType partType)
    {
        PartType = partType;
    }
}
然后将其应用于子类:

[PartType(PartType.Wire)]
class Wire : Part
{
}
然后在
BasePartListViewModel
类的静态构造函数中,可以获得相应的值:

class BasePartListViewModel<T> : ListViewModel
    where T : Part
{
    private static PartType PartTypeOfT;

    static BasePartListViewModel()
    {
        var attr = typeof(T).GetCustomAttributes(typeof(PartTypeAttribute), true)
            .FirstOrDefault() as PartTypeAttribute;
        if (attr != null)
            PartTypeOfT = attr.PartType;
    }

    protected override void OnTreeSelectionChanged(PartType type)
    {
        if (type == PartTypeOfT) {
            ....
        }
    }
}
class BasePartListViewModel:ListViewModel
其中T:部分
{
私有静态PartType PartTypeOfT;
静态BasePartListViewModel()
{
var attr=typeof(T).GetCustomAttributes(typeof(PartTypeAttribute),true)
.FirstOrDefault()作为PartTypeAttribute;
如果(attr!=null)
PartTypeOfT=attr.PartType;
}
受保护的覆盖无效OnTreeSelectionChanged(PartType类型)
{
if(type==PartTypeOfT){
....
}
}
}

由于零件类型是设计类类型的静态信息(对吗?),您可以使用属性来存储它:

[AttributeUsage(AttributeTargets.Class)]
public class PartTypeAttribute : Attribute
{
    public readonly PartType PartType;

    public PartTypeAttribute(PartType partType)
    {
        PartType = partType;
    }
}
然后将其应用于子类:

[PartType(PartType.Wire)]
class Wire : Part
{
}
然后在
BasePartListViewModel
类的静态构造函数中,可以获得相应的值:

class BasePartListViewModel<T> : ListViewModel
    where T : Part
{
    private static PartType PartTypeOfT;

    static BasePartListViewModel()
    {
        var attr = typeof(T).GetCustomAttributes(typeof(PartTypeAttribute), true)
            .FirstOrDefault() as PartTypeAttribute;
        if (attr != null)
            PartTypeOfT = attr.PartType;
    }

    protected override void OnTreeSelectionChanged(PartType type)
    {
        if (type == PartTypeOfT) {
            ....
        }
    }
}
class BasePartListViewModel:ListViewModel
其中T:部分
{
私有静态PartType PartTypeOfT;
静态BasePartListViewModel()
{
var attr=typeof(T).GetCustomAttributes(typeof(PartTypeAttribute),true)
.FirstOrDefault()作为PartTypeAttribute;
如果(attr!=null)
PartTypeOfT=attr.PartType;
}
受保护的覆盖无效OnTreeSelectionChanged(PartType类型)
{
if(type==PartTypeOfT){
....
}
}
}

什么是
\u类型
?它是如何声明的?它是
PartType\u type
。枚举。这不太可能。@JoachimIsaksson:他想要一个枚举值。抽象(=基类)不应该依赖于细节。细节应该取决于抽象。什么是
\u类型
?它是如何声明的?它是
PartType\u type
。枚举。这不太可能。@JoachimIsaksson:他想要一个枚举值。抽象(=基类)不应该依赖于细节。细节应该取决于抽象。另外两个关于GetType的答案已经被删除。此问题与
系统无关。键入
-它与名为
类型
的自定义
PartType
属性有关。另外两个有关GetType的答案已被删除。此问题与
系统无关。Type
-它与一个名为
Type
的自定义
PartType
属性有关。看起来有可能。我去查一下,看来有可能。我去查一下。