I want to write an extension method that checks to see if an attribute is applied when calling a method, and I would like to specify the method as a lambda expression. I currently have the following (working) approach, but I really don't like what this code looks like:
public static bool HasAttribute<TAttribute, TDelegate>(this Expression<TDelegate> method)
Expression<Func<AccountController, LogInModel, string, ActionResult>> mut = (c, m, s) => c.LogIn(m, s);
mut.HasAttribute<ExportModelStateAttribute, Func<AccountController, LogInModel, string, ActionResult>>().ShouldBeTrue();
As you can see, I have to specify the delegate type twice, and not one time looks pretty ... I would like to have something more like
// Usage (if I had my way...)
var mut = (c, m, s) => c.LogIn(m, s);
mut.HasAttribute<ExportModelStateAttribute>().ShouldBeTrue();
but I understand that it may take too much.
Is there a way to reorganize type arguments from current code?
, TDelegate, , , , , void TDelegate . , ...
Update:
Jay , TDelegate type HasAttribute<>. :
Expression<Func<AccountController, LogInModel, string, ActionResult>> mut = (c, m, s) => c.LogIn(m, s);
mut.HasAttribute<ExportModelStateAttribute>().ShouldBeTrue();
, , . , ?