开发者

Lambda expressions automated packing

开发者 https://www.devze.com 2023-01-08 03:56 出处:网络
public void aMethod<T>(Expression<Func<T, object>> lambda) { ...... } aMethod<User>(x=> x.UserId)
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 :-)

0

精彩评论

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