Skip to content

Instantly share code, notes, and snippets.

@DexterHaslem
Last active August 29, 2015 14:07
Show Gist options
  • Save DexterHaslem/b5d7bf4332b4d400167c to your computer and use it in GitHub Desktop.
Save DexterHaslem/b5d7bf4332b4d400167c to your computer and use it in GitHub Desktop.
/* paste these in top so LINQPad eats and adds to usings
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
*/
public class Program
{
private static string src =
@"
using System;
using System.ComponentModel;
namespace LoadMe
{
[TypeConverter(typeof(FooConverter))]
public class Foo
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
public class FooConverter : TypeConverter
{
// stuff
}
}
";
public static void Main()
{
var codeProvider = new CSharpCodeProvider(new Dictionary<string, string>{{ "CompilerVersion", "v4.0" }});
var compileParameters = new CompilerParameters(new[] { "System.dll" });
compileParameters.GenerateInMemory = true;
var compilerResults = codeProvider.CompileAssemblyFromSource(compileParameters, src);
if (compilerResults.Errors.Count == 0)
{
var fooType = compilerResults.CompiledAssembly.GetType("LoadMe.Foo");
Console.WriteLine(fooType.FullName + "::" + fooType.Assembly.FullName);
Console.WriteLine("Type converter type = '" + TypeDescriptor.GetConverter(fooType).GetType().FullName + "'");
}
else
{
foreach (var err in compilerResults.Errors)
Console.WriteLine(err);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment