Esempio n. 1
0
        public void EncodingModifiers()
        {
            ITypeDefinition encoding = Mscorlib.GetTypeDefinition(typeof(Encoding));

            Assert.AreEqual(Accessibility.Public, encoding.Accessibility);
            Assert.IsFalse(encoding.IsSealed);
            Assert.IsTrue(encoding.IsAbstract);

            IMethod getDecoder = encoding.Methods.Single(p => p.Name == "GetDecoder");

            Assert.AreEqual(Accessibility.Public, getDecoder.Accessibility);
            Assert.IsFalse(getDecoder.IsStatic);
            Assert.IsFalse(getDecoder.IsAbstract);
            Assert.IsFalse(getDecoder.IsSealed);
            Assert.IsTrue(getDecoder.IsVirtual);
            Assert.IsFalse(getDecoder.IsOverride);

            IMethod getMaxByteCount = encoding.Methods.Single(p => p.Name == "GetMaxByteCount");

            Assert.AreEqual(Accessibility.Public, getMaxByteCount.Accessibility);
            Assert.IsFalse(getMaxByteCount.IsStatic);
            Assert.IsTrue(getMaxByteCount.IsAbstract);
            Assert.IsFalse(getMaxByteCount.IsSealed);
            Assert.IsFalse(getMaxByteCount.IsVirtual);
            Assert.IsFalse(getMaxByteCount.IsOverride);

            IProperty encoderFallback = encoding.Properties.Single(p => p.Name == "EncoderFallback");

            Assert.AreEqual(Accessibility.Public, encoderFallback.Accessibility);
            Assert.IsFalse(encoderFallback.IsStatic);
            Assert.IsFalse(encoderFallback.IsAbstract);
            Assert.IsFalse(encoderFallback.IsSealed);
            Assert.IsFalse(encoderFallback.IsVirtual);
            Assert.IsFalse(encoderFallback.IsOverride);
        }
Esempio n. 2
0
        public void DateTimeDefaultConstructor()
        {
            ITypeDefinition c = Mscorlib.GetTypeDefinition(typeof(DateTime));

            Assert.IsFalse(c.Methods.Any(m => m.IsConstructor && m.Parameters.Count == 0));             // struct ctor isn't declared
            // but it is implicit:
            Assert.IsTrue(c.GetConstructors(ctx).Any(m => m.Parameters.Count == 0));
        }
Esempio n. 3
0
        public void NestedTypesTest()
        {
            ITypeDefinition c = Mscorlib.GetTypeDefinition(typeof(Environment.SpecialFolder));

            Assert.IsNotNull(c, "c is null");
            Assert.AreEqual("System.Environment.SpecialFolder", c.FullName);
            Assert.AreEqual("System.Environment+SpecialFolder", c.ReflectionName);
        }
Esempio n. 4
0
        public void GenericPropertyTest()
        {
            ITypeDefinition   c   = Mscorlib.GetTypeDefinition(typeof(Comparer <>));
            IProperty         def = c.Properties.Single(p => p.Name == "Default");
            ParameterizedType pt  = (ParameterizedType)def.ReturnType.Resolve(ctx);

            Assert.AreEqual("System.Collections.Generic.Comparer", pt.FullName);
            Assert.AreEqual(c.TypeParameters[0], pt.TypeArguments[0]);
        }
Esempio n. 5
0
        public void InnerClassReferenceTest()
        {
            ITypeDefinition c = Mscorlib.GetTypeDefinition(typeof(Environment));

            Assert.IsNotNull(c, "System.Environment not found");
            ITypeReference rt = c.Methods.First(m => m.Name == "GetFolderPath").Parameters[0].Type;

            Assert.AreSame(c.NestedTypes.Single(ic => ic.Name == "SpecialFolder"), rt.Resolve(ctx));
        }
Esempio n. 6
0
        public void StaticModifierTest()
        {
            ITypeDefinition c = Mscorlib.GetTypeDefinition(typeof(Environment));

            Assert.IsNotNull(c, "System.Environment not found");
            Assert.IsTrue(c.IsAbstract, "class should be abstract");
            Assert.IsTrue(c.IsSealed, "class should be sealed");
            Assert.IsTrue(c.IsStatic, "class should be static");
        }
Esempio n. 7
0
        public void NoEncodingInfoDefaultConstructor()
        {
            ITypeDefinition c = Mscorlib.GetTypeDefinition(typeof(EncodingInfo));

            // EncodingInfo only has an internal constructor
            Assert.IsFalse(c.Methods.Any(m => m.IsConstructor));
            // and no implicit ctor should be added:
            Assert.AreEqual(0, c.GetConstructors(ctx).Count());
        }
