I'm pulling all of the advanced features together for this one, but haven't worked with generics or lambda expressions very much:
Here's example usage of the method I want to create:
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
Here's as far as I've gotten, but Visual Studio is complaining:
You haven't made your method generic in TResult
. You want something like:
public static TResult TryGetOrDefault<TSource, TResult>
(this TSource obj, Expression<Func<TSource, TResult>> expression)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.