To make your app faster and smaller, the .NET Native compiler performs whole program optimizations that employ static analysis to model the program behavior. The optimizations are similar to those traditionally used when compiling languages like C++. One of the optimizations done by the .NET Native compiler is the removal of types, methods, constructors, properties, events, and fields that are not used.
Since reflection is dynamic in nature, not all reflection use can be determined statically at the time of compilation. Because .NET Native does not presently have a just-in-time compiler to compile the pieces that are missing, everything that can be accessed at runtime needs to be known at the time of compilation. Your app will encounter a MissingMetadataException or MissingRuntimeArtifactException if it attempts to use something that was removed.
If the use of reflection is not simple enough for the optimizer to see through, the .NET Native compiler needs your assistance in describing the use of reflection by means of a Runtime Directive.What are runtime directives?
Runtime Directives provide a way for you to describe what types and members your app needs to have available for reflection purposes. The directives are kept in an XML file with an RD.XML extension that is part of your application package. A default RD.XML file is added to all new universal app projects - look for it in the Properties node (My Project node for Visual Basic) of the Solution Explorer. You can add new runtime directives to it when needed.
If you're a library author that needs to describe the use of reflection within the library, you can embed a Runtime Directives files in your library. Simply add a file with an RD.XML extension to your library project, right-click the file in the Solution Explorer window and select Properties. Set the Build Action property to Embedded Resource and you're good to go.
To learn more about the RD.XML syntax, see RD.XML Reference.
The builder below will help you create a directive that will solve the MissingMetadataException you're facing. Use the information in the MissingMetadataException message along with your knowledge of the code that hit the exception to pick the right answers. If the MissingMetadataException message doesn't have helpful information, make sure to rebuild in Debug mode with .NET Native optimization enabled.
A single type
There's no pattern. A single type with the following name:
All subtypes of a type
All types that implement following interface or derive from the following type:
All types with a custom attribute applied
All types that have the following attribute applied:
All types in a namespace
All types within the following namespace:
All types in an assembly
All types in the following assembly:
Use Activator.CreateInstance or ConstructorInfo.Invoke to construct new instances of the type.
Access members or activate
Access methods, constructors, properties, fields and events on the type using reflection. This is a superset of Activation.
Serialize object graph
Access properties, fields, and constructors on the type and the types of the fields and properties recursively. This is a superset of Activation.
Public types and members only
Types and members that are not public won't be explictly included by this rule. They might be included by other rules.
Public, private and internal members and types will be included.
This preview shows you the runtime directive corresponding to the choices
you made in the left column. If you're adding a directive to an existing RD.XML file,
append the directives under the
<Application> element to the
element of the existing RD.XML.
If you are authoring an RD.XML for a library, replace