Attributes that take parameterinfo or methodinfo

*** typeof( x.y ) gives you propertyInfo/MethodInfo


We are making all sorts of cool framework things, using reflection and attributes.  They are all great, but we’ve found a few places where we want to specify properties/methods in other classes, as arguments to attributes – (for instance – the value of this calculated field depends on the value of this other field).  It would be really nice if the typeof operator could return a methodinfo/propertyinfo object!


To do this we would have to extend the attribute scheme to support new kinds of information, and that would require all the languages to add support. I think it would take a fairly compelling argument to do that. I don’t think that changing Type to also be able to point to methods or properties is feasible.

Comments (3)

  1. I already thought about this case, but instead of allowing Type to point to methods and properties, one could add "memberof(" and get MemberInfo object from the attribute. This doesn’t require anything from language designers, it does require some work from CLR point of view. Consider artifical example:

    class MyClass



    public string Name { get { … } set {…} }

    protected bool ValidateName()


    return (Name != null) && Name.Length > 0;



    Some framework could use this attribute to automatically validate properties after they are changed.

    However, there is a problem with methods of same name and different signatures…

  2. Jason G says:


    That seems more like it’s an AOP concept. You might be better off looking at Extensible C# or something that can introduce Aspect-ness to your code.

    I think you can still hack together what you want though. You would just have to include enough information for your attribute’s constructor to use reflection to find the proper memberinfo.