Skip to content

Quick Start

scottyboy805 edited this page Nov 11, 2021 · 2 revisions

Trivial CLR provides runtime reflection types under the 'TrivialCLR.Reflection' namespace that can be used to find and access/invoke members as you would normally. As a result, you should find it quite intuitive to find types and members, as well as executing code by invoking methods.

Before you can do any of that though, you will need to create an 'AppDomain' instance and load one or more modules (assembly images):

// Create an app domain
AppDomain domain = new AppDomain();

// Load an assembly module
CLRModule module = domain.LoadModuleStream(File.OpenRead("MyAssembly.dll"), false);

The AppDomain is the heart of the interpreter, and is where all loaded modules and types are stored. You will usually have one AppDomain instance per application.

Note that module dependencies are not loaded automatically. You will need to either manually resolve and load dependencies, or ensure that any referenced assemblies are already loaded in the native CLR, in which case they will be detected and treated as interop (external) calls.

Once you have loaded a managed assembly module, usage is just like standard C# reflection, only the code will be interpreted instead of JIT compiled and executed:

// Find an entry type
Type mainType = module.GetType("Program");

// Find an entry method
MethodInfo mainMethod = mainType.GetMethod("Main", BindingFlags.Public | BindingFlags.Static);

// Invoke the main method
mainMethod.Invoke(null, new object[] { Environment.GetCommandLineArgs() });
Clone this wiki locally