Exemple #1
0
        public void Blocks()
        {
            MethodReference writeLineM = MethodReference.FromLambda(() => Console.WriteLine(""));
            MethodReference readLineM  = MethodReference.FromLambda(() => Console.ReadLine());
            Expression      expr       = Expression.Block(
                ImmutableArray.Create(
                    Expression.StaticMethodCall(
                        writeLineM,
                        Expression.Constant("Enter the output file path: ")
                        )
                    ),
                Expression.StaticMethodCall(readLineM)
                );

            cx.AddTestExpr(expr);

            Expression listOfWrites =
                Enumerable.Range(1, 30)
                .Select(i => $"Line {i}")
                .Select(Expression.Constant)
                .Select(a => Expression.StaticMethodCall(writeLineM, a))
                .ToBlock();

            cx.AddTestExpr(listOfWrites);

            check.CheckOutput(cx);
        }
Exemple #2
0
        public void HelloWorld()
        {
            // declare
            // namespace MyApp.HelloWorld {
            var ns = NamespaceSignature.Parse("MyApp.HelloWorld");
            // public class Program {
            var programType = TypeSignature.Class("Program", ns, Accessibility.APublic);
            // public static int Main() {
            var mainMethod = MethodSignature.Static("Main", programType, Accessibility.APublic, returnType: TypeSignature.Int32);

            // get the Console.WriteLine reference
            var writeLineRef = MethodReference.FromLambda(() => Console.WriteLine(""));

            var body = new [] {
                Expression.StaticMethodCall(writeLineRef, Expression.Constant("Hello world!"))
            }.ToBlock(
                result: Expression.Constant(0)
                );

            var type = TypeDef.Empty(programType).AddMember(
                MethodDef.Create(mainMethod, body)
                );

            var cx = MetadataContext.Create();

            cx.AddType(type);
            var csharp = cx.EmitToString();

            check.CheckString(csharp, fileExtension: "cs");
        }
Exemple #3
0
        public void LoadGenericFunction()
        {
            var genericMethod = MethodReference.FromLambda(() => ValidationResult.Create("abc")).Signature;
            var genericParent = MethodReference.FromLambda <ValidationResult <string> >(a => a.Expect("a")).Signature;

            check.CheckJsonObject(new { genericMethod, genericParent });
        }
Exemple #4
0
        public void Conditions()
        {
            Expression myString = pString1.Read();
            Expression cond     = Expression.Conditional(
                myString.IsNull(),
                Expression.Constant("<empty>"),
                myString
                );

            cx.AddTestExpr(cond, pString1);
            Expression a     = p1.Read();
            Expression cond2 = Expression.Conditional(
                Expression.Binary(">", a, Expression.Constant(10)),
                Expression.StaticMethodCall(
                    MethodReference.FromLambda(() => Console.WriteLine(1)),
                    a),
                Expression.Nop
                );

            cx.AddTestExpr(cond2, p1);
            Expression cond3 = Expression.IfThen(
                Expression.Binary(">", a, Expression.Constant(10)),
                Expression.StaticMethodCall(
                    MethodReference.FromLambda(() => Console.WriteLine(1)),
                    a)
                );

            cx.AddTestExpr(cond3, p1);

            check.CheckOutput(cx);
        }
Exemple #5
0
        public void NestedGenericParameter()
        {
            var m = MethodReference.FromLambda <ArraySegment <int> .Enumerator>(e => e.Current);
            var s = m.Signature;

            Assert.Empty(s.DeclaringType.TypeParameters);
            Assert.Equal(s.ResultType, ((TypeOrNamespace.TypeSignatureCase)s.DeclaringType.Parent).Item.TypeParameters.Single());
        }
Exemple #6
0
        public void NewObject()
        {
            var ctor = MethodReference.FromLambda(() => new System.Collections.Generic.List <int>(0));

            cx.AddTestExpr(Expression.NewObject(ctor, Expression.Constant(100)));

            check.CheckOutput(cx);
        }
        public static MethodDef ImplementToString(TypeSignature declaringType)
        {
            var method = MethodSignature.Override(declaringType, MethodSignature.Object_ToString);

            return(MethodDef.Create(method, @this => {
                return
                @this.Ref()
                .CallMethod(FormatMethodSignature(declaringType))
                .CallMethod(MethodReference.FromLambda <FmtToken>(x => x.ToString("\t", null)));
            }));
        }
Exemple #8
0
        public void LoadReflectionGenericMethod()
        {
            var ref1        = MethodReference.FromLambda(() => Array.Empty <String>());
            var ilspyMethod = cx.GetMethod(ref1);

            Assert.Equal("System.Array.Empty", ilspyMethod.ReflectionName);
            Assert.Equal("System.String", ilspyMethod.TypeArguments.Single().ReflectionName);
            var ref2 = SymbolLoader.Method(ilspyMethod);

            Assert.Equal(ref1.Signature, ref2);
        }
