public void GetGenericNestedTypeOfBoundGenericClass() { // class A<X> { class B<Y> { } } DefaultUnresolvedTypeDefinition a = new DefaultUnresolvedTypeDefinition(string.Empty, "A"); a.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "X")); DefaultUnresolvedTypeDefinition b = new DefaultUnresolvedTypeDefinition(a, "B"); b.TypeParameters.Add(a.TypeParameters[0]); b.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "Y")); a.NestedTypes.Add(b); var compilation = TypeSystemHelper.CreateCompilation(a, b); ITypeDefinition resolvedA = compilation.MainAssembly.GetTypeDefinition(a.FullTypeName); ITypeDefinition resolvedB = compilation.MainAssembly.GetTypeDefinition(b.FullTypeName); // A<> gets self-parameterized, B<> stays unbound Assert.AreEqual("A`1+B`1[[`0],[]]", resolvedA.GetNestedTypes().Single().ReflectionName); ParameterizedType pt = new ParameterizedType(resolvedA, new [] { compilation.FindType(KnownTypeCode.String) }); Assert.AreEqual("A`1+B`1[[System.String],[]]", pt.GetNestedTypes().Single().ReflectionName); }
public void MultipleInheritanceTest() { DefaultUnresolvedTypeDefinition b1 = new DefaultUnresolvedTypeDefinition(string.Empty, "B1"); b1.Kind = TypeKind.Interface; b1.Members.Add(new DefaultUnresolvedProperty(b1, "P1")); DefaultUnresolvedTypeDefinition b2 = new DefaultUnresolvedTypeDefinition(string.Empty, "B2"); b2.Kind = TypeKind.Interface; b2.Members.Add(new DefaultUnresolvedProperty(b2, "P2")); DefaultUnresolvedTypeDefinition c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); c.Kind = TypeKind.Interface; c.BaseTypes.Add(b1); c.BaseTypes.Add(b2); var compilation = TypeSystemHelper.CreateCompilation(b1, b2, c); ITypeDefinition resolvedC = compilation.MainAssembly.GetTypeDefinition(c.FullTypeName); Assert.AreEqual(new[] { "P1", "P2" }, resolvedC.GetProperties().Select(p => p.Name).ToArray()); // Test that there's only one copy of ToString(): Assert.AreEqual(1, resolvedC.GetMethods(m => m.Name == "ToString").Count()); }
public void EmptyClassHasToString() { DefaultUnresolvedTypeDefinition c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); var compilation = TypeSystemHelper.CreateCompilation(c); Assert.AreEqual("System.Object.ToString", compilation.MainAssembly.GetTypeDefinition(c).GetMethods(m => m.Name == "ToString").Single().FullName); }
public void GetNestedTypesOfBoundGenericClass() { var compilation = TypeSystemHelper.CreateCompilation(); IType dictionary = compilation.FindType(typeof(Dictionary <string, int>)); IType keyCollection = dictionary.GetNestedTypes().Single(t => t.Name == "KeyCollection"); Assert.AreEqual(compilation.FindType(typeof(Dictionary <string, int> .KeyCollection)), keyCollection); }
public void GetNestedTypesOfUnboundGenericClass() { var compilation = TypeSystemHelper.CreateCompilation(); ITypeDefinition dictionary = compilation.FindType(typeof(Dictionary <,>)).GetDefinition(); IType keyCollection = dictionary.GetNestedTypes().Single(t => t.Name == "KeyCollection"); // the type should be parameterized Assert.AreEqual("System.Collections.Generic.Dictionary`2+KeyCollection[[`0],[`1]]", keyCollection.ReflectionName); }
public void ClassDerivingFromItself() { // class C : C {} var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); c.BaseTypes.Add(c); compilation = TypeSystemHelper.CreateCompilation(c); ITypeDefinition resolvedC = Resolve(c); Assert.AreEqual(new [] { resolvedC }, resolvedC.GetAllBaseTypes().ToArray()); }
public void ClassDerivingFromParameterizedVersionOfItself() { // class C<X> : C<C<X>> {} var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "X")); c.BaseTypes.Add(new ParameterizedTypeReference(c, new [] { new ParameterizedTypeReference(c, new [] { new TypeParameterReference(EntityType.TypeDefinition, 0) }) })); compilation = TypeSystemHelper.CreateCompilation(c); ITypeDefinition resolvedC = Resolve(c); Assert.AreEqual(new [] { resolvedC }, resolvedC.GetAllBaseTypes().ToArray()); }
public void TwoClassesDerivingFromEachOther() { // class C1 : C2 {} class C2 : C1 {} var c1 = new DefaultUnresolvedTypeDefinition(string.Empty, "C1"); var c2 = new DefaultUnresolvedTypeDefinition(string.Empty, "C2"); c1.BaseTypes.Add(c2); c2.BaseTypes.Add(c1); compilation = TypeSystemHelper.CreateCompilation(c1, c2); ITypeDefinition resolvedC1 = Resolve(c1); ITypeDefinition resolvedC2 = Resolve(c2); Assert.AreEqual(new [] { resolvedC2, resolvedC1 }, resolvedC1.GetAllBaseTypes().ToArray()); }
public void StructImplementingIEquatable() { // struct S : IEquatable<S> {} // don't use a Cecil-loaded struct for this test; we're testing the implicit addition of System.ValueType var s = new DefaultUnresolvedTypeDefinition(string.Empty, "S"); s.Kind = TypeKind.Struct; s.BaseTypes.Add(new ParameterizedTypeReference(typeof(IEquatable <>).ToTypeReference(), new[] { s })); compilation = TypeSystemHelper.CreateCompilation(s); ITypeDefinition resolvedS = Resolve(s); IType[] expected = { resolvedS, s.BaseTypes[0].Resolve(new SimpleTypeResolveContext(resolvedS)), compilation.FindType(typeof(object)), compilation.FindType(typeof(ValueType)) }; Assert.AreEqual(expected, resolvedS.GetAllBaseTypes().OrderBy(t => t.ReflectionName).ToArray()); }
public void ClassDerivingFromTwoInstanciationsOfIEnumerable() { // class C : IEnumerable<int>, IEnumerable<uint> {} var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); c.BaseTypes.Add(typeof(IEnumerable <int>).ToTypeReference()); c.BaseTypes.Add(typeof(IEnumerable <uint>).ToTypeReference()); compilation = TypeSystemHelper.CreateCompilation(c); ITypeDefinition resolvedC = Resolve(c); IType[] expected = { resolvedC, compilation.FindType(typeof(IEnumerable <int>)), compilation.FindType(typeof(IEnumerable <uint>)), compilation.FindType(typeof(IEnumerable)), compilation.FindType(typeof(object)) }; Assert.AreEqual(expected, resolvedC.GetAllBaseTypes().OrderBy(t => t.ReflectionName).ToArray()); }
public void TypeParameterDerivingFromOtherTypeParameterInheritsEffectiveBaseClass() { // class C<T, U> where T : List<string> where U : T var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T") { Constraints = { typeof(List <string>).ToTypeReference() } }); c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U") { Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) } }); ITypeDefinition resolvedC = TypeSystemHelper.CreateCompilation(c).MainAssembly.GetTypeDefinition(c); Assert.AreEqual(true, resolvedC.TypeParameters[0].IsReferenceType); Assert.AreEqual(true, resolvedC.TypeParameters[1].IsReferenceType); Assert.AreEqual("System.Collections.Generic.List`1[[System.String]]", resolvedC.TypeParameters[0].EffectiveBaseClass.ReflectionName); Assert.AreEqual("System.Collections.Generic.List`1[[System.String]]", resolvedC.TypeParameters[1].EffectiveBaseClass.ReflectionName); }
public void TypeParameterDerivingFromOtherTypeParameterDoesNotInheritReferenceConstraint() { // class C<T, U> where T : class where U : T var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T") { HasReferenceTypeConstraint = true }); c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 1, "U") { Constraints = { new TypeParameterReference(EntityType.TypeDefinition, 0) } }); ITypeDefinition resolvedC = TypeSystemHelper.CreateCompilation(c).MainAssembly.GetTypeDefinition(c); // At runtime, we might have T=System.ValueType and U=int, so C# can't inherit the 'class' constraint // from one type parameter to another. Assert.AreEqual(true, resolvedC.TypeParameters[0].IsReferenceType); Assert.IsNull(resolvedC.TypeParameters[1].IsReferenceType); }