Beispiel #1
0
        public static ILFunction CreateBody(MethodDef method, IMethod generatedMethod, MetadataContext cx)
        {
            Assert.NotNull(method.Body);
            Assert.False(method.ArgumentParams.IsDefault);
            if (method.Body is ILSpyMethodBody iLSpyMethod)
            {
                return(iLSpyMethod.BuildBody(generatedMethod, cx));
            }

            var translator    = new CodeTranslator(method, generatedMethod, cx);
            var declaringType = method.Signature.DeclaringType.SpecializeByItself();

            if (!method.Signature.IsStatic)
            {
                var firstArgument = method.ArgumentParams.First();
                if (declaringType.Type.IsValueType)
                {
                    Assert.Equal(TypeReference.ByReferenceType(declaringType), firstArgument.Type);
                }
                else
                {
                    Assert.Equal(declaringType, firstArgument.Type);
                }

                translator.Parameters.Add(
                    firstArgument.Id,
                    new ILVariable(VariableKind.Parameter, cx.GetTypeReference(firstArgument.Type), -1)
                {
                    Name = "this"
                });
            }
            foreach (var(i, (arg, param)) in method.ArgumentParams.Skip(method.Signature.IsStatic ? 0 : 1).ZipTuples(method.Signature.Params).Indexed())
            {
                Assert.Equal(arg.Type, param.Type);
                translator.Parameters.Add(
                    arg.Id,
                    new ILVariable(VariableKind.Parameter, cx.GetTypeReference(param.Type), i)
                {
                    Name = generatedMethod.Parameters[i].Name
                });
            }
            var verificationVarCopy = translator.Parameters.Keys.ToHashSet();

            var statements = translator.TranslateExpression(method.Body);

            Assert.Empty(translator.BreakTargets);
            Assert.Equal(verificationVarCopy, translator.Parameters.Keys.ToHashSet());

            return(translator.BuildTheFunction(statements));
        }
Beispiel #2
0
        public void LoadReflectionGenericType(Type t)
        {
            var ref1      = TypeReference.FromType(t);
            var ilspyType = cx.GetTypeReference(TypeReference.FromType(t));
            var ref2      = SymbolLoader.TypeRef(ilspyType);

            Assert.Equal(ref1, ref2);
        }