Skip to content

Instantly share code, notes, and snippets.

@adjames
adjames / EdmLibSample
Created August 27, 2012 20:03
EdmLib load an IEdmModel
static IEdmModel GetEdmModel(string url)
{
IEdmModel model = null;
IEnumerable<EdmError> errors = null;
using (var reader = XmlTextReader.Create(url))
{
if (EdmxReader.TryParse(reader, out model, out errors))
{
return model;
}
@adjames
adjames / GenericConstructorHelper.cs
Created February 4, 2012 07:29
Workaround for inability to create C# generic constraints for the presence of a constructor that takes parameters... i.e. Foo<T>(string name) where T: new(string)
public Func<T1, TResult> CompileConstructor<T1, TResult>()
{
var type1 = typeof(T1);
var parameter1 = Expression.Parameter(type1);
var constructor = typeof(TResult).GetConstructor(new Type[] { type1 });
var body = Expression.New(constructor, parameter1);
var lambda = Expression.Lambda<Func<T1, TResult>>(body, parameter1);
var method = lambda.Compile();
return method;
}