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 ??
Try
bool b = collection.GetType().IsGenericType &&
collection.GetType().GetGenericTypeDefinition() == typeof(ObservableCollection<>);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With