IContentReader IContentReaderFactory.TryCreate(Type type) { #if W8CORE var attributes = attributeRegistry.GetAttributes(type.GetTypeInfo(), false); #else var attributes = attributeRegistry.GetAttributes(type, false); #endif if (attributes.Count > 0) { foreach (var attribute in attributes) { if (attribute is YamlTagAttribute) { // Make sure that the assembly for type is registered for loading #if W8CORE yamlSettings.RegisterAssembly(type.GetTypeInfo().Assembly); #else yamlSettings.RegisterAssembly(type.Assembly); #endif return(this); } } } return(null); }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="type">The type.</param> /// <param name="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public PrimitiveDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention) : base(attributeRegistry, type, false, namingConvention) { if (!IsPrimitive(type)) { throw new ArgumentException("Type [{0}] is not a primitive"); } // Handle remap for enum items if (type.GetTypeInfo().IsEnum) { foreach (var member in type.GetFields(BindingFlags.Public | BindingFlags.Static)) { var attributes = attributeRegistry.GetAttributes(member); foreach (var attribute in attributes) { var yamlRemap = attribute as YamlRemapAttribute; if (yamlRemap != null) { if (enumRemap == null) { enumRemap = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); } enumRemap[yamlRemap.Name] = member.GetValue(null); } } } } }
/// <summary> /// Initializes a new instance of the <see cref="ObjectDescriptor" /> class. /// </summary> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="type">The type.</param> /// <param name="namingConvention">The naming convention.</param> /// <exception cref="System.ArgumentException">Type [{0}] is not a primitive</exception> public PrimitiveDescriptor(IAttributeRegistry attributeRegistry, Type type, IMemberNamingConvention namingConvention) : base(attributeRegistry, type, false, namingConvention) { if (!IsPrimitive(type)) throw new ArgumentException("Type [{0}] is not a primitive"); // Handle remap for enum items if (type.IsEnum) { foreach (var member in type.GetFields(BindingFlags.Public|BindingFlags.Static)) { var attributes = attributeRegistry.GetAttributes(member); foreach (var attribute in attributes) { var yamlRemap = attribute as YamlRemapAttribute; if (yamlRemap != null) { if (enumRemap == null) { enumRemap = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); } enumRemap[yamlRemap.Name] = member.GetValue(null); } } } } }
/// <summary> /// Gets an attribute associated with the specified member. /// </summary> /// <typeparam name="T">Type of the attribute</typeparam> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="memberInfo">The member information.</param> /// <param name="inherit">if set to <c>true</c> [inherit].</param> /// <returns>An attribute of type {T} if it was found; otherwise <c>null</c></returns> public static T GetAttribute <T>(this IAttributeRegistry attributeRegistry, [NotNull] MemberInfo memberInfo, bool inherit = true) where T : Attribute { var list = attributeRegistry.GetAttributes(memberInfo, inherit); return(list.OfType <T>().FirstOrDefault()); }
/// <summary> /// Gets the attributes associated with the specified member. /// </summary> /// <typeparam name="T">Type of the attribute</typeparam> /// <param name="attributeRegistry">The attribute registry.</param> /// <param name="memberInfo">The member information.</param> /// <param name="inherit">if set to <c>true</c> [inherit].</param> /// <returns>An enumeration of <see cref="Attribute" />.</returns> public static IEnumerable <T> GetAttributes <T>(this IAttributeRegistry attributeRegistry, MemberInfo memberInfo, bool inherit = true) where T : Attribute { return(attributeRegistry.GetAttributes(memberInfo, inherit).OfType <T>()); }
/// <summary> /// Gets the first attribute of type T associated with the specified member. /// </summary> /// <typeparam name="T">Type of the attribute</typeparam> /// <param name="memberInfo">The member information.</param> /// <param name="inherit">if set to <c>true</c> [inherit].</param> /// <returns>An attribute of type {T} if it was found; otherwise <c>null</c> </returns> public static T GetAttribute <T>(this IAttributeRegistry attributeRegistry, MemberInfo memberInfo, bool inherit = true) where T : Attribute { return(attributeRegistry.GetAttributes(memberInfo, inherit).OfType <T>().FirstOrDefault()); }
public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry) { if (assembly == null) throw new ArgumentNullException("assembly"); if (attributeRegistry == null) throw new ArgumentNullException("attributeRegistry"); // Add automatically the assembly for lookup if (!DefaultLookupAssemblies.Contains(assembly) && !lookupAssemblies.Contains(assembly)) { lookupAssemblies.Add(assembly); var types = new Type[0]; // Register all tags automatically. foreach (var type in assembly.GetTypes()) { var attributes = attributeRegistry.GetAttributes(type); foreach (var attribute in attributes) { string name = null; bool isAlias = false; var tagAttribute = attribute as YamlTagAttribute; if (tagAttribute != null) { name = tagAttribute.Tag; } else { var yamlRemap = attribute as YamlRemapAttribute; if (yamlRemap != null) { name = yamlRemap.Name; isAlias = true; } } if (!string.IsNullOrEmpty(name)) { RegisterTagMapping(name, type, isAlias); } } // Automatically register YamlSerializableFactory if (typeof (IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(types) != null) { try { SerializableFactories.Add((IYamlSerializableFactory)Activator.CreateInstance(type)); } catch { // Registrying an assembly should not fail, so we are silently discarding a factory if // we are not able to load it. } } } } }
public void RegisterAssembly(Assembly assembly, IAttributeRegistry attributeRegistry) { if (assembly == null) { throw new ArgumentNullException("assembly"); } if (attributeRegistry == null) { throw new ArgumentNullException("attributeRegistry"); } // Add automatically the assembly for lookup if (!DefaultLookupAssemblies.Contains(assembly) && !lookupAssemblies.Contains(assembly)) { lookupAssemblies.Add(assembly); var types = new Type[0]; // Register all tags automatically. foreach (var type in assembly.GetTypes()) { var attributes = attributeRegistry.GetAttributes(type.GetTypeInfo()); foreach (var attribute in attributes) { string name = null; bool isAlias = false; var tagAttribute = attribute as YamlTagAttribute; if (tagAttribute != null) { name = tagAttribute.Tag; } else { var yamlRemap = attribute as YamlRemapAttribute; if (yamlRemap != null) { name = yamlRemap.Name; isAlias = true; } } if (!string.IsNullOrEmpty(name)) { RegisterTagMapping(name, type, isAlias); } } // Automatically register YamlSerializableFactory if (typeof(IYamlSerializableFactory).IsAssignableFrom(type) && type.GetConstructor(types) != null) { try { SerializableFactories.Add((IYamlSerializableFactory)Activator.CreateInstance(type)); } catch { // Registrying an assembly should not fail, so we are silently discarding a factory if // we are not able to load it. } } } } }