Exemple #1
0
        public void TypeLoaderConsistency(Type t)
        {
            var signature = TypeSignature.FromType(t);
            var ilSpy     = cx.GetTypeDef(signature);

            Assert.Equal(SymbolFormatter.TypeDefToString(t), SymbolFormatter.TypeDefToString(ilSpy));
            Assert.Equal(signature, SymbolLoader.Type(ilSpy));
        }
Exemple #2
0
        static Expression ExampleCondition(MetadataContext cx)
        {
            var thread        = TypeSignature.FromType(typeof(System.Threading.Thread)).NotGeneric();
            var currentThread = cx.GetMemberProperty(thread, "CurrentThread").Getter();
            var isBackground  = cx.GetMemberProperty(((TypeReference.SpecializedTypeCase)currentThread.ResultType()).Item, "IsBackground").Getter();
            var condition     = Expression.MethodCall(isBackground, ImmutableArray <Expression> .Empty, Expression.MethodCall(currentThread, ImmutableArray <Expression> .Empty, null));

            return(condition);
        }
Exemple #3
0
        public void LoadCoreMethods()
        {
            var stringT = TypeSignature.FromType(typeof(string));

            var methods = cx.GetMemberMethods(stringT.NotGeneric()).ToArray();

            var toUpperInvariantM = methods.Single(s => s.Name() == "ToUpperInvariant");

            check.CheckJsonObject(new { toUpperInvariantM });
        }
Exemple #4
0
        public void LoadReflectionType(Type t)
        {
            var signature         = TypeSignature.FromType(t);
            var methods           = cx.GetMemberMethodDefs(signature).Where(m => m.Accessibility == Accessibility.APublic).Select(m => m.Name).Distinct().OrderBy(a => a).ToArray();
            var reflectionMethods = t.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly)
                                    .Where(m => !m.IsSpecialName)
                                    .Select(m => m.Name).Distinct().OrderBy(a => a).ToArray();

            Assert.Equal(methods, reflectionMethods);
        }
Exemple #5
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);
        }
Exemple #6
0
        public void LoadCoreTypes()
        {
            var stringT   = TypeSignature.FromType(typeof(string));
            var dateTimeT = cx.FindTypeDef("System.DateTime");

            Assert.False(stringT.CanOverride);
            Assert.Empty(stringT.TypeParameters);
            Assert.Equal("String", stringT.Name);
            Assert.Equal("DateTime", dateTimeT.Name);
            Assert.Equal(TypeOrNamespace.NamespaceSignature(NamespaceSignature.System), dateTimeT.Parent);
            Assert.Equal(dateTimeT.Parent, stringT.Parent);

            var enumerableT = TypeSignature.FromType(typeof(IEnumerable <>));

            check.CheckJsonObject(new { stringT, dateTimeT, enumerableT });
        }
Exemple #7
0
        public void StructInterface()
        {
            var icloneable = TypeSignature.FromType(typeof(ICloneable));
            var method     = new MethodSignature(MyStruct, ImmutableArray <MethodParameter> .Empty, "Clone", TypeSignature.Object, false, Accessibility.APublic, false, false, false, false, ImmutableArray <GenericParameter> .Empty);
            var thisP      = ParameterExpression.CreateThisParam(method);
            var methodDef  = new MethodDef(method, ImmutableArray.Create(thisP), Expression.ReferenceConversion(thisP, TypeSignature.Object));

            cx.AddType(MyStructDef.AddMember(methodDef).AddImplements(new SpecializedType(icloneable, ImmutableArray <TypeReference> .Empty)));

            var interfaceClone = cx.GetMemberMethods(icloneable.NotGeneric()).Single(m => m.Signature.Name == "Clone");

            var p = ParameterExpression.Create(MyStruct, "p");

            cx.AddTestExpr(Expression.MethodCall(method, ImmutableArray <Expression> .Empty, p), p);
            cx.AddTestExpr(Expression.MethodCall(interfaceClone, ImmutableArray <Expression> .Empty, Expression.ReferenceConversion(p, icloneable)), p);

            check.CheckOutput(cx);
        }
Exemple #8
0
        public void IEquatableImplementation(bool isStruct, bool isExplicit)
        {
            var type = isStruct ? TypeSignature.Struct("MyType", ns, Accessibility.APublic)
                                : TypeSignature.Class("MyType", ns, Accessibility.APublic);
            var iequatableT     = TypeSignature.FromType(typeof(IEquatable <>)).Specialize(type);
            var interfaceMethod = cx.GetMemberMethods(iequatableT, "Equals").Single();
            var method          = new MethodSignature(type, ImmutableArray.Create(new MethodParameter(type, "obj")), "Equals", TypeReference.FromType(typeof(bool)), false, isExplicit ? Accessibility.APrivate : Accessibility.APublic, false, false, false, false, ImmutableArray <GenericParameter> .Empty);
            var methodDef       = MethodDef.Create(method, (_thisP, _objP) => new ConstantExpression(true, TypeReference.FromType(typeof(bool))))
                                  .AddImplements(interfaceMethod);
            var typeDef = TypeDef.Empty(type).With(
                implements: ImmutableArray.Create(iequatableT),
                members: ImmutableArray.Create <MemberDef>(methodDef));

            cx.AddType(typeDef);


            check.CheckOutput(cx, $"{(isStruct ? "struct" : "class")}{(isExplicit ? "-explicit" : "")}");
        }
Exemple #9
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);
        }
Exemple #10
0
        public void ParameterDefaultValues()
        {
            var type = TypeSignature.Interface("MyInterface2", ns, Accessibility.APublic);

            var method1 = MethodSignature.Instance("StringMethod", type, Accessibility.APublic, TypeSignature.Int32, new MethodParameter(TypeSignature.String, "myParameter").WithDefault("default value"));
            var method2 = MethodSignature.Instance("ValueTypeMethod", type, Accessibility.APublic, TypeSignature.Int32, new MethodParameter(TypeSignature.FromType(typeof(Guid)), "myParameter").WithDefault(null));
            var typeDef = TypeDef.Empty(type)
                          .AddMember(MethodDef.InterfaceDef(method1))
                          .AddMember(MethodDef.InterfaceDef(method2));


            cx.AddType(typeDef);
            check.CheckOutput(cx);
        }