Exemple #9
0
        public void NullCoalesce()
        {
            MethodReference readLineM = MethodReference.FromLambda(() => Console.ReadLine());

            cx.AddTestExpr(pString1.Read().NullCoalesce(Expression.Constant("<null>")), pString1);
            cx.AddTestExpr(Expression.StaticMethodCall(readLineM).NullCoalesce(Expression.Constant("<null>")));
            cx.AddTestExpr(pNullInt.Read().NullCoalesce(ExpressionFactory.Nullable_Create(Expression.Constant(-1))), pNullInt);
            // cx.AddTestExpr(pNullInt.Read().NullCoalesce(Expression.Constant(-1)), pNullInt);

            check.CheckOutput(cx);
        }
Exemple #10
0
        public void LoadSpecializeGenericFunction()
        {
            var genericMethod = MethodReference.FromLambda(() => ValidationResult.Create("abc"))
                                .Signature
                                .Specialize(null, new [] { TypeReference.FromType(typeof(int)) });
            var genericParent = MethodReference.FromLambda <ValidationResult <string> >(a => a.Expect("a"))
                                .Signature
                                .Specialize(new [] { TypeReference.FromType(typeof(bool)) }, null);

            check.CheckJsonObject(new { genericMethod_S = genericMethod.ToString(),
                                        genericParent_S = genericParent.ToString(),
                                        genericMethod,
                                        genericParent });
        }
Exemple #11
0
        public void ConstructorCall()
        {
            cx.AddTestExpr(Expression.NewObject(
                               MethodReference.FromLambda(() => new List <String>()),
                               ImmutableArray <Expression> .Empty
                               ));

            cx.AddTestExpr(Expression.NewObject(
                               MethodReference.FromLambda(() => new List <String>(55)),
                               ImmutableArray.Create(Expression.Constant(1234))
                               ));

            cx.AddTestExpr(Expression.NewObject(
                               MethodReference.FromLambda(() => new DateTime(55L)),
                               ImmutableArray.Create(Expression.Constant(1234L))
                               ));

            check.CheckOutput(cx);
        }
 static MethodSignature FormatMethodSignature(TypeSignature declaringType) =>
 MethodSignature.Override(declaringType, MethodReference.FromLambda <ITokenFormatable>(t => t.Format()).Signature);
Exemple #13
0
        public void GenericClass()
        {
            var paramT         = GenericParameter.Create("T");
            var myContainerSgn = TypeSignature.Class(
                "MyContainer",
                NamespaceSignature.Parse("NS"),
                Accessibility.APublic,
                genericParameters: new [] { paramT }
                );

            var(item_field, item_prop) = PropertyBuilders.CreateAutoProperty(
                myContainerSgn,
                name: "Item",
                propertyType: paramT,
                isReadOnly: false
                );

            var listType = TypeSignature.FromType(typeof(List <>))
                           .Specialize(paramT);
            var toListSgn = MethodSignature.Instance(
                "ToList",
                myContainerSgn,
                Accessibility.APublic,
                returnType: listType
                );

            var toListDef = MethodDef.Create(toListSgn, thisParam => {
                var resultVar = ParameterExpression.Create(listType, "result");
                var listCtor  = MethodReference.FromLambda(() => new List <int>())
                                .Signature
                                .Specialize(paramT);
                var listAdd = MethodReference.FromLambda <List <int> >(l => l.Add(0))
                              .Signature
                              .Specialize(paramT);

                return(Expression.LetIn(
                           // result = new List<T>()
                           resultVar, Expression.NewObject(listCtor),
                           new [] {
                    // result.Add(this.Item)
                    resultVar.Read().CallMethod(listAdd,
                                                thisParam.Read().ReadField(item_field.Signature.SpecializeFromDeclaringType())
                                                )
                }.ToBlock(resultVar)
                           ));
            });

            var copyFromSgn = MethodSignature.Instance(
                "CopyFrom",
                myContainerSgn,
                Accessibility.APublic,
                returnType: TypeSignature.Void,
                new MethodParameter(
                    myContainerSgn.SpecializeByItself(),
                    "other"
                    )
                );

            var copyFromDef = MethodDef.Create(copyFromSgn, (thisParam, otherParam) => {
                var field = item_field.Signature.SpecializeFromDeclaringType();
                return(thisParam.Read().AssignField(
                           field,
                           otherParam.Read().ReadField(field)
                           ));
            });


            var myContainerDef =
                TypeDef.Empty(myContainerSgn)
                .AddMember(item_field, item_prop, toListDef, copyFromDef);

            cx.AddType(myContainerDef);

            check.CheckOutput(cx);
        }