I’m trying to detect a property of ICollection where anything can be any TClass.
I have this but it wont detect unless I put the class as ICollection<Class>
which I would have to do for every type.
var properties = packet.GetType().GetProperties();
foreach (var property in properties)
{
if (property.PropertyType == typeof(string))
{
Console.WriteLine($"string {property.Name}");
}
else if (property.PropertyType == typeof(int))
{
Console.WriteLine($"int {property.Name}");
}
else if (property.PropertyType == typeof(ICollection))
{
Console.WriteLine($"collection {property.Name}");
}
}
If I have
public required ICollection<CatalogItem> Items { get; set; }
It wont detect unless I use typeof(ICollection<CatalogItem>)
New contributor
adam says is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.