public DynamicMetaObject Create(CallSignature signature, DynamicMetaObject target, DynamicMetaObject[] args, Expression contextExpression) { Type t = GetTargetType(target.Value); if (t != null) { if (typeof(Delegate).IsAssignableFrom(t) && args.Length == 1) { // PythonOps.GetDelegate(CodeContext context, object callable, Type t); return(new DynamicMetaObject( Ast.Call( typeof(PythonOps).GetMethod(nameof(PythonOps.GetDelegate)), contextExpression, AstUtils.Convert(args[0].Expression, typeof(object)), Expression.Constant(t) ), target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, target.Value)) )); } return(CallMethod( new PythonOverloadResolver( this, args, signature, contextExpression ), PythonTypeOps.GetConstructors(t, PrivateBinding), target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, target.Value)) )); } return(null); }
/// <summary> /// Creating a standard .NET type is easy - we just call it's constructor with the provided /// arguments. /// </summary> private DynamicMetaObject /*!*/ MakeStandardDotNetTypeCall(DynamicMetaObjectBinder /*!*/ call, Expression /*!*/ codeContext, DynamicMetaObject /*!*/[] /*!*/ args) { CallSignature signature = BindingHelpers.GetCallSignature(call); PythonContext state = PythonContext.GetPythonContext(call); MethodBase[] ctors = PythonTypeOps.GetConstructors(Value.UnderlyingSystemType, state.Binder.PrivateBinding); if (ctors.Length > 0) { return(state.Binder.CallMethod( new PythonOverloadResolver( state.Binder, args, signature, codeContext ), ctors, Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(Expression, Value)) )); } else { string msg; if (Value.UnderlyingSystemType.IsAbstract) { msg = String.Format("Cannot create instances of {0} because it is abstract", Value.Name); } else { msg = String.Format("Cannot create instances of {0} because it has no public constructors", Value.Name); } return(new DynamicMetaObject( call.Throw( Ast.New( typeof(TypeErrorException).GetConstructor(new Type[] { typeof(string) }), AstUtils.Constant(msg) ) ), Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(Expression, Value)) )); } }