Lambda expressions automated packing
public void aMethod<T>(Expression<Func<T, object>> lambda)
{
......
}
aMethod<User>(x=> x.UserId)
User is my custom class, members of his UserId int type...
I want the lambda expression x => x.UserId
But in the way,开发者_Go百科 I get is x => Convert (x.UserId)
Therefore, the following operations can not, I need to ensure that my expression type Expression <Func <T, object>>
it is not defined as Expression <Func <T, int>>
Is there any way it?
public void aMethod<T,TMember>(Expression<Func<T, TMember>> lambda)
{
......
}
aMethod<User>(x=> x.UserId)
object is a reference type. int a value type. if you get an int as an object, it must be packed :-)
精彩评论