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开发者_C百科 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)
精彩评论