public void ImportOpenGenericType() { // class C<T, U> { void M<X>() {} } var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.TypeDefinition, 0, "T")); c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.TypeDefinition, 1, "U")); var m = new DefaultUnresolvedMethod(c, "M"); m.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.Method, 0, "X")); c.Members.Add(m); var resolvedC1 = TypeSystemHelper.CreateCompilationAndResolve(c); var resolvedM1 = resolvedC1.Methods.Single(method => method.Name == "M"); var resolvedC2 = TypeSystemHelper.CreateCompilationAndResolve(c); var resolvedM2 = resolvedC2.Methods.Single(method => method.Name == "M"); // the types, methods and type parameters differ in the two compilations: Assert.AreNotEqual(resolvedC1, resolvedC2); Assert.AreNotEqual(resolvedM1, resolvedM2); Assert.AreNotEqual(resolvedC1.TypeParameters[1], resolvedC2.TypeParameters[1]); Assert.AreNotEqual(resolvedM1.TypeParameters[0], resolvedM2.TypeParameters[0]); // C<U, X> var pt1 = new ParameterizedType(resolvedC1, new[] { resolvedC1.TypeParameters[1], resolvedM1.TypeParameters[0] }); var pt2 = (ParameterizedType)resolvedC2.Compilation.Import(pt1); // importing resulted in C<U, X> in the new compilation: Assert.AreEqual(resolvedC2, pt2.GetDefinition()); Assert.AreEqual(resolvedC2.TypeParameters[1], pt2.TypeArguments[0]); Assert.AreEqual(resolvedM2.TypeParameters[0], pt2.TypeArguments[1]); }
public void EmptyClassHasToString() { DefaultUnresolvedTypeDefinition c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); var resolvedC = TypeSystemHelper.CreateCompilationAndResolve(c); Assert.AreEqual("System.Object.ToString", resolvedC.GetMethods(m => m.Name == "ToString").Single().FullName); }
public void ClassDerivingFromItself() { // class C : C {} var c = new DefaultUnresolvedTypeDefinition(string.Empty, "C"); c.BaseTypes.Add(c); ITypeDefinition resolvedC = TypeSystemHelper.CreateCompilationAndResolve(c); Assert.AreEqual(new [] { resolvedC }, resolvedC.GetAllBaseTypes().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(SymbolKind.TypeDefinition, 0, "T") { Constraints = { typeof(List <string>).ToTypeReference() } }); c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.TypeDefinition, 1, "U") { Constraints = { new TypeParameterReference(SymbolKind.TypeDefinition, 0) } }); ITypeDefinition resolvedC = TypeSystemHelper.CreateCompilationAndResolve(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(SymbolKind.TypeDefinition, 0, "T") { HasReferenceTypeConstraint = true }); c.TypeParameters.Add(new DefaultUnresolvedTypeParameter(SymbolKind.TypeDefinition, 1, "U") { Constraints = { new TypeParameterReference(SymbolKind.TypeDefinition, 0) } }); ITypeDefinition resolvedC = TypeSystemHelper.CreateCompilationAndResolve(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); }