Type parameter to variable
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().GetPrope开发者_C百科rties())
{
//item.GetValue(?,null);
}
return 1;
}
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.
item.GetValue(obj, null);
that would work
Like this, instead of indexer you can use 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;
}
精彩评论