/// <summary> /// Initializes a new instance of the SerializableMember class. /// </summary> /// <param name="member">The member to serialize.</param> private SerializableMember(MemberInfo member) { Debug.Assert(member != null, "member cannot be null!"); Debug.Assert(!member.Has<IgnoreDataMemberAttribute>(), "member should be ignored!"); this.Name = member.Name; // May be overwritten by DataMember below this.MemberName = member.Name; DataMemberAttribute dataMemberAttribute = member.GetCustomAttribute<DataMemberAttribute>(true); if (dataMemberAttribute != null) { if (!string.IsNullOrWhiteSpace(dataMemberAttribute.Name)) { this.Name = dataMemberAttribute.Name; } this.IsRequired = dataMemberAttribute.IsRequired; this.Order = dataMemberAttribute.Order; } DataMemberJsonConverterAttribute converter = member.GetCustomAttribute<DataMemberJsonConverterAttribute>(true); if (converter != null && converter.ConverterType != null) { this.Converter = SerializableType.GetConverter(converter.ConverterType); } }
public MemberData(Type type, MemberInfo memberInfo) { Type = type; ServiceKey = memberInfo.GetCustomAttribute<ServiceKeyAttribute>()?.Key; InjectableAttribute = memberInfo.GetCustomAttribute<InjectableAttribute>(); }
protected PropertyOrField(MemberInfo memberInfo) { MemberInfo = memberInfo; Base64Encoded = memberInfo.GetCustomAttribute<SerializeAsBase64Attribute>() != null; SerializeNull = memberInfo.GetCustomAttribute<SerializeNullAttribute>() != null; HasParameterAttribute = memberInfo.GetCustomAttribute<ParameterAttribute>() != null; }
public string GetName(MemberInfo member, bool includeProtocolProps = true) { Guard.ArgumentNotNull(member, "member"); if (includeProtocolProps) { var protocolAttr = member.GetCustomAttribute<SDataProtocolPropertyAttribute>(); if (protocolAttr != null) { var name = protocolAttr.Value != null ? protocolAttr.Value.ToString() : member.Name; if (char.IsUpper(name[0])) { name = char.ToLowerInvariant(name[0]) + name.Substring(1); } return "$" + name; } } #if !NET_2_0 var contractAttr = member.GetCustomAttribute<DataContractAttribute>(); if (contractAttr != null && !string.IsNullOrEmpty(contractAttr.Name)) { return contractAttr.Name; } var memberAttr = member.GetCustomAttribute<DataMemberAttribute>(); if (memberAttr != null && !string.IsNullOrEmpty(memberAttr.Name)) { return memberAttr.Name; } #endif var elementAttr = member.GetCustomAttribute<XmlElementAttribute>(); if (elementAttr != null && !string.IsNullOrEmpty(elementAttr.ElementName)) { return elementAttr.ElementName; } var attributeAttr = member.GetCustomAttribute<XmlAttributeAttribute>(); if (attributeAttr != null && !string.IsNullOrEmpty(attributeAttr.AttributeName)) { return attributeAttr.AttributeName; } var arrayAttr = member.GetCustomAttribute<XmlArrayAttribute>(); if (arrayAttr != null && !string.IsNullOrEmpty(arrayAttr.ElementName)) { return arrayAttr.ElementName; } var serviceAttr = member.GetCustomAttribute<SDataServiceOperationAttribute>(); if (serviceAttr != null && !string.IsNullOrEmpty(serviceAttr.Name)) { return serviceAttr.Name; } return _transform(member.Name); }
public override string GetIndexFieldName(MemberInfo member) { var memberAttribute = GetIndexFieldNameFormatterAttribute(member); if (memberAttribute != null) return base.GetIndexFieldName(member); var fieldConfig = member.GetCustomAttribute<SitecoreFieldAttribute>(true); if (fieldConfig == null && member.DeclaringType != null) { var interfaceFromProperty = member.DeclaringType.GetInterfaces().FirstOrDefault(inter => inter.GetProperty(member.Name) != null); if (interfaceFromProperty != null) { fieldConfig = interfaceFromProperty.GetMember(member.Name).First().GetCustomAttribute<SitecoreFieldAttribute>(true); } } if (fieldConfig != null && !string.IsNullOrEmpty(fieldConfig.FieldName)) return base.GetIndexFieldName(fieldConfig.FieldName); if (fieldConfig == null) { var infoConfig = member.GetCustomAttribute<SitecoreInfoAttribute>(true); if (infoConfig == null && member.DeclaringType != null) { var interfaceFromProperty = member.DeclaringType.GetInterfaces().FirstOrDefault(inter => inter.GetProperty(member.Name) != null); if (interfaceFromProperty != null) { infoConfig = interfaceFromProperty.GetMember(member.Name).First().GetCustomAttribute<SitecoreInfoAttribute>(true); } } if (infoConfig != null) { switch (infoConfig.Type) { case SitecoreInfoType.DisplayName: return base.GetIndexFieldName(BuiltinFields.DisplayName); case SitecoreInfoType.FullPath: return base.GetIndexFieldName(BuiltinFields.FullPath); case SitecoreInfoType.TemplateId: return base.GetIndexFieldName(BuiltinFields.Template); case SitecoreInfoType.TemplateName: return base.GetIndexFieldName(BuiltinFields.TemplateName); case SitecoreInfoType.Url: return base.GetIndexFieldName(BuiltinFields.Url); case SitecoreInfoType.Version: return base.GetIndexFieldName(BuiltinFields.Version); case SitecoreInfoType.Name: return base.GetIndexFieldName(BuiltinFields.Name); case SitecoreInfoType.Language: return base.GetIndexFieldName(BuiltinFields.Language); } } } return base.GetIndexFieldName(member); }
private static void Process(MemberInfo member, ISpecification holder) { // give priority to Idempotent as more restrictive if (member.GetCustomAttribute<IdempotentAttribute>() != null) { FacetUtils.AddFacet(new IdempotentFacet(holder)); } else if (member.GetCustomAttribute<QueryOnlyAttribute>() != null) { FacetUtils.AddFacet(new QueryOnlyFacet(holder)); } }
public static MetaColumn Create(MemberInfo member) { var databaseGeneratedAttr = member.GetCustomAttribute<DatabaseGeneratedAttribute>(); return new MetaColumn { Member = member, ColumnName = GetColumnName(member), IsKey = member.GetCustomAttribute<KeyAttribute>() != null, IsIdentity = databaseGeneratedAttr != null && databaseGeneratedAttr.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity, IsCompute = databaseGeneratedAttr != null && databaseGeneratedAttr.DatabaseGeneratedOption == DatabaseGeneratedOption.Computed, }; }
private static string GetColumnName(MemberInfo member) { var columnAttr = member.GetCustomAttribute<ColumnAttribute>(); return columnAttr != null && !string.IsNullOrWhiteSpace(columnAttr.Name) ? columnAttr.Name : member.Name; }
private static void UpdateDefaultValue(MemberInfo memberInfo, MetaProperty info) { var defaultAttribute = memberInfo.GetCustomAttribute<DefaultValueAttribute>(); if (defaultAttribute == null) return; info.DefaultValue = defaultAttribute.Value; }
public static string GetCommandName(MemberInfo t) { var attr = t.GetCustomAttribute<CommandAttribute>(); if (attr != null) return attr.Name; else return null; }
private static bool HasAttribute(MemberInfo mi, object o) { if (mi.DeclaringType.GetCustomAttribute<AppSettingAttribute>() != null) { return true; } return mi.GetCustomAttribute<AppSettingAttribute>() != null; }
private BrowserTypes GetExecutionBrowserMethodLevel(MemberInfo memberInfo) { var executionBrowserAttribute = memberInfo.GetCustomAttribute<ExecutionBrowserAttribute>(true); if (executionBrowserAttribute != null) { return executionBrowserAttribute.BrowserType; } return BrowserTypes.NotSet; }
public void SwaggerDtoPropertiesShouldHaveSwaggerPropertyAttribute(MemberInfo member) { var swaggerProperty = member.GetCustomAttribute<SwaggerPropertyAttribute>(); if (swaggerProperty != null) { swaggerProperty.Name.ShouldNotBeNull(); return; } var enumValue = member.GetCustomAttribute<SwaggerEnumValueAttribute>(); if (enumValue != null) { enumValue.Value.ShouldNotBeNull(); return; } throw new Exception(string.Format( "Member {0} is missing SwaggerProperty- or SwaggerEnumValueAttribute.", GetDisplayName(member))); }
/// <summary> /// Gets the XML identifier of the member. /// </summary> /// <param name="memberInfo">The information about the member to use.</param> /// <returns>The XML identifier of the member.</returns> public static string GetIdentifier(MemberInfo memberInfo) { var nameAttribute = memberInfo.GetCustomAttribute<XmlConvertCustomElementAttribute>(); if (nameAttribute != null) { return nameAttribute.Name; } return memberInfo.Name; }
// Note: this method is called just once for a given property; Newtonsoft serializer caches metadata information, // so the result is cached and reused. We are not concerned with efficiency here protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { var property = base.CreateProperty(member, memberSerialization); var nodeAttr = member.GetCustomAttribute<NodeAttribute>(inherit: true); if (nodeAttr != null) property.PropertyName = nodeAttr.Name; else if (ChangeToCamelCase) property.PropertyName = property.PropertyName.ToCamelCase(); return property; }
private void ThrowExceptionIfOwnerAttributeNotSet(MemberInfo memberInfo) { try { memberInfo.GetCustomAttribute<OwnerAttribute>(true); } catch { throw new Exception("You have to set Owner of your test before you run it"); } }
private static void UpdateSPFieldInfo(MemberInfo memberInfo, MetaProperty info) { var fieldAttribute = memberInfo.GetCustomAttribute<SpFieldAttribute>(); if (fieldAttribute == null) { throw new ArgumentException(string.Format("Member {0} has no attribute SpFieldAttribute", memberInfo.Name), "memberInfo"); } info.SpFieldInternalName = fieldAttribute.InternalName ?? info.MemberName; info.CustomConverterType = fieldAttribute.CustomConverterType; }
protected override JsonProperty CreateProperty (MemberInfo member, MemberSerialization memberSerialization) { JsonProperty property = base.CreateProperty(member, memberSerialization); // Skip serialization of empty collections. if ((property.DefaultValueHandling ?? DefaultValueHandling.Ignore).Has(DefaultValueHandling.Ignore) && IsPropertyCollection(property)) { Predicate<object> shouldSerialize = obj => { var collection = property.ValueProvider.GetValue(obj) as ICollection; return collection == null || collection.Count != 0; }; property.ShouldSerialize = property.ShouldSerialize.Merge(shouldSerialize, (a, b) => a && b); } // Apply linked collection attributes. var linkedCollectionAttr = member.GetCustomAttribute<JsonLinkedCollectionAttribute>(); var originCollectionAttr = linkedCollectionAttr as JsonOriginCollectionAttribute; if (originCollectionAttr != null && property.ItemConverter == null) property.ItemConverter = _originConverter; var linkCollectionAttr = linkedCollectionAttr as JsonLinkCollectionAttribute; if (linkCollectionAttr != null && property.ItemConverter == null) property.ItemConverter = _linkConverter; // Apply linked object attributes. var linkedAttr = member.GetCustomAttribute<JsonLinkedAttribute>(); var originAttr = linkedAttr as JsonOriginAttribute; if (originAttr != null) { if (property.Converter == null) property.Converter = _originConverter; if (property.MemberConverter == null) property.MemberConverter = _originConverter; } var linkAttr = linkedAttr as JsonLinkAttribute; if (linkAttr != null) { if (property.Converter == null) property.Converter = _linkConverter; if (property.MemberConverter == null) property.MemberConverter = _linkConverter; } return property; }
/// <summary> /// Overrides this method to exclude properties with [Ignore] attribute or those that are in the given list. /// </summary> protected override JsonProperty CreateProperty( MemberInfo member, MemberSerialization memberSerialization ) { var property = base.CreateProperty(member, memberSerialization); // Don't serialize properties that are decorated with [Ignore] or whose name are in the given list. if ( member.GetCustomAttribute(typeof(BaseVM.IgnoreAttribute)) != null ) property.Ignored = true; else if ( _ignoredPropertyNames != null && _ignoredPropertyNames.Contains(property.PropertyName) ) property.Ignored = true; return property; }
private Expression ExpressionForMember(Expression objExpression, MemberInfo member, Type memberType, ParameterExpression registrationContext) { var attribute = member.GetCustomAttribute<InjectAttribute>(true); if (attribute == null) return null; var memberAccess = Expression.MakeMemberAccess(objExpression, member); var memberValue = this.parentContext.GetSingleRegistration(memberType, attribute.Key, true).GetInstanceExpression(registrationContext); var assign = Expression.Assign(memberAccess, memberValue); // Only actually do the assignment if the field/property is currently null return Expression.IfThen(Expression.Equal(memberAccess, Expression.Constant(null, memberType)), assign); }
public static bool IsSubcategoryOf(this SubCategory sub, Category cat) { Type t = typeof(SubCategory); System.Reflection.MemberInfo mi = t.GetMember(sub.ToString()).FirstOrDefault(m => m.GetCustomAttribute(typeof(SubcategoryOf)) != null); if (mi == null) { throw new ArgumentException("Subcategory " + sub + " has no category."); } SubcategoryOf subAttr = (SubcategoryOf)mi.GetCustomAttribute(typeof(SubcategoryOf)); return(subAttr.Category == cat); }
protected override bool Validate(MemberInfo member) { if (member == null) { throw new ArgumentNullException(); } if (!(member is Type)) { throw new ArgumentException("The given member is no class."); } if (member.GetCustomAttribute<JUUTTestClassAttribute>() != null) { return true; } throw new ArgumentException("The class " + member.Name + " doesn't have the JUUTTestClass-Attribute."); }
private void initByAttributes(MemberInfo mi) { DeserializerFactory = mi.GetCustomAttributes(false).OfType<IDeserializerFactory>().SingleOrDefault(); var dmAttr = mi.GetCustomAttribute<DataMemberAttribute>(); if (dmAttr == null) return; Required = dmAttr.IsRequired; if (string.IsNullOrWhiteSpace(dmAttr.Name)) return; Name = dmAttr.Name; }
public static float GetMemberDisplayOrder(MemberInfo member) { var attribute = member.GetCustomAttribute<DisplayAttribute>(); if (attribute != null && attribute.DisplayOrder.HasValue) return attribute.Order; switch (member.MemberType) { case MemberTypes.Field: return 100f; case MemberTypes.Property: return 200f; case MemberTypes.Method: return 300f; default: throw new NotSupportedException(); } }
public static Operation CreateOperationObject(MemberInfo method) { Operation op = new Operation(); OperationAttribute operationAttrib = method.GetCustomAttribute<OperationAttribute>(); if (operationAttrib == null) throw new Exception(string.Format("The custom operation '{0}', doesn't contain the OperationAttribute.", method.Name)); op.Verb = operationAttrib.Verb.ToString(); op.Static = operationAttrib.Static; op.Path = operationAttrib.Path; op.Response = new Response(); op.Response.ContentType = operationAttrib.ResponseContentType; op.Response.Type = operationAttrib.ResponseType; if (!string.IsNullOrEmpty(operationAttrib.ErrorResponseType)) { op.ErrorResponse = new ErrorResponse(); op.ErrorResponse.Type = operationAttrib.ErrorResponseType; if (!string.IsNullOrEmpty(operationAttrib.ErrorResponseProperties)) { string[] errorProperties = operationAttrib.ErrorResponseProperties.Split(';'); op.ErrorResponse.Properties = new Dictionary<string, string>(); foreach(string errorProperty in errorProperties) { if (!string.IsNullOrEmpty(errorProperty)) { string[] splitProperty = errorProperty.Split(':'); if (splitProperty.Length == 2) op.ErrorResponse.Properties.Add(splitProperty[0], splitProperty[1]); } } } } foreach(ParamAttribute paramAttribute in method.GetCustomAttributes<ParamAttribute>()) { if (op.Parameters == null) op.Parameters = new Dictionary<string, VSPlugin.Parameters>(); op.Parameters.Add(paramAttribute.Name, new VSPlugin.Parameters() { Kind = paramAttribute.Kind.ToStringAPS(), Required = paramAttribute.Required, Type = Utility.ConvertType2APSType(paramAttribute.Type) }); } return op; }
private bool IsOptionalProperty(MemberInfo prop) { switch (Options.OptionalMemberGenerationMode) { case OptionalMemberGenerationMode.None: return(false); case OptionalMemberGenerationMode.UseDataMemberAttribute: var dataMemberAttribute = prop.GetCustomAttribute <DataMemberAttribute>(true); return(dataMemberAttribute != null && !dataMemberAttribute.IsRequired); default: throw new NotSupportedException($"Specified required member discovery option is not supported: {Options.OptionalMemberGenerationMode}."); } }
protected override bool Validate(MemberInfo member) { MethodInfo method = (MethodInfo) member; if (member.GetCustomAttribute<ClassSetUpAttribute>() != null) { if (!method.IsStatic) { throw new ArgumentException("The method " + method.Name + " isn't static."); } if (method.GetParameters().Count() != 0) { throw new ArgumentException("The method " + method.Name + " has parameters."); } return true; } return false; }
public MemberData(object source, MemberInfo member) { Source = source; Member = member; var valueConverterAttribute = member.GetCustomAttribute<ValueConverterAttribute>(); if (valueConverterAttribute != null && !(valueConverterAttribute is CellViewTemplate)) { ValueConverter = valueConverterAttribute.ValueConverter; ConverterParameter = valueConverterAttribute.ConverterParameter; ConverterParameterName = valueConverterAttribute.ConverterParameterPropertyName; } UpdateValue(); Id = CreateId(); }
public static Attribute GetCustomAttribute(MemberInfo member, Type memberType) { if (member == null) { throw new NullReferenceException("Must supply the member parameter"); } if (memberType == null) { throw new NullReferenceException("Must supply the memberType parameter"); } #if WINRT return member.GetCustomAttribute(memberType); #else return Attribute.GetCustomAttribute(member, memberType); #endif }
/// <summary> /// Gets the name specified for use in Json serialization. /// </summary> /// <param name="value"></param> /// <returns></returns> public static string GetJsonSpecifiedProperty(MemberInfo memberInfo) { #if NETFX_CORE if (memberInfo == null) return null; var attribute = memberInfo.GetCustomAttribute<JsonSpecifiedPropertyAttribute>(true); if (attribute == null) { return null; } #else if (memberInfo == null || !Attribute.IsDefined(memberInfo, typeof(JsonSpecifiedPropertyAttribute))) { return null; } JsonSpecifiedPropertyAttribute attribute = (JsonSpecifiedPropertyAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(JsonSpecifiedPropertyAttribute)); #endif return attribute.SpecifiedProperty; }
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) { var property = base.CreateProperty(member, memberSerialization); var required = member.GetCustomAttribute(typeof(RequiredAttribute), inherit: true); if (required != null) { var propertyType = ((PropertyInfo)member).PropertyType; // DefaultObjectValidator does required attribute validation on properties based on the property // value being null. Since this is not possible in case of value types, we depend on the formatters // to handle value type validation. // With the following settings here, if a value is not present on the wire for value types // like primitive, struct etc., Json.net's serializer would throw exception which we catch // and add it to model state. if (propertyType.IsValueType() && !propertyType.IsNullableValueType()) { property.Required = Required.AllowNull; } } return property; }
private static bool ShouldDisplayMember(MemberInfo member, bool hasDataContractAttribute) { JsonIgnoreAttribute jsonIgnore = member.GetCustomAttribute<JsonIgnoreAttribute>(); XmlIgnoreAttribute xmlIgnore = member.GetCustomAttribute<XmlIgnoreAttribute>(); IgnoreDataMemberAttribute ignoreDataMember = member.GetCustomAttribute<IgnoreDataMemberAttribute>(); NonSerializedAttribute nonSerialized = member.GetCustomAttribute<NonSerializedAttribute>(); ApiExplorerSettingsAttribute apiExplorerSetting = member.GetCustomAttribute<ApiExplorerSettingsAttribute>(); bool hasMemberAttribute = member.DeclaringType.IsEnum ? member.GetCustomAttribute<EnumMemberAttribute>() != null : member.GetCustomAttribute<DataMemberAttribute>() != null; // Display member only if all the followings are true: // no JsonIgnoreAttribute // no XmlIgnoreAttribute // no IgnoreDataMemberAttribute // no NonSerializedAttribute // no ApiExplorerSettingsAttribute with IgnoreApi set to true // no DataContractAttribute without DataMemberAttribute or EnumMemberAttribute return jsonIgnore == null && xmlIgnore == null && ignoreDataMember == null && nonSerialized == null && (apiExplorerSetting == null || !apiExplorerSetting.IgnoreApi) && (!hasDataContractAttribute || hasMemberAttribute); }