mercredi 17 mars 2021

How to reflection check nullable object is IDictionary

How to reflection check nullable object is IDictionary

e.g :

if object is not null it can use is keyword to check.

void Main()
{
    Dictionary<string, object> v = new Dictionary<string,object>();
    Console.WriteLine(CheckValueIsIDictionary(v)); //true
}

bool CheckValueIsIDictionary(object value){
    return value is IDictionary;
}

but if parameter value is null then will get false

void Main()
{
    Dictionary<string, object> v = null;
    Console.WriteLine(CheckValueIsIDictionary(v)); //false
}

bool CheckValueIsIDictionary(object value){
    return value is IDictionary;
}

I expect get true result, thanks!


update:

I want to check value declare type is a IDictionary

before example you can know this null value is from Dictionary<string,object> and this type is a IDictionary and I want to get the relationship result.

and why I'd like to do it :

I want to check values is IDictionary or not to do different logic, object value can be Array or Dictionary<int,int> or List





Aucun commentaire:

Enregistrer un commentaire