开发者

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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