What is the TargetedPatchingOptOut attribute and when does it apply?

I did not find the attribute documentation on MSDN , I was probably looking bad.

    1 answer 1

    This attribute allows NGen to inline the methods outside the assembly.

    What could be the problem and why methods between assemblies are not inline in ngen by default? Imagine this situation:

    1. There is build A, which calls method M from build B.
    2. NGin inline method M in build A and saved the binary to its cache.
    3. Build B has been updated - and the implementation of method M has changed.
    4. Assembly A is launched ... and the old version of method M is called, while in GAC a new version of the assembly has been lying for a long time.

    Therefore, NGen, unlike the JIT compiler, does not allow inline methods between assemblies unless it is explicitly allowed to do this.


    This attribute applies when the following conditions are met:

    1. use of ngen.exe is assumed;
    2. the method is available from other assemblies (public or protected method in a public class of a library);
    3. the method is rather short;
    4. the method will never change.

    If you do not use ngen.exe, but the usual JIT compiler, then this attribute is not needed, because the assembly cannot change during execution and the problem of obsolete cached code does not exist.

    For methods that are not accessible from the outside, the attribute is also not needed - the internal method is not a problem.

    With the third condition, everything should also be clear - large methods are not inline anyway, and the attribute is useless for them.

    The fourth condition is the most restrictive. You can bypass it if you clear the binaries of all dependent assemblies when the library changes. This solution is suitable for internal libraries of the project (if the project is always reinstalled entirely, then you should not think about the problem of cached obsolete methods).