/// <summary> /// Registers an individual type name with the underlying codegen infrastructure. /// </summary> /// <param name="type">The type to register.</param> /// <param name="containingNamespace">The containing namespace.</param> private void RegisterTypeName(Type type, string containingNamespace) { if (string.IsNullOrEmpty(type.Namespace)) { this.LogError(string.Format(CultureInfo.CurrentCulture, Resource.ClientCodeGen_Namespace_Required, type)); return; } // Check if we're in conflict if (!CodeGenUtilities.RegisterTypeName(type, containingNamespace)) { // Aggressively check for potential conflicts across other DomainService entity types. IEnumerable <Type> potentialConflicts = // Entity types with namespace matches this.DomainServiceDescriptions .SelectMany <DomainServiceDescription, Type>(d => d.EntityTypes) .Where(entity => entity.Namespace == type.Namespace) .Concat( // DomainService types with namespace matches this.DomainServiceDescriptions .Select(d => d.DomainServiceType) .Where(dst => dst.Namespace == type.Namespace)).Distinct(); foreach (Type potentialConflict in potentialConflicts) { // Register potential conflicts so we qualify type names correctly // later during codegen. CodeGenUtilities.RegisterTypeName(potentialConflict, containingNamespace); } } }
/// <summary> /// Returns a <see cref="Type"/> name that is safe to use. /// </summary> /// <remarks> /// This method is not safe for use with generic types. /// </remarks> /// <param name="typeFullName">The full name of the <see cref="Type"/>.</param> /// <param name="containingNamespace">The containing namespace.</param> /// <param name="userType">A <see cref="Boolean"/> indicating whether or not the <paramref name="typeFullName"/> is a user type.</param> /// <returns>A string representing the safe type name.</returns> private static string GetSafeTypeName(string typeFullName, string containingNamespace, bool userType) { if (string.IsNullOrEmpty(typeFullName)) { throw new ArgumentNullException(nameof(typeFullName)); } string typeName = typeFullName; string typeNamespace = string.Empty; int idx = typeFullName.LastIndexOf('.'); if (idx != -1) { typeName = typeFullName.Substring(idx + 1); typeNamespace = typeFullName.Substring(0, idx); } if (useFullTypeNames) { bool prependRootNamespace = isVisualBasic && userType && !string.IsNullOrEmpty(rootNamespace) && !(typeNamespace.Equals(rootNamespace, StringComparison.Ordinal) || typeNamespace.StartsWith(rootNamespace + ".", StringComparison.Ordinal)); if (prependRootNamespace) { typeFullName = rootNamespace + "." + typeFullName; } return(typeFullName); } bool useFullName = CodeGenUtilities.RegisterTypeName(typeNamespace, typeName, containingNamespace); return(useFullName ? typeFullName : typeName); }
/// <summary> /// Checks if a using a <see cref="Type"/>'s short-name will generate an ambiguous type reference error. /// </summary> /// <param name="type">The <see cref="Type"/> to examine.</param> /// <param name="containingNamespace">The namespace in which to check for <see cref="Type"/> short-name conflicts.</param> /// <returns>A Boolean indicating whether or not the <see cref="Type"/> will generate an ambiguous type /// reference error.</returns> internal static bool RegisterTypeName(Type type, string containingNamespace) { // Short-circuit in case of global 'use full type names' flag if (useFullTypeNames) { return(true); } // Translate generic types if (type.IsGenericType) { type = type.GetGenericTypeDefinition(); } return(CodeGenUtilities.RegisterTypeName(type.Namespace, type.Name, containingNamespace)); }