Property / Method inlining and impact on Reflection
Asked Answered
G

4

9

My answer to one of the question on SO was commented by Valentin Kuzub, who argues that inlining a property by JIT compiler will cause the reflection to stop working.

The case is as follows:

class Foo
{
    public string Bar { get; set; }

    public void Fuzz<T>(Expression<Func<T>> lambda)
    {
    }
}

Fuzz(x => x.Bar);

Fuzz function accepts a lambda expression and uses reflection to find the property. It is a common practice in MVC in HtmlHelper extensions.

I don't think that the reflection will stop working even if the Bar property gets inlined, as it is a call to Bar that will be inlined and typeof(Foo).GetProperty("Bar") will still return a valid PropertyInfo.

Could you confirm this please or my understanding of method inlining is wrong?

Ganger answered 14/7, 2011 at 10:34 Comment(1)
Btw I found this interesting article that shows other reasons for not using lambdas in INotifyPropertyChange implementations blog.quantumbitdesigns.com/2010/01/26/…Rovner
H
3

JIT compiler operates at runtime and it can't rewrite metadata information stored in the assembly. And reflection reads assembly to access this metadata. So there are no impact from JIT-compiler to reflection.

EDIT: Actually there are couple of places when C# compiler itself "inlines" some information during compilation. For example, constants, enums and default arguments are "inlined" so you can't access them during reflection. But it definitely not related to your particular case.

Harlamert answered 14/7, 2011 at 10:44 Comment(2)
What do you mean by default properties?Abirritant
You can add enums to your list.Ganger
R
1

Yeah when I think about it more I guess only way inlining properties could fail INotifyPropertyChanged interface correct work would be if you were using a reflection based method used like

public Count
{
get {return m_Count;}
 set { m_Count=value;
      GetCurrentPropertyNameUsingReflectionAndNotifyItChanged();}
}

If used like you suggest indeed metadata exists in assembly and property name will be successfully taken from there.

Got us both thinking though.

Rovner answered 14/7, 2011 at 11:0 Comment(4)
how exactly? method will be called not from property, but from method that is using property, and result name will not be Count, but something different ( most likely a method name, instead of property name)Rovner
Is your method GetCurrentPropertyNameUsingReflectionAndNotifyItChanged analysing the call stack?Ganger
yes. as you can see nothing is passed to it. It is a generic reflection based approach that would be nice if no inlining could ever happen. Implement one method in viewmodel base, call it from all properties and enjoy..Rovner
Then you have to mark your properties with MethodImplAttribute to stop inlining.Ganger
M
0

I personally agree with @Sergey:

Considering that inlining happens on JIT compiler side, but metadata generated before, it shouldn't inpact on reflection in any way. By the way, good question, like it +1

Mackle answered 14/7, 2011 at 10:48 Comment(0)
H
0

Expression trees can't be in-lined anyway since they are a representation of the expression (abstract syntax tree) rather than the expression itself.

Delegates, even if they can be in-lined, will still carry the data about the method and target being called in their properties.

Hernandez answered 14/7, 2011 at 11:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.