Skip to content

Instantly share code, notes, and snippets.

@jvanlangen
Created March 21, 2020 00:27
Show Gist options
  • Save jvanlangen/88cd145beac998758bc5434224367fab to your computer and use it in GitHub Desktop.
Save jvanlangen/88cd145beac998758bc5434224367fab to your computer and use it in GitHub Desktop.
Constructor generator using Expressions
// this delegate is just, so you don't have to pass an object array. _(params)_
public delegate object ConstructorDelegate(params object[] args);
public static ConstructorDelegate CreateConstructor(Type type, params Type[] parameters)
{
// Get the constructor info for these parameters
var constructorInfo = type.GetConstructor(parameters);
// define a object[] parameter
var paramExpr = Expression.Parameter(typeof(Object[]));
// To feed the constructor with the right parameters, we need to generate an array
// of parameters that will be read from the initialize object array argument.
var constructorParameters = parameters.Select((paramType, index) =>
// convert the object[index] to the right constructor parameter type.
Expression.Convert(
// read a value from the object[index]
Expression.ArrayAccess(
paramExpr,
Expression.Constant(index)),
paramType)).ToArray();
// just call the constructor.
var body = Expression.New(constructorInfo, constructorParameters);
var constructor = Expression.Lambda<ConstructorDelegate>(body, paramExpr);
return constructor.Compile();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment