예제 #1
0
        private ReflectedMethodInfo CreateMethodInfo(MethodInfo methInfo)
        {
            var reflectedMethod = new ReflectedMethodInfo(methInfo.Name);

            reflectedMethod.SetPrivate(!methInfo.IsExport);
            reflectedMethod.IsFunction = methInfo.IsFunction;

            var unknownVal = ValueFactory.CreateInvalidValueMarker();

            for (int i = 0; i < methInfo.Params.Length; i++)
            {
                var currentParam   = methInfo.Params[i];
                var reflectedParam = new ReflectedParamInfo(currentParam.Name, currentParam.IsByValue);
                reflectedParam.SetOwner(reflectedMethod);
                reflectedParam.SetPosition(i);
                if (currentParam.HasDefaultValue)
                {
                }

                reflectedParam.SetDefaultValue(unknownVal);
                if (currentParam.Annotations != null)
                {
                    foreach (var annotation in currentParam.Annotations)
                    {
                        reflectedParam.AddAnnotation(annotation);
                    }
                }

                reflectedMethod.Parameters.Add(reflectedParam);
            }

            if (methInfo.Annotations != null)
            {
                foreach (var annotation in methInfo.Annotations)
                {
                    reflectedMethod.AddAnnotation(annotation);
                }
            }

            return(reflectedMethod);
        }
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            int methIdx;

            try
            {
                methIdx = _context.FindMethod(binder.Name);
            }
            catch (MethodAccessException)
            {
                result = null;
                return(false);
            }

            var methInfo   = _context.GetMethodInfo(methIdx);
            var valueArgs  = new IValue[methInfo.Params.Length];
            var passedArgs = args.Select(x => CustomMarshaller.ConvertReturnValue(x, x.GetType())).ToArray();

            for (int i = 0; i < valueArgs.Length; i++)
            {
                if (i < passedArgs.Length)
                {
                    valueArgs[i] = passedArgs[i];
                }
                else
                {
                    valueArgs[i] = ValueFactory.CreateInvalidValueMarker();
                }
            }

            IValue methResult;

            _context.CallAsFunction(methIdx, valueArgs, out methResult);
            result = methResult == null? null : CustomMarshaller.ConvertToDynamicCLRObject(methResult);

            return(true);
        }