Check if Object is Dictionary or List

2019-02-08 10:33发布

Working with .NET 2 in mono, I'm using a basic JSON library that returns nested string, object Dictionary and lists.

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 object is a Dictionary or a List. Below is the method I'm using to perform this test, but was wondering if theres a cleaner way?

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 litJson but the JsonMapper class essentially doesn't work on iOS, hence the reason I am writing my own mapper.

4条回答
孤傲高冷的网名
2楼-- · 2019-02-08 10:58

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:

public virtual Type GetGenericTypeDefinition()

Here is the link: 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<>));
}
查看更多
三岁会撩人
3楼-- · 2019-02-08 10:59

If you want to check that a certain object is of some type, use the is operator. For example:

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 is operator directly where you need it.

查看更多
Juvenile、少年°
4楼-- · 2019-02-08 11:01

Use the is keyword and reflection.

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<,>));
}
查看更多
仙女界的扛把子
5楼-- · 2019-02-08 11:14

If you just need to detect the object is List/Dictionary or not, you can use 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);
查看更多
登录 后发表回答