How to get class from ObservableCollection

BitSmithy 1,751 Reputation points
2021-12-15T17:24:59.43+00:00

Hello,

I have an object wich is type of ObservableCollection<T>

var oc = new ObservableCollection<MyClass>();

How to get know what type uses ObservableCollection (what type is T parameter)?

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,856 Reputation points
    2021-12-16T01:26:42.547+00:00

    Hello,
    Welcome to Microsoft Q&A!

    For your requirement, you could examine Generic Types with Reflection. For more info please refer the following code. And here is official document that you could refer to.

    var oc = new ObservableCollection<MyClass>();  
    var toc = oc.GetType();  
    Type[] typeParameters = toc.GetGenericArguments();  
    var ctype = typeParameters[0];  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful