How to know if a property is a type of List?


How to know if a property is a type of List<MyClass>?



I have this in these classes.


public class MyClass:BaseClass
{ }

public class BaseClass
{ }

public class CollectionClass
{
public string SomeProperty {get; set;}

public List<MyClass> Collection {get; set;}
}



In my code I want to find out if the property in some object (e. g. CollectionClass) is a type of List<BaseClass> also I want to return true if the property is a type of List<MyClass>. The code below explains that.


CollectionClass


List<BaseClass>


List<MyClass>


public bool ContainsMyCollection(object obj)
{
foreach(var property in obj.GetType().GetProperties())
{
// Idk how to accomplish that
if(property isTypeof List<BaseClass>)
return true;
}
return false
}





I wrote an answer, but I think I misunderstood so I've now deleted it. You understand that List<MyClass> isn't a derived type of List<BaseClass>, right? List<BaseClass> a = new List<MyClass>() won't work, for example.
– john
Jul 3 at 7:07



List<MyClass>


List<BaseClass>


List<BaseClass> a = new List<MyClass>()





if(property.PropertyType == typeof(List<BaseClass>)) but it's not clear what you actually want to achieve
– vc 74
Jul 3 at 7:09



if(property.PropertyType == typeof(List<BaseClass>))





Maybe better to explain why you want this rather than what you want.
– TheGeneral
Jul 3 at 7:17




1 Answer
1



You need to check if you have a closed type of List<>. This can be done like so:


List<>


if(property.PropertyType.IsGenericType
&& property.PropertyType.GetGenericTypeDefinition() == typeof(List<>))



and then you have to check if the generic argument (the T part of List<T>) is assignable to your base type:


T


List<T>


if (typeof(BaseClass).IsAssignableFrom(property.PropertyType.GetGenericArguments()[0]))



Putting that together, you get this:


public bool ContainsMyCollection(object obj)
{
foreach(var property in obj.GetType().GetProperties())
{
// Idk how to accomplish that
if(property.PropertyType.IsGenericType
&& property.PropertyType.GetGenericTypeDefinition() == typeof(List<>)
&& typeof(BaseClass).IsAssignableFrom(property.PropertyType.GetGenericArguments()[0]))
{
return true;
}
}
return false;
}



Note that, as explained in the comments, List<MyClass> isn't derived from List<BaseClass>, even if MyClass is derived from BaseClass. So, for example, List<BaseClass> a = new List<MyClass>(); will fail. That falls outside the scope of your question, but I figured I'd give you the heads up in case you didn't already know.


List<MyClass>


List<BaseClass>


MyClass


BaseClass


List<BaseClass> a = new List<MyClass>();






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

PHP contact form sending but not receiving emails