Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type parameter to variable

Tags:

c#

asp.net

c#-4.0

how to getValue of property class with this method?

public static int SQLInsert<TEntity>(TEntity obj) where TEntity : class
{
    foreach (var item in obj.GetType().GetProperties())
    {
        //item.GetValue(?,null);
    }
    return 1;
}
like image 730
hamid Avatar asked Apr 18 '26 03:04

hamid


1 Answers

item will be a PropertyInfo. You'd use:

object value = item.GetValue(obj, null);

Note that you're pretty much ignoring the TEntity type parameter at the moment. You may want to use:

foreach (var property in typeof(TEntity).GetProperties())

That way if someone calls

SQLInsert<Customer>(customer)

and the value of customer actually refers to a subclass of Customer with extra properties, only the properties of Customer will be used.

like image 165
Jon Skeet Avatar answered Apr 20 '26 18:04

Jon Skeet