C# Access the Properties of a Generic Object
Posted
by Jimbo
on Stack Overflow
See other posts from Stack Overflow
or by Jimbo
Published on 2010-05-21T14:08:06Z
Indexed on
2010/05/21
14:10 UTC
Read the original article
Hit count: 750
I have a method that counts the number of Contacts each Supplier, Customer and Manufacturer has (this is a scenario to try make explaining easier!)
The models are all created by Linq to SQL classes. Each Supplier, Customer and Manufacturer may have one or more Contacts
public int CountContacts<TModel>(TModel entity) where TModel : class
{
return entity.Contacts.Count();
}
The above of course doesnt work, because the 'entity' is generic and doesnt know whether it has the property 'Contacts'. Can someone help with how to achieve this?
© Stack Overflow or respective owner