/// <summary> /// Formats type names in human-readable form. This removes special characters in generic type names /// and converts System.String to string, etc. /// </summary> /// <param name="sb">The string builder instance that contains the context for pretty printing.</param> /// <param name="t">The type to be pretty printed.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="sb"/> is null.</exception> public static void FormatTypeName(StringBuilder sb, Type t) { if (sb == null) { throw new ArgumentNullException("sb"); } if (t.IsGenericType) { string name = t.Name; int tailPos = name.IndexOf('`'); sb.Append(tailPos > 0 ? name.Substring(0, tailPos) : name); } else { // lookup for builtins like int and string, otherwise use type name string name; if (!AbstractValue.GetLiteralTypes().TryGetValue(t, out name)) { name = t.Name; } sb.Append(name); } }
/// <summary> /// Get the default type of the sort /// </summary> public Type DefaultSortType(Symbol sort) { Type result; if (sortType.TryGetValue(sort, out result)) { return(result); } string[] namespaces = new string[] { "NModel", "NModel.Internals", "NModel.Terms", "System" }; if (sort.DomainParameters != null && sort.DomainParameters.Count > 0) { int nParameters = sort.DomainParameters.Count; Symbol genericSort = new Symbol(sort.Name, Sequence <Symbol> .EmptySequence); Type genericTypeDefinition = DefaultSortType(genericSort); Type[] args = new Type[nParameters]; int i = 0; foreach (Symbol argSort in sort.DomainParameters) { args[i] = DefaultSortType(argSort); i += 1; } result = genericTypeDefinition.MakeGenericType(args); } else if (sort.DomainParameters != null) { // to do: fix problem of missing arity int[] nParameters = new int[] { 1, 2, 3, 4 }; // assert sort.DomainParameters.Count == 0; Type /*?*/ genericType = null; foreach (string ns in namespaces) { foreach (int i in nParameters) { genericType = Type.GetType(ns + "." + sort.Name + "`" + i.ToString()); if (genericType != null) { break; } } if (genericType != null) { break; } } if (genericType == null) { throw new ArgumentException("Can't find type for sort " + sort.ToString()); } result = genericType; } else if (AbstractValue.IsLiteralSort(sort)) { result = AbstractValue.GetLiteralSortType(sort); } else { foreach (string ns in namespaces) { result = Type.GetType(ns + "." + sort.Name); if (result != null) { break; } } } if (result == null) { // If the execution got here it means that it didn't find the type of sort // in this model and in the NModel framework. // Check for the type in the AppDomain assemblies - // Is it a type of another model program that is being composed with this one result = getSortTypeFromAppDomainAssemblies(namespaces, sort.Name); } if (result == null) { throw new ArgumentException("No default type for sort " + sort.ToString()); } this.RegisterSortType(sort, result); return(result); }