I am looping through the properties of an object and need to find out is the property is an class or multiple classes (enumerable, collection or list) and inherit a class of x and i am a little stuck, any help would be appriciated.
I am looping through the properties of an object and need to find out is the property is an class or multiple classes (enumerable, collection or list) and inherit a class of x and i am a little stuck, any help would be appriciated.
public class Base
{
public int Id { get; set; }
}
public class Test:Base
{
public Test2 Test2 { get; set; }
public List<Test2> Tests { get; set; }
}
public class Test2
{
public string Data { get; set; }
}
2.Test:
public void Testmethod()
{
var obj = new Test();
foreach (var property in obj.GetType().GetProperties())
{
//for complex types
if (property.PropertyType.IsClass&&!typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
Console.WriteLine("{0} is a class", property);
}
if(typeof(IEnumerable).IsAssignableFrom(property.PropertyType))
{
Console.WriteLine("{0} is List class", property);
}
//if it is a subclass
if(typeof(Test).IsSubclassOf(typeof(Base)))
{
Console.WriteLine("{0} is inherits from Base", property);
}
}
}
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Participant
1861 Points
2836 Posts
is a property an object, list and inherit from x class
Mar 15, 2020 06:52 PM|EnenDaveyBoy|LINK
Hi
I am looping through the properties of an object and need to find out is the property is an class or multiple classes (enumerable, collection or list) and inherit a class of x and i am a little stuck, any help would be appriciated.
All-Star
53081 Points
23648 Posts
Re: is a property an object, list and inherit from x class
Mar 15, 2020 07:41 PM|mgebhard|LINK
I think you are looking for an extension method.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/how-to-implement-and-call-a-custom-extension-method
All-Star
58234 Points
15673 Posts
Re: is a property an object, list and inherit from x class
Mar 15, 2020 09:49 PM|bruce (sqlwork.com)|LINK
you use the is operator:
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is
also as core supports c# 8, you can use the new pattern matching:
https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching
Participant
1861 Points
2836 Posts
Re: is a property an object, list and inherit from x class
Mar 15, 2020 11:06 PM|EnenDaveyBoy|LINK
thanks for the replie, sorry rushed the op. have tried to us is but I am looping through the properties
do i have to check if its all enumerable and collection and List, or is there one i can check for them all?
can i use
if (prop == typeof(MyInheitedClassName))
{
}
Contributor
2690 Points
874 Posts
Re: is a property an object, list and inherit from x class
Mar 16, 2020 05:45 AM|Rena Ni|LINK
Hi EnenDaveyBoy,
Here is a working demo like below:
1.Model:
2.Test:
Reference:
https://stackoverflow.com/a/8699063/11398810
https://stackoverflow.com/a/37844503/11398810
Best Regards,
Rena
Participant
1861 Points
2836 Posts
Re: is a property an object, list and inherit from x class
Mar 16, 2020 02:13 PM|EnenDaveyBoy|LINK
thanks for the info, really helped, only bit i changed for my own future reference.