Skip to content

Instantly share code, notes, and snippets.

@Crisfole
Created March 2, 2012 00:32
Show Gist options
  • Save Crisfole/1954282 to your computer and use it in GitHub Desktop.
Save Crisfole/1954282 to your computer and use it in GitHub Desktop.
How to create an object in a foreign assembly
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
dynamic foreignMember = CSharpCodeProvider
.CreateProvider("CSharp")
.CompileAssemblyFromSource(
new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false,
IncludeDebugInformation = false
},
new[]
{
"public static class Test { public static object GetModel() { return new { Name = new { Last = \"Nancy\" }}; } }"
})
.CompiledAssembly
.GetType("Test")
.GetMethod("GetModel")
.Invoke(null, null);
@Crisfole
Copy link
Author

Note this is useful for testing cross-assembly code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment