0

我正在尝试查看是否可以在比较中在变量中使用 System.Type。我有以下代码:

    internal ObservableCollection<FREQUENCY> GetFrequencies(System.Type equipmenttype)
    {
        ... 
        foreach (var incident in query)
        {

            if (typeof(equipmenttype).IsSubclassOf(typeof(incident)))
            {
                foreach (var freq in incident.FREQUENCY)
                {
                    freqs.Add(freq);
                }
            }
        }
        return freqs;
    }

但是变量 'tmp' 和 'equipmenttype' 拉出错误“找不到类型或命名空间名称 'tmp'(您是否缺少 using 指令或程序集引用?)”

我知道这通常通过 typeof(MYCLASS) 来使用,但我很好奇这是否可以使用 System.Type 的变量,或者是否有任何方法可以做到这一点。谢谢。

4

2 回答 2

4

我看不到tmp你的代码在哪里。但确定你错过了这个

if (typeof(equipmenttype).IsSubclassOf(typeof(incident)))

应该

if (equipmenttype.IsSubclassOf(incident.GetType()))

typeof运算符用于获取RuntimeType类型的。但是你已经到了RuntimeType这里equipmenttype,所以你不需要在typeof这里使用。

于 2013-09-25T16:11:41.167 回答
2

试试if (equipmenttype.IsSubclassOf(incident.GetType())equipmenttype已经是System.Type, 并且GetType()必须被调用以确定实例的类型。

于 2013-09-25T16:11:09.867 回答