1 //---------------------------------------------------------------------
2 // <copyright file="Utils.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 using System.Collections.Specialized;
12 using System.Collections.Generic;
15 using System.Data.Metadata.Edm;
16 using System.Data.EntityModel.SchemaObjectModel;
17 using System.Data.Entity.Design.Common;
18 using System.Diagnostics;
19 using System.Reflection;
22 namespace System.Data.EntityModel.Emitters
25 /// Summary description for Utils
27 internal static class Utils
31 public const string AdoFrameworkNamespace = "System.Data.Objects";
32 public const string AdoFrameworkDataClassesNamespace = "System.Data.Objects.DataClasses";
33 public const string AdoFrameworkMetadataEdmNamespace = "System.Data.Metadata.Edm";
34 public const string AdoEntityClientNamespace = "System.Data.EntityClient";
36 public const string SetValidValueMethodName = "SetValidValue";
37 public const string ReportPropertyChangingMethodName = "ReportPropertyChanging";
38 public const string ReportPropertyChangedMethodName = "ReportPropertyChanged";
39 public const string GetValidValueMethodName = "GetValidValue";
40 public const string VerifyComplexObjectIsNotNullName = "VerifyComplexObjectIsNotNull";
43 // to guarantee uniqueness these must all be unique, begin with and end with an underscore and not contain internal underscores
44 private static string[] _privateMemberPrefixes = new string[(int)PrivateMemberPrefixId.Count]
52 // suffix that is added to field names to create a boolean field used to indicate whether or
53 // not a complex property has been explicitly initialized
54 private static string _complexPropertyInitializedSuffix = "Initialized";
55 private static List<KeyValuePair<string, Type>> _typeReservedNames = InitializeTypeReservedNames();
58 /// Initialize some statics that cannot be initialized in member declaration...
60 static List<KeyValuePair<string, Type>> InitializeTypeReservedNames()
62 Dictionary<string, Type> typeReservedNames = new Dictionary<string, Type>(StringComparer.Ordinal);
63 BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Public
64 | BindingFlags.Instance | BindingFlags.Static;
65 foreach (MemberInfo member in TypeReference.ComplexTypeBaseClassType.GetMembers(bindingFlags))
67 if (ShouldReserveName(member))
69 if (!typeReservedNames.ContainsKey(member.Name))
71 typeReservedNames.Add(member.Name, typeof(ComplexType));
76 foreach (MemberInfo member in TypeReference.EntityTypeBaseClassType.GetMembers(bindingFlags))
78 if (ShouldReserveName(member))
80 if (typeReservedNames.ContainsKey(member.Name))
82 if (typeReservedNames[member.Name] == typeof(ComplexType))
85 typeReservedNames[member.Name] = null;
90 typeReservedNames.Add(member.Name, typeof(EntityType));
95 List<KeyValuePair<string, Type>> pairs = new List<KeyValuePair<string, Type>>();
96 foreach (KeyValuePair<string, Type> pair in typeReservedNames)
104 private static bool ShouldReserveName(MemberInfo member)
106 if (member is EventInfo)
108 return ShouldReserveName((EventInfo)member);
110 else if(member is FieldInfo)
112 return ShouldReserveName((FieldInfo)member);
114 else if(member is MethodBase)
116 return ShouldReserveName((MethodBase)member);
118 else if(member is PropertyInfo)
120 return ShouldReserveName((PropertyInfo)member);
124 Debug.Assert(member is Type, "Did you add a new type of member?");
125 return ShouldReserveName((Type)member);
130 private static bool ShouldReserveName(EventInfo member)
132 bool hasNonPrivate = false;
134 MethodInfo miAdd = member.GetAddMethod();
137 hasNonPrivate |= ShouldReserveName(miAdd, false);
140 MethodInfo miRemove = member.GetRemoveMethod();
141 if (miRemove != null)
143 hasNonPrivate |= ShouldReserveName(miRemove, false);
146 return hasNonPrivate;
149 private static bool ShouldReserveName(PropertyInfo member)
151 bool hasNonPrivate = false;
153 MethodInfo miSet = member.GetSetMethod();
156 hasNonPrivate |= ShouldReserveName(miSet, false);
159 MethodInfo miGet = member.GetGetMethod();
162 hasNonPrivate |= ShouldReserveName(miGet, false);
165 return hasNonPrivate;
168 private static bool ShouldReserveName(FieldInfo member)
170 return !member.IsPrivate && !member.IsAssembly &&
171 !member.IsSpecialName;
174 private static bool ShouldReserveName(MethodBase member, bool checkForSpecial)
176 return !member.IsPrivate && !member.IsAssembly &&
177 (!checkForSpecial || !member.IsSpecialName);
180 private static bool ShouldReserveName(MethodBase member)
182 return ShouldReserveName(member, true);
185 private static bool ShouldReserveName(Type member)
187 // we don't want to keep types
192 #region Public Methods
196 /// <param name="text"></param>
197 /// <returns></returns>
198 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
199 public static string CamelCase(string text)
201 if ( string.IsNullOrEmpty(text) )
204 if ( text.Length == 1 )
205 return text[0].ToString(System.Globalization.CultureInfo.InvariantCulture).ToLowerInvariant();
207 return text[0].ToString(System.Globalization.CultureInfo.InvariantCulture).ToLowerInvariant()+text.Substring(1);
210 public static string FixParameterName(string name)
212 // FxCop consider 'iD' as violation, we will change any property that is 'id'(case insensitive) to 'ID'
213 if (StringComparer.OrdinalIgnoreCase.Equals(name, "id"))
215 // it is an abreviation not an acronym so it should be all lower case
218 return CamelCase(name);
224 /// <param name="propName"></param>
225 /// <returns></returns>
226 public static string FieldNameFromPropName(string propName)
228 return PrivateMemberPrefix(PrivateMemberPrefixId.Field)+propName;
232 /// Generate the name of a field that is used to indicate whether a complex property has been explicitly initialized
234 /// <param name="propName">Name of the property associated that with this field</param>
235 /// <returns>Generated field name</returns>
236 public static string ComplexPropertyInitializedNameFromPropName(string propName)
238 return FieldNameFromPropName(propName) + _complexPropertyInitializedSuffix;
242 /// get the prefix ussed for a private member
244 /// <param name="id"></param>
245 /// <returns></returns>
246 public static string PrivateMemberPrefix(PrivateMemberPrefixId id)
248 return _privateMemberPrefixes[(int)id];
254 /// <param name="name"></param>
255 /// <returns></returns>
256 public static string FQAdoFrameworkName( string name )
258 return AdoFrameworkNamespace + "." + name;
264 /// <param name="name"></param>
265 /// <returns></returns>
266 public static string FQAdoFrameworkDataClassesName( string name )
268 return AdoFrameworkDataClassesNamespace + "." + name;
274 /// <param name="name">unqualifed name of the type</param>
275 /// <returns></returns>
276 public static string FQAdoFrameworkMetadataEdmName(string name)
278 return AdoFrameworkMetadataEdmNamespace + "." + name;
284 /// <param name="name"></param>
285 /// <returns></returns>
286 public static string FQAdoEntityClientName(string name)
288 return AdoEntityClientNamespace + "." + name;
294 /// <param name="element"></param>
295 /// <param name="modelType"></param>
296 /// <returns></returns>
297 public static bool TryGetPrimitiveTypeKind(EdmType type, out PrimitiveTypeKind modelType )
299 if (!MetadataUtil.IsPrimitiveType(type))
301 // set it to something bogus because I have to
302 modelType = PrimitiveTypeKind.Binary;
306 modelType = ((PrimitiveType)type).PrimitiveTypeKind;
313 /// <param name="name"></param>
314 /// <returns></returns>
315 public static string[] SplitName(string name)
317 Debug.Assert(!string.IsNullOrEmpty(name), "name parameter is null or empty");
319 if ( name.Length > 0 && name[0] == '.' )
320 return name.Substring(1).Split('.');
322 return name.Split('.');
325 public static string GetFullyQualifiedCodeGenerationAttributeName(string attribute)
327 return XmlConstants.CodeGenerationSchemaNamespace + ":" + attribute;
331 /// check if a name is reserved for a type
333 /// <param name="type">the object representing the schema type being defined</param>
334 /// <param name="name">the member name</param>
335 /// <returns>true if the name is reserved by the type</returns>
336 public static bool DoesTypeReserveMemberName(StructuralType type, string name, StringComparison comparison)
338 Type reservingType = null;
339 if (!TryGetReservedName(name,comparison, out reservingType))
344 // if reserving types is null it means the name is reserved for all types.
345 if (reservingType == null)
350 return (reservingType == type.GetType());
353 public static bool TryGetReservedName(string name, StringComparison comparison, out Type applyToSpecificType)
355 applyToSpecificType = null;
356 foreach(KeyValuePair<string, Type> pair in _typeReservedNames)
358 if (pair.Key.Equals(name, comparison))
360 applyToSpecificType = pair.Value;