POCO不是IDictionary或IEnumerable

时间:2013-11-09 13:44:19

标签: c# casting

我在我使用的自定义序列化程序中将POCO对象转换为IDictionary / IEnumerable。它已经使用了大约一年没有失败。但是我有一个类不能作为IDictionary / IEnumerable转换。以下所有都返回false:

(obj is IDictionary); // false
(obj is IList); // false
(obj is IEnumerable); // false

该类定义为:(名称已更改,有更多属性)

internal class MyClass {
   public int ID;
   public string Text;
}

是什么给出的?什么不能把一些对象作为IDictionaries?我需要一些方法来通过属性作为键/值存储,反射不是首选。

1 个答案:

答案 0 :(得分:3)

您的MyClass未实施IDictionary,因此无法投放到IDictionary。它也没有实现IEnumerableIList,因此它也不会强制转换为其中任何一个。