Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether an object is ObservableCollection or not?

Tags:

c#

c#-4.0

I want to check whether a value is of type ObservableCollection of any type ,or not in C# ?

eg: I can check whether a value is of string type or not as follows :

string value = "value to Check";
bool b = value.GetType().Equals(typeof(string));  // b =true

but If I need to check whether a value is ObservableCollection or not , irrespective of the constituent type , how can I do this ?

eg:

ObservableCollection<T> collection = new ObservableCollection<T>();

If I am checking like this

bool b = collection.GetType().Equals(typeof(ObservableCollection<>)); // b=false

How can I check whether the value is collection or not ??

like image 816
Abhishek Gahlout Avatar asked Dec 09 '22 12:12

Abhishek Gahlout


1 Answers

Try

bool b = collection.GetType().IsGenericType &&
           collection.GetType().GetGenericTypeDefinition() == typeof(ObservableCollection<>);
like image 141
dcastro Avatar answered Feb 11 '23 10:02

dcastro