/// <summary> /// Gets the first <see href="Attribute"/> of type <paramref name="attributeType"/> associated with the /// enumeration value given in the <paramref name="provider"/> parameter. /// </summary> /// <param name="provider">An enumeration value on which to search for the attribute.</param> /// <param name="attributeType">The attribute type to search for.</param> /// <returns>The first attribute found on the source.</returns> public static Attribute Attribute(this Enum provider, Type attributeType) { Type type = provider.GetType(); MemberInfo info = type.Member(provider.ToString(), Flags.StaticAnyVisibility | Flags.DeclaredOnly); return(info.Attribute(attributeType)); }
static InjectableInfo CreateForMember(MemberInfo memInfo, Type enclosingType) { var injectAttr = memInfo.Attribute <InjectAttribute>(); var info = new InjectableInfo() { Optional = memInfo.HasAttribute(typeof(InjectOptionalAttribute)), Identifier = (injectAttr == null ? null : injectAttr.Identifier), SourceName = memInfo.Name, EnclosingType = enclosingType, }; if (memInfo is FieldInfo) { var fieldInfo = (FieldInfo)memInfo; info.Setter = ((object injectable, object value) => fieldInfo.SetValue(injectable, value)); info.ContractType = fieldInfo.FieldType; } else { Assert.That(memInfo is PropertyInfo); var propInfo = (PropertyInfo)memInfo; info.Setter = ((object injectable, object value) => propInfo.SetValue(injectable, value, null)); info.ContractType = propInfo.PropertyType; } return(info); }
/// <summary> /// If there is a post processor, run it, else use regular typecast /// </summary> /// <param name="prop">The prop.</param> /// <param name="valueString">The value string.</param> /// <returns></returns> private static object PostProcess(MemberInfo prop, string valueString) { var postProcessors = prop.Attribute <PostRetrievalProcessingAttribute>(); return(postProcessors != null ? postProcessors.Process(valueString, prop.Type()) : CastValue(prop.Type(), valueString)); }
/// <summary> /// <para>Returns a value of either <see cref="DescriptionAttribute"/>, <see cref="DisplayAttribute"/> or <see cref="DisplayNameAttribute"/> (whatever is present and found first) for a given class member.</para> /// </summary> /// <param name="self">Member of the class or <see cref="Type"/> itself.</param> /// <returns>Description for a given class <paramref name="self"/>. If <paramref name="self"/> has a <see cref="DescriptionAttribute"/>, its value is returned. If it has a <see cref="DisplayAttribute"/>, its description property is returned. If it has a <see cref="DisplayNameAttribute"/>, its display name property is returned. If there is neither of these attributes on a <paramref name="self"/>, a <c>null</c> is returned.</returns> /// <exception cref="ArgumentNullException">If <paramref name="self"/> is a <c>null</c> reference.</exception> /// <seealso cref="DescriptionAttribute"/> public static string Description(this MemberInfo self) { Assertion.NotNull(self); var descriptionAttribute = self.Attribute <DescriptionAttribute>(); if (descriptionAttribute != null) { return(descriptionAttribute.Description); } var displayAttribute = self.Attribute <DisplayAttribute>(); if (displayAttribute != null) { return(displayAttribute.Description); } var displayNameAttribute = self.Attribute <DisplayNameAttribute>(); return(displayNameAttribute?.DisplayName); }
private IDefaultValueProvider CreateDefaultValueProvider(MemberInfo member) { var attr = member.Attribute <FlatBuffersDefaultValueAttribute>(); if (attr == null) { return(TypeDefaultValueProvider.Instance); } if (attr.Value == null) { throw new FlatBuffersStructFieldReflectionException("Default value attribute used with null Value"); } return(new AttributeDefaultValueProvider(attr)); }
private void ReflectStructFieldDef(StructTypeDefinition structDef, MemberInfo member) { var valueProvider = CreateValueProvider(member); var defaultValueProvider = CreateDefaultValueProvider(member); var attr = member.Attribute <FlatBuffersFieldAttribute>(); var valueType = valueProvider.ValueType; TypeModel memberTypeModel = null; TypeModel nestedTypeModel = null; if (valueType == typeof(object)) { if (attr == null || (!attr.IsUnionField && !attr.HasNestedFlatBufferType)) { throw new FlatBuffersStructFieldReflectionException("Field with 'object' member must have a UnionType or NestedFlatBufferType declared"); } if (attr.HasNestedFlatBufferType) { memberTypeModel = nestedTypeModel = GetTypeModel(attr.NestedFlatBufferType); } if (attr.IsUnionField) { memberTypeModel = GetTypeModel(attr.UnionType); } } else { if (attr != null && attr.HasNestedFlatBufferType) { throw new FlatBuffersStructFieldReflectionException("HasNestedFlatBufferType can only be used on fields with 'object' member"); } memberTypeModel = GetTypeModel(valueType); } FieldTypeDefinition unionTypeField = null; if (memberTypeModel.IsUnion) { var unionTypeFieldValueProvider = new UnionTypeValueProvider(valueProvider, memberTypeModel); var unionTypeFieldDefaultValueProvider = TypeDefaultValueProvider.Instance; unionTypeField = new FieldTypeDefinition(unionTypeFieldValueProvider, unionTypeFieldDefaultValueProvider) { Name = string.Format("{0}_type", member.Name), TypeModel = GetTypeModel <UnionFieldType>() }; } var field = new FieldTypeDefinition(valueProvider, defaultValueProvider) { Name = member.Name, // TODO: allow attribute override TypeModel = memberTypeModel, }; if (nestedTypeModel != null) { field.NestedFlatBufferType = nestedTypeModel; } ReflectUserMetadata(member, field); if (attr != null) { if (!string.IsNullOrEmpty(attr.Name)) { field.Name = attr.Name; } if (attr.IsIdSetExplicitly) { field.UserIndex = attr.Id; if (unionTypeField != null) { unionTypeField.UserIndex = attr.Id - 1; } } field.Required = attr.Required; field.Deprecated = attr.Deprecated; if (attr.Key) { if (!ValidKeyType(valueType)) { throw new FlatBuffersStructFieldReflectionException("Cannot add '{0}' as a key field. Type must be string or scalar", member.Name); } field.Key = attr.Key; } if (attr.Hash != FlatBuffersHash.None) { if (!ValidHashType(valueType)) { throw new FlatBuffersStructFieldReflectionException("Cannot use Hash setting on '{0}'. Type must be int/uint/long/ulong", member.Name); } field.Hash = attr.Hash; } } if (unionTypeField != null) { structDef.AddField(unionTypeField); field.UnionTypeField = unionTypeField; } structDef.AddField(field); }