Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get custom attributes form lambda expression

Suppose something like this :

public static IQueryable<T> Find<T>(IQueryable<T> query, string value, params Expression<Func<T, object>>[] subSelectors) where T : class
{
   foreach (var include in subSelectors)
   {
     var entityType = include.Body.Type.GetGenericArguments().First();
     var properties = from p in entityType.GetProperties()
                      where Attribute.IsDefined(p, typeof(FilterAttribute))
                      select p;
   }
}

This method is called from another assembly, exemple call of this method :

     var container = new List<MyClass>();
     var q = (from m in container
              select m).AsQueryable();
     SimpleFilter.Find(q, "something", m => m.Navigation);

For the T parameter is ok I see my custom attribute. But form the lambda expression I cant see my custom attribute.

like image 645
Francis Avatar asked Nov 21 '25 22:11

Francis


1 Answers

Assuming you just want to see if each selector specified has the attribute:

var member = ((MemberExpression) include.Body).Member;
bool hasAttribute = Attribute.IsDefined(member, typeof (FilterAttribute));

it isn't clear how you intend to plug that into the rest of the Find method, but I think that covers the main thrust of the issue.

like image 88
Marc Gravell Avatar answered Nov 24 '25 15:11

Marc Gravell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!