Skip to content

Instantly share code, notes, and snippets.

@jarrettv
Last active August 29, 2015 14:07
Show Gist options
  • Save jarrettv/19ee88c0549f32dc8025 to your computer and use it in GitHub Desktop.
Save jarrettv/19ee88c0549f32dc8025 to your computer and use it in GitHub Desktop.
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CSharp;
namespace BbbAsmTest
{
class Program
{
static void Main(string[] args)
{
ListCurrentAssemblies();
TestLinq();
ListCurrentAssemblies();
TestCompilingCode();
ListCurrentAssemblies();
Console.ReadLine();
}
static void ListCurrentAssemblies()
{
Console.WriteLine("-----------------");
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var a in assemblies)
{
Console.WriteLine(a.Location);
}
Console.WriteLine("------------------Count={0}",assemblies.Length);
}
static void TestCompilingCode()
{
Console.WriteLine("Test compiling code");
var provider = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters
{
GenerateInMemory = true,
GenerateExecutable = false,
IncludeDebugInformation = false,
TreatWarningsAsErrors = false,
OutputAssembly = "test.dll",
};
bool relocateSystemAsm = false;
Type type = Type.GetType("Mono.Runtime");
if (type != null)
{
Console.WriteLine("Mono.Runtime");
MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
if (displayName != null)
{
Console.WriteLine(displayName.Invoke(null, null));
int major = 0;
if (int.TryParse(displayName.Invoke(null, null).ToString().Substring(0, 1), out major) && major > 2)
{
relocateSystemAsm = true;
}
}
}
Console.WriteLine("Relocate:" + relocateSystemAsm);
//cp.ReferencedAssemblies.Add("System.dll");
//cp.ReferencedAssemblies.Add("System.Core.dll");
//cp.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
var name = assembly.GetName();
if (name.Name.ToLower() == "system")
{
cp.ReferencedAssemblies.Add(assembly.Location);
}
else if (name.Name.ToLower() == "system.core")
{
cp.ReferencedAssemblies.Add(assembly.Location);
}
else if (name.Name.ToLower() == "microsoft.csharp")
{
cp.ReferencedAssemblies.Add(assembly.Location);
}
}
var code = @"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BbbAsmTest
{
public class Class1
{
public void Hello(IQueryable<string> strings)
{
strings.ToList().ForEach(x => Console.WriteLine(x));
}
}
}";
var results = provider.CompileAssemblyFromSource(cp, code);
foreach (var o in results.Output)
Console.WriteLine(o);
foreach (var o in results.Errors)
Console.WriteLine(o);
Console.WriteLine("Compile={0},{1}", results.NativeCompilerReturnValue, results.CompiledAssembly.GetName());
}
static void TestLinq()
{
var data = new[] { 1, 2, 3, 4 };
var avg = data.Where(x => x % 2 == 0).Average();
Console.WriteLine("Testing linq: " + avg.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment