C# 检查对象是字典还是列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17190204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if Object is Dictionary or List
提问by user1711383
Working with .NET 2 in mono, I'm using a basic JSONlibrary that returns nested string, object Dictionary and lists.
在单声道中使用 .NET 2,我使用一个基本JSON库,它返回嵌套字符串、对象字典和列表。
I'm writing a mapper to map this to a jsonData class that I already have and I need to be able to determine if the underlying type of an objectis a Dictionary or a List. Below is the method I'm using to perform this test, but was wondering if theres a cleaner way?
我正在编写一个映射器来将它映射到我已经拥有的 jsonData 类,我需要能够确定 an 的基础类型object是字典还是列表。下面是我用来执行此测试的方法,但想知道是否有更清洁的方法?
private static bool IsDictionary(object o) {
try {
Dictionary<string, object> dict = (Dictionary<string, object>)o;
return true;
} catch {
return false;
}
}
private static bool IsList(object o) {
try {
List<object> list = (List<object>)o;
return true;
} catch {
return false;
}
}
The library I'm using is litJsonbut the JsonMapperclass essentially doesn't work on iOS, hence the reason I am writing my own mapper.
我正在使用的库是litJson但JsonMapper该类本质上在 iOS 上不起作用,因此我正在编写自己的映射器。
采纳答案by Dustin Kingen
Use the iskeyword and reflection.
使用is关键字和反射。
public bool IsList(object o)
{
if(o == null) return false;
return o is IList &&
o.GetType().IsGenericType &&
o.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>));
}
public bool IsDictionary(object o)
{
if(o == null) return false;
return o is IDictionary &&
o.GetType().IsGenericType &&
o.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(Dictionary<,>));
}
回答by svick
If you want to check that a certain object is of some type, use the isoperator. For example:
如果要检查某个对象是否属于某种类型,请使用is运算符。例如:
private static bool IsDictionary(object o)
{
return o is Dictionary<string, object>;
}
Though for something this simple, you probably don't need a separate method, just use the isoperator directly where you need it.
尽管对于这么简单的事情,您可能不需要单独的方法,只需is在需要的地方直接使用运算符即可。
回答by TheMiddleMan
Modifying the above answer. In order to use GetGenericTypeDefinition()you must preface the method with GetType(). If you look at MSDN this is how GetGenericTypeDefinition()is accessed:
修改上面的答案。为了使用,GetGenericTypeDefinition()您必须在方法前加上GetType()。如果您查看 MSDN,这GetGenericTypeDefinition()是访问方式:
public virtual Type GetGenericTypeDefinition()
Here is the link: https://msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition(v=vs.110).aspx
这是链接:https: //msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition(v=vs.110).aspx
public bool IsList(object o)
{
return o is IList &&
o.GetType().IsGenericType &&
o.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List<>));
}
public bool IsDictionary(object o)
{
return o is IDictionary &&
o.GetType().IsGenericType &&
o.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(Dictionary<>));
}
回答by Mehdi Dehghani
If you just need to detect the object is List/Dictionaryor not, you can use myObject.GetType().IsGenericType && myObject is IEnumerable
如果你只需要检测物体是否List/Dictionary存在,你可以使用myObject.GetType().IsGenericType && myObject is IEnumerable
Here is some example:
下面是一些例子:
var lst = new List<string>();
var dic = new Dictionary<int, int>();
string s = "Hello!";
object obj1 = new { Id = 10 };
object obj2 = null;
// True
Console.Write(lst.GetType().IsGenericType && lst is IEnumerable);
// True
Console.Write(dic.GetType().IsGenericType && dic is IEnumerable);
// False
Console.Write(s.GetType().IsGenericType && s is IEnumerable);
// False
Console.Write(obj1.GetType().IsGenericType && obj1 is IEnumerable);
// NullReferenceException: Object reference not set to an instance of
// an object, so you need to check for null cases too
Console.Write(obj2.GetType().IsGenericType && obj2 is IEnumerable);

![C# 带有字符串数组的列表 (List<string[]>)](/res/img/loading.gif)