public void NestedTypeInCurrentClass() { var type = new ParameterizedTypeReference(nestedClass, new[] { baseClass.TypeParameters[0], KnownTypeReference.String }); Assert.AreEqual("Nested<string>", TypeToString(type, baseClass)); Assert.AreEqual("Nested<string>", TypeToString(type, nestedClass)); }
public void AliasedType() { var type = new ParameterizedTypeReference(ctx.GetTypeDefinition(typeof(List <>)), new[] { KnownTypeReference.Char }); Assert.AreEqual("List<char>", TypeToString(type)); Assert.AreEqual("L", TypeToString(type, systemClass)); }
static bool UseFullParameterizedName(ParameterizedTypeReference pr, IType t) { if (!Sk.NewInterfaceImplementation) { return(pr.TypeArguments.All((p) => p is ICSharpCode.NRefactory.TypeSystem.Implementation.GetClassTypeReference)); } return(pr.TypeArguments.All((p) => p is ICSharpCode.NRefactory.TypeSystem.Implementation.GetClassTypeReference) && t.Kind != TypeKind.Interface); }
public void NestedType() { var type = new ParameterizedTypeReference(nestedClass, new[] { KnownTypeReference.Char, KnownTypeReference.String }); Assert.AreEqual("Base<char>.Nested<string>", TypeToString(type)); Assert.AreEqual("Base<char>.Nested<string>", TypeToString(type, baseClass)); Assert.AreEqual("Base<char>.Nested<string>", TypeToString(type, nestedClass)); Assert.AreEqual("Base<char>.Nested<string>", TypeToString(type, derivedClass)); }
public void NestedTypeInDerivedClass() { var type1 = new ParameterizedTypeReference(nestedClass, new[] { derivedClass.TypeParameters[0], KnownTypeReference.String }); Assert.AreEqual("Base<T>.Nested<string>", TypeToString(type1, derivedClass)); var type2 = new ParameterizedTypeReference(nestedClass, new[] { derivedClass.TypeParameters[1], KnownTypeReference.String }); Assert.AreEqual("Nested<string>", TypeToString(type2, derivedClass)); }
public static string GetTypeName(this IMember member, string[] usedNamespaces) { ParameterizedTypeReference paramTypeRef = member.UnresolvedMember.ReturnType as ParameterizedTypeReference; if (paramTypeRef != null) //generic type declaration { return(paramTypeRef.GetTypeName(usedNamespaces)); } else { return(member.ReturnType.FullName.NormaliseClrName(usedNamespaces)); } }
ICodeContext CreateContext(ITextEditor editor) { var compilation = SD.ParserService.GetCompilationForFile(editor.FileName); var project = SD.ProjectService.FindProjectContainingFile(editor.FileName); var resolveContext = new SimpleTypeResolveContext(compilation.MainAssembly); var currentTypeDefinition = new DefaultUnresolvedTypeDefinition(project.RootNamespace, Path.GetFileNameWithoutExtension(editor.FileName)); ITypeReference baseTypeReference = new GetClassTypeReference("System.Web.Mvc", "WebViewPage", 1); baseTypeReference = new ParameterizedTypeReference(baseTypeReference, new[] { FindModelType(editor) }); currentTypeDefinition.BaseTypes.Add(baseTypeReference); var currentMethod = new DefaultUnresolvedMethod(currentTypeDefinition, "__ContextStub__"); currentMethod.ReturnType = KnownTypeReference.Void; currentTypeDefinition.Members.Add(currentMethod); var currentResolvedTypeDef = new DefaultResolvedTypeDefinition(resolveContext, currentTypeDefinition); var projectContent = compilation.MainAssembly.UnresolvedAssembly as IProjectContent; var currentFile = new CSharpUnresolvedFile(); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Mvc"); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Mvc.Ajax"); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Mvc.Html"); currentFile.RootUsingScope.AddSimpleUsing("System.Web.Routing"); currentFile.TopLevelTypeDefinitions.Add(currentTypeDefinition); if (projectContent != null) { compilation = projectContent.AddOrUpdateFiles(currentFile).CreateCompilation(SD.ParserService.GetCurrentSolutionSnapshot()); } var context = new CSharpTypeResolveContext(compilation.MainAssembly, currentFile.RootUsingScope.Resolve(compilation), currentResolvedTypeDef, currentMethod.CreateResolved(resolveContext.WithCurrentTypeDefinition(currentResolvedTypeDef))); return(new CSharpResolver(context)); }
public static string GetTypeName(this ParameterizedTypeReference obj, string[] usedNamespaces) { string retval = ""; if (obj.GenericType is DefaultUnresolvedTypeDefinition) { retval = (obj.GenericType as DefaultUnresolvedTypeDefinition).GetTypeName(usedNamespaces); } else if (obj.GenericType is GetClassTypeReference) { retval = (obj.GenericType as GetClassTypeReference).GetTypeName(usedNamespaces); } else { retval = obj.GenericType.ToString(); } bool isNullable = (retval == "System.Nullable" || retval == "Nullable"); if (isNullable) { retval = ""; } else { retval += "<"; } foreach (ITypeReference arg in obj.TypeArguments) { if (arg != obj.TypeArguments.First()) { retval += ", "; } string typeName; if (arg is ParameterizedTypeReference) { typeName = (arg as ParameterizedTypeReference).GetTypeName(usedNamespaces); } else if (arg is DefaultUnresolvedTypeDefinition) { typeName = (arg as DefaultUnresolvedTypeDefinition).FullName.NormaliseClrName(usedNamespaces); } else if (arg is TypeParameterReference) { typeName = (arg as TypeParameterReference).ToString(); } else if (arg is GetClassTypeReference) { typeName = (arg as GetClassTypeReference).GetTypeName(usedNamespaces); } else { typeName = arg.ToString(); } retval += typeName; } if (isNullable) { retval += "?"; } else { retval += ">"; } retval = retval.NormaliseClrName(usedNamespaces); return(retval); }
static ITypeReference ParseTypeName(string typeName, ref int pos) { string reflectionTypeName = typeName; if (pos == typeName.Length) { throw new ReflectionNameParseException(pos, "Unexpected end"); } ITypeReference result; if (reflectionTypeName[pos] == '`') { // type parameter reference pos++; if (pos == reflectionTypeName.Length) { throw new ReflectionNameParseException(pos, "Unexpected end"); } if (reflectionTypeName[pos] == '`') { // method type parameter reference pos++; int index = ReflectionHelper.ReadTypeParameterCount(reflectionTypeName, ref pos); result = TypeParameterReference.Create(SymbolKind.Method, index); } else { // class type parameter reference int index = ReflectionHelper.ReadTypeParameterCount(reflectionTypeName, ref pos); result = TypeParameterReference.Create(SymbolKind.TypeDefinition, index); } } else { // not a type parameter reference: read the actual type name List <ITypeReference> typeArguments = new List <ITypeReference>(); int typeParameterCount; string typeNameWithoutSuffix = ReadTypeName(typeName, ref pos, true, out typeParameterCount, typeArguments); result = new GetPotentiallyNestedClassTypeReference(typeNameWithoutSuffix, typeParameterCount); while (pos < typeName.Length && typeName[pos] == '.') { pos++; string nestedTypeName = ReadTypeName(typeName, ref pos, false, out typeParameterCount, typeArguments); result = new NestedTypeReference(result, nestedTypeName, typeParameterCount); } if (typeArguments.Count > 0) { result = new ParameterizedTypeReference(result, typeArguments); } } while (pos < typeName.Length) { switch (typeName[pos]) { case '[': int dimensions = 1; do { pos++; if (pos == typeName.Length) { throw new ReflectionNameParseException(pos, "Unexpected end"); } if (typeName[pos] == ',') { dimensions++; } } while (typeName[pos] != ']'); result = new ArrayTypeReference(result, dimensions); break; case '*': result = new PointerTypeReference(result); break; case '@': result = new ByReferenceTypeReference(result); break; default: return(result); } pos++; } return(result); }
static void AppendTypeName(StringBuilder b, ITypeReference type, ITypeResolveContext context) { IType resolvedType = type as IType; if (resolvedType != null) { AppendTypeName(b, resolvedType); return; } GetClassTypeReference gctr = type as GetClassTypeReference; if (gctr != null) { if (!string.IsNullOrEmpty(gctr.Namespace)) { b.Append(gctr.Namespace); b.Append('.'); } b.Append(gctr.Name); if (gctr.TypeParameterCount > 0) { b.Append('`'); b.Append(gctr.TypeParameterCount); } return; } NestedTypeReference ntr = type as NestedTypeReference; if (ntr != null) { AppendTypeName(b, ntr.DeclaringTypeReference, context); b.Append('.'); b.Append(ntr.Name); if (ntr.AdditionalTypeParameterCount > 0) { b.Append('`'); b.Append(ntr.AdditionalTypeParameterCount); } return; } ParameterizedTypeReference pt = type as ParameterizedTypeReference; if (pt != null && IsGetClassTypeReference(pt.GenericType)) { AppendParameterizedTypeName(b, pt.GenericType, pt.TypeArguments, context); return; } ArrayTypeReference array = type as ArrayTypeReference; if (array != null) { AppendTypeName(b, array.ElementType, context); b.Append('['); if (array.Dimensions > 1) { for (int i = 0; i < array.Dimensions; i++) { if (i > 0) { b.Append(','); } b.Append("0:"); } } b.Append(']'); return; } PointerTypeReference ptr = type as PointerTypeReference; if (ptr != null) { AppendTypeName(b, ptr.ElementType, context); b.Append('*'); return; } ByReferenceTypeReference brtr = type as ByReferenceTypeReference; if (brtr != null) { AppendTypeName(b, brtr.ElementType, context); b.Append('@'); return; } if (context == null) { b.Append('?'); } else { AppendTypeName(b, type.Resolve(context)); } }