I have 2 ObservableCollections, say of type class1 and class2.
private ObservableCollection<Class1> cOne; // collection of objects of type Class1
private ObservableCollection<Class1> cTwo; // collection of objects of type Class2
Now in the below method I want to iterate over this collections and access object's members.
public void MyMethod<T> (){
var listOfLayers = new ObservableCollection<T>();
if (typeof(T) == typeof(Class1))
{
listOfLayers = (T) cOne;
}
else{
listOfLayers = (T) cTwo;
}
foreach (var entry in listOfLayers){
WL entry.someprop;
}
}
But it throws me error every time about type conversion.
Error CS0030 Cannot convert type 'System.Collections.ObjectModel.ObservableCollection' to 'T'
Please note that I haven't created two methods based on type because both methods essentially going to do the same thing in separate database tables. And hence they both gonna contain the duplicate code.