public void CopyFrom(MethodInfo method)
        {
            var body = method.GetMethodBody();
            var il   = ILInstructionReader.FromByteCode(body.GetILAsByteArray());

            Stream = il;
            Locals = body.LocalVariables.Count == 0 ? null : body.LocalVariables.Select(lcl => new ILVariable(lcl)).ToArray();
            SetResolver(method);
        }
Esempio n. 2
0
        public object ExecuteTyped(MethodInfo method, params object[] args)
        {
            var argLength        = args?.Length;
            var methodParameters = method.GetParameters();

            if (method.CallingConvention == CallingConventions.VarArgs)
            {
                // throw new PlatformID
            }
            var methodParameterCount = methodParameters.Length;

            if (methodParameterCount == 0 && method.ContainsGenericParameters)
            {
                //var genericArguments = method.GetGenericArguments().Length;
            }
            if (!method.IsStatic)
            {
                methodParameterCount += 1;
            }
            var body           = method.GetMethodBody();
            var localVariables = body.LocalVariables;

            if (methodParameterCount != argLength)
            {
                bool parsedParamArray = false;
                for (var i = 0; parsedParamArray == false && i < methodParameters.Length; i++)
                {
                    if (methodParameters[i].GetCustomAttribute <ParamArrayAttribute>() != null)
                    {
                        parsedParamArray = true;
                        var l = new List <object>();
                        for (var k = 0; k < i; k++)
                        {
                            l.Add(args[k]);
                        }

                        var paramArgs = new object[args.Length - i];
                        for (var k = i; k < args.Length; k++)
                        {
                            paramArgs[k - i] = args[k];
                        }
                        l.Add(paramArgs);
                        args = l.ToArray();
                    }
                }
                //if (argLength < methodParameterCount || !methodParameters.Any(x => !(x.GetCustomAttribute<ParamArrayAttribute>() is null)))
                if (!parsedParamArray)
                {
                    throw new TargetParameterCountException($"{method.ReflectedType.Name}::{method} does not take {argLength} parameters");
                }
            }

            var methodBytes  = method.GetMethodBody().GetILAsByteArray();
            var ilStream     = ILInstructionReader.FromByteCode(method.GetMethodBody().GetILAsByteArray());
            var ilStreamCode = ILInstructionReader.ToString(ilStream);


            var resolver = new ILInstructionResolver(method);
            var locals   = new ILVariable[localVariables.Count];

            for (var i = 0; i < locals.Length; i++)
            {
                locals[localVariables[i].LocalIndex].CopyFrom(localVariables[i], body.InitLocals);
            }


            bool doTrace = bool.Parse(bool.FalseString);

            if (doTrace)
            {
                System.Diagnostics.Trace.WriteLine(method);
                if (args != null && args.Length > 0)
                {
                    System.Diagnostics.Trace.WriteLine(string.Join("\r\n", Enumerable.Range(0, args.Length).Select(i => $"arg[{i}]: {args[i]}")));
                }
                if (locals != null && locals.Length > 0)
                {
                    System.Diagnostics.Trace.WriteLine(string.Join("\r\n", Enumerable.Range(0, locals.Length).Select(i => $"loc[{i}]: {locals[i]}")));
                }

                System.Diagnostics.Trace.WriteLine(ilStreamCode);
            }
            return(ExecuteTyped(ilStream, resolver, args.ToArray(), locals));
        }