[英]Need help creating extension method in C# with generics and lambda expression
我将所有的高级功能都整合到了一起,但还没有使用 generics 或 lambda 表达式:
这是我要创建的方法的示例用法:
MyClass mc = null;
int x = mc.TryGetOrDefault(z => z.This.That.TheOther); // z is a reference to mc
// the code has not failed at this point and the value of x is 0 (int's default)
// had mc and all of the properties expressed in the lambda expression been initialized
// x would be equal to mc.This.That.TheOther's value
这是据我所知,但 Visual Studio 抱怨:
您还没有在TResult
您的方法通用。 你想要这样的东西:
public static TResult TryGetOrDefault<TSource, TResult>
(this TSource obj, Expression<Func<TSource, TResult>> expression)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.