0 out of 1 rated this helpful Rate this topic

LoaderOptimizationAttribute Class

Used to set the default loader optimization policy for the main method of an executable application.

System.Object
  System.Attribute
    System.LoaderOptimizationAttribute

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[AttributeUsageAttribute(AttributeTargets.Method)]
[ComVisibleAttribute(true)]
public sealed class LoaderOptimizationAttribute : Attribute

The LoaderOptimizationAttribute type exposes the following members.

  Name Description
Public method LoaderOptimizationAttribute(Byte) Initializes a new instance of the LoaderOptimizationAttribute class to the specified value.
Public method LoaderOptimizationAttribute(LoaderOptimization) Initializes a new instance of the LoaderOptimizationAttribute class to the specified value.
Top
  Name Description
Public property TypeId When implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Public property Value Gets the current LoaderOptimization value for this instance.
Top
  Name Description
Public method Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IsDefaultAttribute When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)
Top
  Name Description
Explicit interface implemetation Private method _Attribute.GetIDsOfNames Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetation Private method _Attribute.GetTypeInfo Retrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetation Private method _Attribute.GetTypeInfoCount Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetation Private method _Attribute.Invoke Provides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

The LoaderOptimizationAttribute can only be set on the main method for an executable application and is ignored on all other methods.

The LoaderOptimizationAttribute attribute is specified with a LoaderOptimization enumeration value, which tells the loader what type of application to optimize for: SingleDomain, MultiDomain, or MultiDomainHost.

This attribute is only a hint to the loader and does not affect program behavior.

Note Note

If custom code access security policy is set for the AppDomain, by using the SetAppDomainPolicy property, and the AppDomain is created using the MultiDomain flag, the effect is the same as specifying the MultiDomainHost flag; that is, only assemblies in the GAC are shared. When this occurs, the loader does not throw an exception and the application does not experience the performance gains associated with the MultiDomain flag.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(2000 characters remaining)
Community Content Add
Annotations FAQ
Useful with WPF in AppDomains
I've found that if you load assemblies that use WPF into an app-domain, they take a long time to load even though you've already loaded them in the primary app-domain.

The default behavior is to load them again in each app domain that uses them, and for some reason this is much slower than doing the same with Windows Forms.

This can be overcome by placing [LoaderOptimization(LoaderOptimization.MultiDomainHost)] on the main method. The WPF assemblies are all signed, so they'll be shared by all app-domains and not reloaded, and they start up much faster then.

The downside, of course, is that this affects all signed assemblies, and none of them can be unloaded if you do this.
LoaderOptimization + ClickOnce + WPF = Crash
There's some sort of nasty interaction here; if you  use this attribute to change the optimization setting on a WPF app, and then deploy with ClickOnce, your program will then crash on startup (or very shortly thereafter).

If you do this with a trivial WPF application, you die with a XamlParseException. In more complex cases, I've found it leaves an event-log message that says it had an "internal error", and crashed with error code 80131506; I believed that works out to a "Fatal Execution Engine Error", which isn't an exception anymore.

The best workaround I've found is to avoid this attribute, and instead set the LoaderOptimization property in the AppDomainSetup object when creating an app-domain. This doesn't quite do the same thing- the app-domains don't seem to share assemblies with the primary domain, but they do share with each other, which is something.