public EnumMapping FindEnumMappingByType(Type type) { if (type == null) { throw Error.ArgumentNull("type"); } if (!type.IsEnum()) { throw Error.Argument("type", "Type {0} is not an enumeration", type.Name); } EnumMapping entry = null; // Try finding a resource with the specified profile first var success = _enumMappingsByType.TryGetValue(type, out entry); if (success) { return(entry); } else { return(null); } }
public static EnumMapping Create(Type enumType) { if (enumType == null) throw Error.ArgumentNull("enumType"); if (!enumType.IsEnum()) throw Error.Argument("enumType", "Type {0} is not an enumerated type", enumType.Name); var result = new EnumMapping(); result.Name = getEnumName(enumType); result.EnumType = enumType; result._enumToLiteral = new Dictionary<Enum, string>(); result._literalToEnum = new Dictionary<string, Enum>(); foreach(var enumValue in ReflectionHelper.FindEnumFields(enumType)) { var attr = ReflectionHelper.GetAttribute<EnumLiteralAttribute>(enumValue); string literal = enumValue.Name; if (attr != null) literal = attr.Literal; Enum value = (Enum)enumValue.GetValue(null); result._enumToLiteral.Add(value, literal); result._literalToEnum.Add(literal, value); } return result; }
internal EnumMapping ImportEnum(Type type) { EnumMapping mapping = null; if (!EnumMapping.IsMappableEnum(type)) { throw Error.Argument("type", "Type {0} is not a mappable enumeration", type.Name); } lock (lockObject) { mapping = FindEnumMappingByType(type); if (mapping != null) { return(mapping); } mapping = EnumMapping.Create(type); _enumMappingsByType[type] = mapping; Message.Info("Created Enum mapping for newly encountered type {0}", type.Name); } return(mapping); }
public static object ParseLiteral(string rawValue, Type enumType) { EnumMapping fieldInfo = null; lock (_cacheLock) { if (!_cache.TryGetValue(enumType, out fieldInfo)) { fieldInfo = EnumMapping.Create(enumType); _cache.Add(enumType, fieldInfo); } } return((object)fieldInfo.ParseLiteral(rawValue)); //foreach (var enumValue in fieldInfo) //{ // var attr = ReflectionHelper.GetAttribute<EnumLiteralAttribute>(enumValue); // if (attr != null) // { // if (attr.Literal == rawValue) // { // return (T)enumValue.GetValue(null); // } // } //} //return null; }
private static EnumMapping GetEnumMapping(Type enumType) { EnumMapping fieldInfo = null; lock ( _cacheLock ) { if (!_cache.TryGetValue(enumType, out fieldInfo)) { fieldInfo = EnumMapping.Create(enumType); _cache.Add(enumType, fieldInfo); } } return(fieldInfo); }
public void Import(Assembly assembly) { if (assembly == null) { throw Error.ArgumentNull("assembly"); } if (Attribute.GetCustomAttribute(assembly, typeof(NotMappedAttribute)) != null) { return; } foreach (Type type in assembly.GetExportedTypes()) { // Don't import types marked with [NotMapped] if (Attribute.GetCustomAttribute(type, typeof(NotMappedAttribute)) != null) { continue; } if (type.IsEnum) { // Map an enumeration if (EnumMapping.IsMappableEnum(type)) { ImportEnum(type); } else { Message.Info("Skipped enum {0} while doing inspection: not recognized as representing a FHIR enumeration", type.Name); } } else { // Map a Fhir Datatype if (ClassMapping.IsMappableType(type)) { ImportType(type); } else { Message.Info("Skipped type {0} while doing inspection: not recognized as representing a FHIR type", type.Name); } } } }
public void Import(Assembly assembly) { if (assembly == null) { throw Error.ArgumentNull("assembly"); } #if (PORTABLE45 || NETCOREAPP1_1) if (assembly.GetCustomAttribute <NotMappedAttribute>() != null) { return; } #else if (Attribute.GetCustomAttribute(assembly, typeof(NotMappedAttribute)) != null) { return; } #endif #if (PORTABLE45 || NETCOREAPP1_1) IEnumerable <Type> exportedTypes = assembly.ExportedTypes; #else Type[] exportedTypes = assembly.GetExportedTypes(); #endif foreach (Type type in exportedTypes) { // Don't import types marked with [NotMapped] #if (PORTABLE45 || NETCOREAPP1_1) if (type.GetTypeInfo().GetCustomAttribute <NotMappedAttribute>() != null) { continue; } #else if (Attribute.GetCustomAttribute(type, typeof(NotMappedAttribute)) != null) { continue; } #endif if (type.IsEnum()) { // Map an enumeration if (EnumMapping.IsMappableEnum(type)) { ImportEnum(type); } else { Message.Info("Skipped enum {0} while doing inspection: not recognized as representing a FHIR enumeration", type.Name); } } else { // Map a Fhir Datatype if (ClassMapping.IsMappableType(type)) { ImportType(type); } else { Message.Info("Skipped type {0} while doing inspection: not recognized as representing a FHIR type", type.Name); } } } }