Esempio n. 8
0
        public void PointerTypeTest()
        {
            ITypeDefinition c         = Mscorlib.GetTypeDefinition(typeof(IntPtr));
            IMethod         toPointer = c.Methods.Single(p => p.Name == "ToPointer");

            Assert.AreEqual("System.Void*", toPointer.ReturnType.Resolve(ctx).ReflectionName);
            Assert.IsTrue(toPointer.ReturnType.Resolve(ctx) is PointerType);
            Assert.AreEqual("System.Void", ((PointerType)toPointer.ReturnType.Resolve(ctx)).ElementType.FullName);
        }
Esempio n. 9
0
        public void NestedClassInGenericClassTest()
        {
            ITypeDefinition dictionary = Mscorlib.GetTypeDefinition(typeof(Dictionary <,>));

            Assert.IsNotNull(dictionary);
            ITypeDefinition valueCollection = Mscorlib.GetTypeDefinition(typeof(Dictionary <,> .ValueCollection));

            Assert.IsNotNull(valueCollection);
            var       dictionaryRT  = new ParameterizedType(dictionary, new[] { Mscorlib.GetTypeDefinition(typeof(string)), Mscorlib.GetTypeDefinition(typeof(int)) });
            IProperty valueProperty = dictionaryRT.GetProperties(ctx).Single(p => p.Name == "Values");
            IType     parameterizedValueCollection = valueProperty.ReturnType.Resolve(ctx);

            Assert.AreEqual("System.Collections.Generic.Dictionary`2+ValueCollection[[System.String],[System.Int32]]", parameterizedValueCollection.ReflectionName);
            Assert.AreSame(valueCollection, parameterizedValueCollection.GetDefinition());
        }
Esempio n. 10
0
        public void UTF32EncodingModifiers()
        {
            ITypeDefinition encoding = Mscorlib.GetTypeDefinition(typeof(UTF32Encoding));

            Assert.AreEqual(Accessibility.Public, encoding.Accessibility);
            Assert.IsTrue(encoding.IsSealed);
            Assert.IsFalse(encoding.IsAbstract);

            IMethod getDecoder = encoding.Methods.Single(p => p.Name == "GetDecoder");

            Assert.AreEqual(Accessibility.Public, getDecoder.Accessibility);
            Assert.IsFalse(getDecoder.IsStatic);
            Assert.IsFalse(getDecoder.IsAbstract);
            Assert.IsFalse(getDecoder.IsSealed);
            Assert.IsFalse(getDecoder.IsVirtual);
            Assert.IsTrue(getDecoder.IsOverride);
        }
Esempio n. 11
0
        public void ValueCollectionCountModifiers()
        {
            ITypeDefinition valueCollection = Mscorlib.GetTypeDefinition(typeof(Dictionary <,> .ValueCollection));

            Assert.AreEqual(Accessibility.Public, valueCollection.Accessibility);
            Assert.IsTrue(valueCollection.IsSealed);
            Assert.IsFalse(valueCollection.IsAbstract);
            Assert.IsFalse(valueCollection.IsStatic);

            IProperty count = valueCollection.Properties.Single(p => p.Name == "Count");

            Assert.AreEqual(Accessibility.Public, count.Accessibility);
            // It's sealed on the IL level; but in C# it's just a normal non-virtual method that happens to implement an interface
            Assert.IsFalse(count.IsSealed);
            Assert.IsFalse(count.IsVirtual);
            Assert.IsFalse(count.IsAbstract);
        }
Esempio n. 12
0
        public void VoidTest()
        {
            ITypeDefinition c = Mscorlib.GetTypeDefinition(typeof(void));

            Assert.IsNotNull(c, "System.Void not found");
            Assert.AreEqual(0, c.GetMethods(ctx).Count());
            Assert.AreEqual(0, c.GetProperties(ctx).Count());
            Assert.AreEqual(0, c.GetEvents(ctx).Count());
            Assert.AreEqual(0, c.GetFields(ctx).Count());
            Assert.AreEqual(
                new string[] {
                "[System.SerializableAttribute]",
                "[System.Runtime.InteropServices.StructLayoutAttribute(0, Size=1)]",
                "[System.Runtime.InteropServices.ComVisibleAttribute(true)]"
            },
                c.Attributes.Select(a => a.ToString()).ToArray());
        }
