Enter the parameter into the variable

How to getValue a property class using this method?

public static int SQLInsert<TEntity>(TEntity obj) where TEntity : class
{
    foreach (var item in obj.GetType().GetProperties())
    {
        //item.GetValue(?,null);
    }
    return 1;
}
+3
source share
3 answers
item.GetValue(obj, null);

which would work

+1
source

itemwill be PropertyInfo. You should use:

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

Note that you pretty much ignore the type parameter TEntityat the moment. You can use:

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

So if someone calls

SQLInsert<Customer>(customer)

and the value customeractually refers to a subclass customerwith additional properties, only properties will be used customer.

+5
source

Similarly, instead of an indexer, you can use a null value:

public static int SQLInsert<TEntity>(TEntity obj) where TEntity : class
{
    var indexer = new object[0];
    foreach (var item in obj.GetType().GetProperties())
    {
        item.GetValue(obj,indexer);
    }
    return 1;
}
0
source

All Articles