0

I have a method in C# which I need to pass the ObservableCollection to thaat method. I need to get the properties and their values in class T. However, my class T can be any class name.

e.g.:

public class MyClass 
{ 
    public string prop1 {get; set;}  
    public prop2 {get; set; }   
}

public class OtherClass
{   
    public string OtherProp1 {get; set;}  
    public OtherProp2 {get; set;}  
}

private ObservableCollection<MyClass> _myselectedItems = new ObservableCollection<MyClass>();

public ObservableCollection<MyClass> MySelectedItems
{  
    get{return _myselectedItem;}
    set{_myselectedItem = value;}  
}

private ObservableCollection<OtherClass> _otherselectedItems = new ObservableCollection<OtherClass>();

public ObservableCollection<OtherClass> OtherSelectedItems
{  
    get{return _otherselectedItem;}
    set{_otherselectedItem = value;}   
}

public GenericMethod<T>(ObservableCollection<T> anySelectedItems)
{
   if (anySelectedItems[0].**prop1** != "Hello")
   {   // do something
   }
}

I have this generic method that I want to call.

----> here I want to be able to get to the corresponding properties of the class (e.g. prop1, prop2, otherProp1, or OtherProp2) based on where this method is called and type of anySelectedItems. If the pass value is ObservableCollection<MyClass> then I need to get the prop1 and prop2.

Any suggestion is appreciated. Thanks.

Always23
  • 15
  • 4
  • Possible duplicate of [Get property of generic class](https://stackoverflow.com/questions/14129421/get-property-of-generic-class) – Bruno Avelar Sep 29 '17 at 15:07
  • ******NOTE: Correction of last sentence: If the passed value is ObservableCollection then I need to get the prop1 and prop2. – Always23 Sep 29 '17 at 15:07
  • @Kitty23 dont correct in the comments, edit your post – maccettura Sep 29 '17 at 15:08
  • Why are you making a generic method to immediately make it non-generic? Why not make an interface that defines the class must have prop1 and prop2, Then make the generic only accept that interface. You won't have to deal with this at that point. – Blast_dan Sep 29 '17 at 15:12
  • @Blast_dan I have many different classes with their own properties and values. But I want to have a method that I can call and get the corresponding property and value. I don't know how? – Always23 Sep 29 '17 at 15:14
  • @Kitty23 the way you are describing the problem doesn't really make sense. with the method signature you have you are saying that method should be able to work on any type, but it will break as soon as you pass something in that doesn't follow the type you expect. If you want a purely dynamic type you could consider using the dynamic keyword instead of using Generics. – Blast_dan Sep 29 '17 at 15:22

2 Answers2

0

As per my comment I would recommend an apporach as follows. Let the interface do the work for you.

public interface IMyClass {
    string prop1 {get; set;}
    string prop2 {get; set;}  
}

public class MyClass : IMyClass
{ 
    public string prop1 {get; set;}  
    public string prop2 {get; set; }   
}

public class OtherClass : IMyClass
{   
    public string prop1 {get; set;}  
    public string prop2 {get; set;}  
}

private ObservableCollection<MyClass> _myselectedItems = new ObservableCollection<MyClass>();

public ObservableCollection<IMyClass> MySelectedItems
{  
    get{return _myselectedItem;}
    set{_myselectedItem = value;}  
}

private ObservableCollection<OtherClass> _otherselectedItems = new ObservableCollection<OtherClass>();

public ObservableCollection<OtherClass> OtherSelectedItems
{  
    get{return _otherselectedItem;}
    set{_otherselectedItem = value;}   
}

public GenericMethod(ObservableCollection<IMyClass> anySelectedItems)
{
    if (anySelectedItems[0].prop1 != "Hello")
    {   // do something
    }
}
Blast_dan
  • 1,135
  • 9
  • 18
  • why you have changed public ObservableCollection MySelectedItems { get{return _myselectedItem;} set{_myselectedItem = value;} } to public ObservableCollection MySelectedItems { get{return _myselectedItem;} set{_myselectedItem = value;} } chenged MyClass to IMyClass? – Always23 Sep 29 '17 at 15:28
  • So it works against the interface or the description of the class instead of the class itself. That way the method doesn't care what class it's given as long as the class implements the IMyClass interface. – Blast_dan Sep 29 '17 at 15:33
0

I did create one single class with prop1 and prop2. I passed the type of that class to my ObservableCollections<> and then I create different instances of that class and passed it into my generic method.

Always23
  • 15
  • 4