Created
March 2, 2012 00:32
-
-
Save Crisfole/1954282 to your computer and use it in GitHub Desktop.
How to create an object in a foreign assembly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note this is useful for testing cross-assembly code