C# Compiler optimization - Unused methods

后端 未结 4 1085
Happy的楠姐
Happy的楠姐 2020-12-06 10:14

Does C# compiler (in VS2008 or VS2010) remove unused methods while compiling ?

I assume that it may have a problem deciding if public methods will ever be used, so I

4条回答
  •  天涯浪人
    2020-12-06 10:33

    The compiler doesn't strip any method from the assembly, public or private. I could in fact cause weird issues with reflection, and prevent runtime calls to such methods.

    There are a lot of frameworks (like the XAML parser) which enable you to call private methods without static bindings (think about OnClick="myFunction" in a XAML file) This markup will call the potentially private myFunction when the OnClick event is raised... But the compiler has no informations about such a behavior at compile time.

    Dynamic code suffer from the same issue, IL generation too. And you can access private methods from any object when executing under full trust.

提交回复
热议问题