我正在使用.NET4.5和C#我想创建扩展方法,允许我传递对象的属性,如果该对象的Id为0,则returnnull否则返回该属性值。我可以通过反射(reflection)来解决问题,所以请考虑更多的训练练习,而不是我试图解决实际问题。当前扩展方法位于static类中,如下所示:publicstaticobjectGetNullIfNotSet(thisWillAnsweranswer,Expression>expression){if(answer.Id==0)returnnull;returnexpression.Compile()();}我希望能够使用它的方式如下(答案类型为Wil