Ejemplo n.º 1
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");
        }
Ejemplo n.º 2
0
        public void GenericMethodInGenericTypes()
        {
            var ns       = NamespaceSignature.Parse("MyNamespace");
            var t1       = new GenericParameter(Guid.NewGuid(), "T1");
            var t2       = new GenericParameter(Guid.NewGuid(), "T2");
            var tresult  = new GenericParameter(Guid.NewGuid(), "TResult");
            var rootType = TypeSignature.Class("MyType", ns, Accessibility.APublic, true, false, t1);

            var(f, p) = PropertyBuilders.CreateAutoProperty(rootType, "A", t1, isReadOnly: false);

            var map_fn_type = new FunctionType(new [] { new MethodParameter(t1, "a") }, tresult);
            var map_sgn     = MethodSignature.Instance("Map", rootType, Accessibility.APublic,
                                                       rootType.Specialize(tresult),
                                                       new [] { tresult },
                                                       new MethodParameter(map_fn_type.TryGetDelegate(), "func")
                                                       );
            var tmp     = ParameterExpression.Create(map_sgn.ResultType, "result");
            var map_def = MethodDef.Create(map_sgn, (@this, fn) =>
                                           Expression.Block(
                                               new [] {
                tmp.Ref()
                .CallMethod(p.Setter.Signature.Specialize(new TypeReference[] { tresult }, null),
                            fn.Read().FunctionConvert(map_fn_type).Invoke(@this.Ref().CallMethod(p.Getter.Signature.SpecializeFromDeclaringType()))
                            )
            },
                                               result: tmp
                                               )
                                           .Where(tmp, Expression.NewObject(MethodSignature.ImplicitConstructor(rootType).Specialize(new TypeReference[] { tresult }, null)))
                                           );


            var type = TypeSignature.Class("MyNestedType", rootType, Accessibility.APublic, true, false, t2);

            var t3     = new GenericParameter(Guid.NewGuid(), "T3");
            var method = MethodSignature.Instance(
                "M", type, Accessibility.APublic,
                returnType: type.Specialize(t1, t3),
                typeParameters: new [] { t3 });

            var td = TypeDef.Empty(type)
                     .AddMember(MethodDef.Create(method, @this =>
                                                 Expression.NewObject(
                                                     MethodSignature.ImplicitConstructor(type).Specialize(new TypeReference[] { t1, t3 }, null),
                                                     ImmutableArray <Expression> .Empty
                                                     )
                                                 ))
            ;

            cx.AddType(TypeDef.Empty(rootType).AddMember(td, f, p, map_def));
            check.CheckOutput(cx);
        }
Ejemplo n.º 3
0
        public void AutoProperties()
        {
            var ns   = NamespaceSignature.Parse("MyNamespace");
            var t1   = new GenericParameter(Guid.NewGuid(), "T1");
            var t2   = new GenericParameter(Guid.NewGuid(), "T2");
            var type = TypeSignature.Class("MyType", ns, Accessibility.APublic, true, false, t1, t2);
            var td   = TypeDef.Empty(type)
                       .AddAutoProperty("A", t1, Accessibility.APublic)
                       .AddAutoProperty("B", t2, Accessibility.APublic, isStatic: true)
                       .AddAutoProperty("C", TypeSignature.FromType(typeof(Dictionary <,>)).Specialize(t1, t2), Accessibility.APublic, isStatic: true, isReadOnly: false)
                       .AddAutoProperty("D", TypeReference.Tuple(t1, TypeSignature.String, t2), Accessibility.APublic, isReadOnly: false)
            ;

            cx.AddType(td);
            check.CheckOutput(cx);
        }
Ejemplo n.º 4
0
        public void ToStringExample()
        {
            var type = TypeSignature.Class("MyClass", NamespaceSignature.Parse("NS"), Accessibility.APublic);

            cx.AddType(ImplementToString(
                           TypeDef.Empty(type)
                           .AddAutoProperty("A", TypeSignature.String)
                           .AddAutoProperty("B", TypeSignature.ValueTuple2.Specialize(TypeSignature.Boolean, TypeSignature.Boolean))
                           ));

            cx.AddType(ImplementToString(
                           TypeDef.Empty(type.With(name: "SecondClass"))
                           ));

            check.CheckOutput(cx);
        }
Ejemplo n.º 5
0
        public void FewFields()
        {
            var stringT   = TypeReference.FromType(typeof(string));
            var stringArr = TypeReference.FromType(typeof(string[]));
            var someTuple = TypeReference.FromType(typeof((List <int>, System.Threading.Tasks.Task)));

            var ns      = NamespaceSignature.Parse("MyNamespace");
            var type    = TypeSignature.Class("MyType", ns, Accessibility.APublic);
            var typeDef = TypeDef.Empty(type).With(members: ImmutableArray.Create <MemberDef>(
                                                       new FieldDef(new FieldSignature(type, "F1", Accessibility.APublic, stringT, false, true)),
                                                       new FieldDef(new FieldSignature(type, "F2", Accessibility.APrivate, stringArr, false, true)),
                                                       new FieldDef(new FieldSignature(type, "F3", Accessibility.AInternal, someTuple, false, true)),
                                                       new FieldDef(new FieldSignature(type, "F4", Accessibility.AProtectedInternal, stringArr, true, false))
                                                       ));

            cx.AddType(typeDef);
            check.CheckOutput(cx);
        }
Ejemplo n.º 6
0
        public void Variables()
        {
            var myVar = ParameterExpression.Create(TypeSignature.Int32, "myVar");

            cx.AddTestExpr(Expression.LetIn(
                               myVar, Expression.Constant(42),
                               target: Expression.Binary("+", myVar, myVar)
                               ));

            var mutableVar = ParameterExpression.CreateMutable(TypeSignature.Int32, "mutableVar");

            cx.AddTestExpr(Expression.LetIn(
                               mutableVar, Expression.Constant(42),
                               target: Expression.Binary("+",
                                                         mutableVar,
                                                         new [] {
                mutableVar.Assign(Expression.Constant(30))
            }.ToBlock(mutableVar)
                                                         )
                               ));

            var declaringType = TypeSignature.StaticClass("TestClass", NamespaceSignature.Parse("NS"), Accessibility.APublic);
            var method        = MethodSignature.Static(
                "M",
                declaringType,
                Accessibility.APublic,
                TypeSignature.Int32,
                new MethodParameter(TypeSignature.Int32, "a"));
            var argA = ParameterExpression.CreateMutable(TypeSignature.Int32, "a");
            var body = new [] {
                argA.Assign(Expression.Binary("+", argA, Expression.Constant(1)))
            }.ToBlock(result: argA);
            var methodDef = new MethodDef(method, new [] { argA }, body);

            cx.AddType(TypeDef.Empty(declaringType).AddMember(methodDef));

            check.CheckOutput(cx);
        }
Ejemplo n.º 7
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);
        }