Example #1
0
 public object Call(params object[] args)
 {
     try {
         object ret = obj.GetType().InvokeMember(
             name,
             System.Reflection.BindingFlags.InvokeMethod,
             Type.DefaultBinder,
             obj,
             args
             );
         return(Ops.ToPython(ret));
     } catch (Exception e) {
         if (e.InnerException != null)
         {
             throw ExceptionConverter.UpdateForRethrow(e.InnerException);
         }
         throw;
     }
 }
Example #2
0
        internal void SetAttr(ICallerContext context, SymbolId name, object value)
        {
            Initialize(context);

            if (HaveInterfaces)
            {
                foreach (DynamicType type in interfaces)
                {
                    try {
                        type.SetAttr(context, obj, name, value);
                        return;
                    } catch {
                    }
                }
                throw Ops.AttributeErrorForMissingAttribute(ComType.MakeDynamicType().__name__.ToString(), name);
            }
            else
            {
                try {
                    Obj.GetType().InvokeMember(
                        (string)SymbolTable.IdToString(name),
                        System.Reflection.BindingFlags.SetProperty |
                        System.Reflection.BindingFlags.SetField,
                        Type.DefaultBinder,
                        Obj,
                        new object[1] {
                        value
                    }
                        );
                } catch (Exception e) {
                    if (e.InnerException != null)
                    {
                        throw ExceptionConverter.UpdateForRethrow(e.InnerException);
                    }
                    throw;
                }
            }
        }
Example #3
0
        /// <summary>
        /// Performs late-bound invocation - used by ReflectedMethod's and
        /// for keyword argument calls.
        /// </summary>
        protected static object Invoke(MethodBinding binding)
        {
            object result;

            try {
                if (binding.method is ConstructorInfo)
                {
                    result = ((ConstructorInfo)binding.method).Invoke(binding.arguments);
                }
                else
                {
                    result = binding.method.Invoke(binding.instance, binding.arguments);
                }
            } catch (TargetInvocationException tie) {
                throw ExceptionConverter.UpdateForRethrow(tie.InnerException);
            }

            MethodBase info = binding.method;

            ParameterInfo[] parameters = info.GetParameters();
            int             results    = 0;

            for (int parm = 0; parm < parameters.Length; parm++)
            {
                if (parameters[parm].ParameterType.IsByRef)
                {
                    results++;
                }
            }
            if (results == 0)
            {
                return(Ops.ToPython(CompilerHelpers.GetReturnType(info), result));
            }

            object[] retValues;
            int      retValueIndex = 0;

            if (info is MethodInfo && ((MethodInfo)info).ReturnType != typeof(void))
            {
                retValues = new object[results + 1];
                retValues[retValueIndex++] = Ops.ToPython(CompilerHelpers.GetReturnType(info), result);
            }
            else
            {
                retValues = new object[results];
            }

            for (int parm = 0; parm < parameters.Length; parm++)
            {
                Type parmType = parameters[parm].ParameterType;
                if (parmType.IsByRef)
                {
                    retValues[retValueIndex++] = Ops.ToPython(parmType, binding.arguments[parm]);
                }
            }
            if (retValues.Length == 1)
            {
                return(retValues[0]);
            }
            else
            {
                return(Tuple.MakeTuple(retValues));
            }
        }