public override DynamicMetaObject BindCreateInstance(CreateInstanceBinder binder, DynamicMetaObject[] args)
{
var constructors = ReflType.GetConstructors();
var ctors = constructors.Where(c => c.GetParameters().Length == args.Length);
var res = new List<ConstructorInfo>();
foreach (var c in ctors)
{
if (RuntimeHelpers.ParametersMatchArguments(c.GetParameters(), args))
{
res.Add(c);
}
}
if (res.Count == 0)
{
// Binders won't know what to do with TypeModels, so pass the
// RuntimeType they represent. The binder might not be Sympl's.
return binder.FallbackCreateInstance(
RuntimeHelpers.GetRuntimeTypeMoFromModel(this),
args);
}
// For create instance of a TypeModel, we can create a instance
// restriction on the MO, hence the true arg.
var restrictions = RuntimeHelpers.GetTargetArgsRestrictions(this, args, true);
var ctorArgs = RuntimeHelpers.ConvertArguments(args, res[0].GetParameters());
return new DynamicMetaObject(
// Creating an object, so don't need EnsureObjectResult.
Expression.New(res[0], ctorArgs),
restrictions);
}