Esempio n. 13
0
        public void MathAcosModifiers()
        {
            ITypeDefinition math = Mscorlib.GetTypeDefinition(typeof(Math));

            Assert.AreEqual(Accessibility.Public, math.Accessibility);
            Assert.IsTrue(math.IsSealed);
            Assert.IsTrue(math.IsAbstract);
            Assert.IsTrue(math.IsStatic);

            IMethod acos = math.Methods.Single(p => p.Name == "Acos");

            Assert.AreEqual(Accessibility.Public, acos.Accessibility);
            Assert.IsTrue(acos.IsStatic);
            Assert.IsFalse(acos.IsAbstract);
            Assert.IsFalse(acos.IsSealed);
            Assert.IsFalse(acos.IsVirtual);
            Assert.IsFalse(acos.IsOverride);
        }
Esempio n. 14
0
        public void InheritanceTest()
        {
            ITypeDefinition c  = Mscorlib.GetTypeDefinition(typeof(SystemException));
            ITypeDefinition c2 = Mscorlib.GetTypeDefinition(typeof(Exception));

            Assert.IsNotNull(c, "c is null");
            Assert.IsNotNull(c2, "c2 is null");
            //Assert.AreEqual(3, c.BaseTypes.Count); // Inherited interfaces are not reported by Cecil
            // which matches the behaviour of our C#/VB parsers
            Assert.AreEqual("System.Exception", c.BaseTypes[0].Resolve(ctx).FullName);
            Assert.AreSame(c2, c.BaseTypes[0]);

            string[] superTypes = c.GetAllBaseTypes(ctx).Select(t => t.ToString()).ToArray();
            Assert.AreEqual(new string[] {
                "System.Object",
                "System.Runtime.Serialization.ISerializable", "System.Runtime.InteropServices._Exception",
                "System.Exception", "System.SystemException"
            }, superTypes);
        }
Esempio n. 15
0
 public void FixtureSetUp()
 {
     compilation = new SimpleCompilation(TestAssembly,
                                         Mscorlib.WithOptions(TypeSystemOptions.Default));
     language = new CSharpLanguage();
 }
Esempio n. 16
0
 private Type ImportImpl(System.Type type)
 {
     if (type.Assembly == typeof(IKVM.Reflection.Type).Assembly)
     {
         throw new ArgumentException("Did you really want to import " + type.FullName + "?");
     }
     if (type.HasElementType)
     {
         if (type.IsArray)
         {
             if (type.Name.EndsWith("[]"))
             {
                 return(Import(type.GetElementType()).MakeArrayType());
             }
             else
             {
                 return(Import(type.GetElementType()).MakeArrayType(type.GetArrayRank()));
             }
         }
         else if (type.IsByRef)
         {
             return(Import(type.GetElementType()).MakeByRefType());
         }
         else if (type.IsPointer)
         {
             return(Import(type.GetElementType()).MakePointerType());
         }
         else
         {
             throw new InvalidOperationException();
         }
     }
     else if (type.IsGenericParameter)
     {
         if (type.DeclaringMethod != null)
         {
             throw new NotImplementedException();
         }
         else
         {
             return(Import(type.DeclaringType).GetGenericArguments()[type.GenericParameterPosition]);
         }
     }
     else if (type.IsGenericType && !type.IsGenericTypeDefinition)
     {
         System.Type[] args         = type.GetGenericArguments();
         Type[]        importedArgs = new Type[args.Length];
         for (int i = 0; i < args.Length; i++)
         {
             importedArgs[i] = Import(args[i]);
         }
         return(Import(type.GetGenericTypeDefinition()).MakeGenericType(importedArgs));
     }
     else if (type.IsNested)
     {
         // note that we can't pass in the namespace here, because .NET's Type.Namespace implementation is broken for nested types
         // (it returns the namespace of the declaring type)
         return(Import(type.DeclaringType).ResolveNestedType(new TypeName(null, type.Name)));
     }
     else if (type.Assembly == typeof(object).Assembly)
     {
         // make sure mscorlib types always end up in our mscorlib
         return(Mscorlib.ResolveType(new TypeName(type.Namespace, type.Name)));
     }
     else
     {
         return(Import(type.Assembly).ResolveType(new TypeName(type.Namespace, type.Name)));
     }
 }
Esempio n. 17
0
        internal void Emit(OpCode opCode, Label builder)
        {
            var real = Mscorlib.GetType("System.Reflection.Emit.Label");

            this.Invoke("Emit", new Type[] { typeof(OpCode), real }, opCode, builder.Inner);
        }