Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>how about this:</p> <pre><code> class ExpandableQueryAttribute : Attribute { private LambdaExpression someLambda; //ctor public ExpandableQueryAttribute(Type hostingType, string filterMethod) { someLambda = (LambdaExpression)hostingType.GetField(filterMethod).GetValue(null); // could also use a static method } } </code></pre> <p>this should let you assign your lambda to a field and then suck it in at runtime, although in general I would prefer to use something like PostSharp to do this at compile time.</p> <p><strong>simple usage example</strong></p> <pre><code> public class LambdaExpressionAttribute : Attribute { public LambdaExpression MyLambda { get; private set; } //ctor public LambdaExpressionAttribute(Type hostingType, string filterMethod) { MyLambda = (LambdaExpression)hostingType.GetField(filterMethod).GetValue(null); } } public class User { public bool IsAdministrator { get; set; } } public static class securityExpresions { public static readonly LambdaExpression IsAdministrator = (Expression&lt;Predicate&lt;User&gt;&gt;)(x =&gt; x.IsAdministrator); public static readonly LambdaExpression IsValid = (Expression&lt;Predicate&lt;User&gt;&gt;)(x =&gt; x != null); public static void CheckAccess(User user) { // only for this POC... never do this in shipping code System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(); var method = stackTrace.GetFrame(1).GetMethod(); var filters = method.GetCustomAttributes(typeof(LambdaExpressionAttribute), true).OfType&lt;LambdaExpressionAttribute&gt;(); foreach (var filter in filters) { if ((bool)filter.MyLambda.Compile().DynamicInvoke(user) == false) { throw new UnauthorizedAccessException("user does not have access to: " + method.Name); } } } } public static class TheClass { [LambdaExpression(typeof(securityExpresions), "IsValid")] public static void ReadSomething(User user, object theThing) { securityExpresions.CheckAccess(user); Console.WriteLine("read something"); } [LambdaExpression(typeof(securityExpresions), "IsAdministrator")] public static void WriteSomething(User user, object theThing) { securityExpresions.CheckAccess(user); Console.WriteLine("wrote something"); } } static void Main(string[] args) { User u = new User(); try { TheClass.ReadSomething(u, new object()); TheClass.WriteSomething(u, new object()); } catch(Exception e) { Console.WriteLine(e); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload