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)); }
internal SettingsLogs Apply(PrintJob job) { SettingsLogs logs = new SettingsLogs(); PropertyInfo[] properties = this.GetType().GetProperties(); for (int t = properties.Length - 1; t >= 0; t--) { PropertyInfo prop = properties[t]; Type propType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType; if (NativelySupportedTypes.Contains(propType) || typeof(Enum).IsAssignableFrom(propType)) { XmlIgnoreAttribute ignore = prop.GetCustomAttribute <XmlIgnoreAttribute>(true); if (!ReferenceEquals(ignore, null)) { continue; } object value = prop.GetValue(this); if (!ReferenceEquals(value, null)) { string fullName = Path + prop.Name; string val = value.ToString(); try { job.SetProfileSetting(fullName, val); logs.AppliedSettings.Add(string.Concat(fullName, " = \"", val, "\"")); }catch (Exception ex) { logs.Errors.Add(string.Concat("Error while setting \"", fullName, "\" with value \"", val, "\": ", ex.Message)); } } } else if (typeof(PdfCreatorSettingsBase).IsAssignableFrom(propType)) { PdfCreatorSettingsBase sub = prop.GetValue(this) as PdfCreatorSettingsBase; if (!ReferenceEquals(sub, null)) { SettingsLogs l = sub.Apply(job); logs.Errors.AddRange(l.Errors); logs.AppliedSettings.AddRange(l.AppliedSettings); } } else { System.Diagnostics.Debugger.Break(); } } return(logs); }
public void TestMembersWithPrimitiveNoDataMembersGeneration() { ProjectMappingManagerSetup.InitializeManager(ServiceProvider, "ProjectMapping.DataContractDsl.Tests.xml"); DataContract rootElement = CreateDefaultDataContract(); rootElement.DataMembers.AddRange(LoadPrimitiveDataElements(null, false, false)); string content = RunTemplate(rootElement); Type generatedType = CompileAndGetType(content); TypeAsserter.AssertField(PrimitiveDataElementType1, PrimitiveDataElementName1, generatedType); TypeAsserter.AssertField(PrimitiveDataElementType2, PrimitiveDataElementName2, generatedType); XmlIgnoreAttribute xmlIgnoreAttr = TypeAsserter.AssertAttribute <XmlIgnoreAttribute>(generatedType.GetProperty(PrimitiveDataElementName1)); Assert.IsNotNull(xmlIgnoreAttr); }
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; return(jsonIgnore == null && xmlIgnore == null && ignoreDataMember == null && nonSerialized == null && (apiExplorerSetting == null || !apiExplorerSetting.IgnoreApi) && (!hasDataContractAttribute || hasMemberAttribute)); }
bool getDefaultValue(PropertyInfo pi, List<Style> styling, out object defaultValue){ defaultValue = null; string name = ""; XmlIgnoreAttribute xia = (XmlIgnoreAttribute)pi.GetCustomAttribute (typeof(XmlIgnoreAttribute)); if (xia != null) return false; XmlAttributeAttribute xaa = (XmlAttributeAttribute)pi.GetCustomAttribute (typeof(XmlAttributeAttribute)); if (xaa != null) { if (string.IsNullOrEmpty (xaa.AttributeName)) name = pi.Name; else name = xaa.AttributeName; } int styleIndex = -1; if (styling.Count > 0){ for (int i = 0; i < styling.Count; i++) { if (styling[i].ContainsKey (name)){ styleIndex = i; break; } } } if (styleIndex >= 0){ if (pi.PropertyType.IsEnum)//maybe should be in parser.. defaultValue = Enum.Parse(pi.PropertyType, (string)styling[styleIndex] [name], true); else defaultValue = styling[styleIndex] [name]; }else { DefaultValueAttribute dv = (DefaultValueAttribute)pi.GetCustomAttribute (typeof (DefaultValueAttribute)); if (dv == null) return false; defaultValue = dv.Value; } return true; }
private void DiscoverRecordFields(Type recordType, string declaringMember, bool optIn = false) { if (recordType == null) { return; } if (!recordType.IsDynamicType()) { IsComplexXPathUsed = false; Type pt = null; if (optIn) //ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType<ChoXmlNodeRecordFieldAttribute>().Any()).Any()) { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { pt = pd.PropertyType.GetUnderlyingType(); var fa = pd.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().FirstOrDefault(); bool optIn1 = fa == null || fa.UseXmlSerialization ? optIn : ChoTypeDescriptor.GetProperties(pt).Where(pd1 => pd1.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().Any()).Any(); if (false) //optIn1 && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt)) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn1); } else if (pd.Attributes.OfType <ChoXmlNodeRecordFieldAttribute>().Any()) { bool useCache = true; string xpath = null; ChoXmlNodeRecordFieldAttribute attr = ChoTypeDescriptor.GetPropetyAttribute <ChoXmlNodeRecordFieldAttribute>(pd); if (attr.XPath.IsNullOrEmpty()) { if (!attr.FieldName.IsNullOrWhiteSpace()) { attr.XPath = $"/{attr.FieldName}|/@{attr.FieldName}"; } else { attr.XPath = xpath = $"/{pd.Name}|/@{pd.Name}"; } IsComplexXPathUsed = true; } else { useCache = false; } var obj = new ChoXmlRecordFieldConfiguration(pd.Name, attr, pd.Attributes.OfType <Attribute>().ToArray()); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name); obj.UseCache = useCache; if (obj.XPath.IsNullOrWhiteSpace()) { if (!obj.FieldName.IsNullOrWhiteSpace()) { obj.XPath = $"/{obj.FieldName}|/@{obj.FieldName}"; } else { obj.XPath = $"/{obj.Name}|/@{obj.Name}"; } } obj.FieldType = pd.PropertyType.GetUnderlyingType(); if (!XmlRecordFieldConfigurations.Any(c => c.Name == pd.Name)) { XmlRecordFieldConfigurations.Add(obj); } } } } else { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { XmlIgnoreAttribute xiAttr = pd.Attributes.OfType <XmlIgnoreAttribute>().FirstOrDefault(); if (xiAttr != null) { continue; } pt = pd.PropertyType.GetUnderlyingType(); if (false) //pt != typeof(object) && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt)) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn); } else { var obj = new ChoXmlRecordFieldConfiguration(pd.Name, $"/{pd.Name}|/@{pd.Name}"); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name); StringLengthAttribute slAttr = pd.Attributes.OfType <StringLengthAttribute>().FirstOrDefault(); if (slAttr != null && slAttr.MaximumLength > 0) { obj.Size = slAttr.MaximumLength; } XmlElementAttribute xAttr = pd.Attributes.OfType <XmlElementAttribute>().FirstOrDefault(); if (xAttr != null && !xAttr.ElementName.IsNullOrWhiteSpace()) { obj.FieldName = xAttr.ElementName; } else { XmlAttributeAttribute xaAttr = pd.Attributes.OfType <XmlAttributeAttribute>().FirstOrDefault(); if (xAttr != null && !xaAttr.AttributeName.IsNullOrWhiteSpace()) { obj.FieldName = xaAttr.AttributeName; } else { DisplayNameAttribute dnAttr = pd.Attributes.OfType <DisplayNameAttribute>().FirstOrDefault(); if (dnAttr != null && !dnAttr.DisplayName.IsNullOrWhiteSpace()) { obj.FieldName = dnAttr.DisplayName.Trim(); } else { DisplayAttribute dpAttr = pd.Attributes.OfType <DisplayAttribute>().FirstOrDefault(); if (dpAttr != null) { if (!dpAttr.ShortName.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.ShortName; } else if (!dpAttr.Name.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.Name; } obj.Order = dpAttr.Order; } else { ColumnAttribute clAttr = pd.Attributes.OfType <ColumnAttribute>().FirstOrDefault(); if (clAttr != null) { obj.Order = clAttr.Order; if (!clAttr.Name.IsNullOrWhiteSpace()) { obj.FieldName = clAttr.Name; } } } } } } DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault(); if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace()) { obj.FormatText = dfAttr.DataFormatString; } if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace()) { obj.NullValue = dfAttr.NullDisplayText; } if (!XmlRecordFieldConfigurations.Any(c => c.Name == pd.Name)) { XmlRecordFieldConfigurations.Add(obj); } } } } } }
public virtual void WriteXml (System.Xml.XmlWriter writer) { foreach (PropertyInfo pi in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (pi.GetSetMethod () == null) continue; bool isAttribute = false; bool hasDefaultValue = false; bool ignore = false; string name = ""; object value = null; Type valueType = null; MemberInfo mi = pi.GetGetMethod (); if (mi == null) continue; value = pi.GetValue (this, null); valueType = pi.PropertyType; name = pi.Name; object[] att = pi.GetCustomAttributes (false); foreach (object o in att) { XmlAttributeAttribute xaa = o as XmlAttributeAttribute; if (xaa != null) { isAttribute = true; if (string.IsNullOrEmpty (xaa.AttributeName)) name = pi.Name; else name = xaa.AttributeName; continue; } XmlIgnoreAttribute xia = o as XmlIgnoreAttribute; if (xia != null) { ignore = true; continue; } DefaultValueAttribute dv = o as DefaultValueAttribute; if (dv != null) { if (dv.Value.Equals (value)) hasDefaultValue = true; if (dv.Value.ToString () == value.ToString ()) hasDefaultValue = true; continue; } } if (hasDefaultValue || ignore || value==null) continue; if (isAttribute) writer.WriteAttributeString (name, value.ToString ()); else { if (valueType.GetInterface ("IXmlSerializable") == null) continue; (pi.GetValue (this, null) as IXmlSerializable).WriteXml (writer); } } foreach (EventInfo ei in this.GetType().GetEvents()) { FieldInfo fi = this.GetType().GetField(ei.Name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField); Delegate dg = (System.Delegate)fi.GetValue (this); if (dg == null) continue; foreach (Delegate d in dg.GetInvocationList()) { if (!d.Method.Name.StartsWith ("<"))//Skipping empty handler, not clear it's trikky writer.WriteAttributeString (ei.Name, d.Method.Name); } } }
/// <summary> /// Adds new attribute to the node decorations. /// </summary> /// <param name="attr">An attriubute.</param> protected void Add(object attr) { if (attr is NodePolicyAttribute) { if (_nodePolicy == null) { _nodePolicy = (NodePolicyAttribute)attr; } } else if (attr is ConverterAttribute) { if (_converter == null) { _converter = (ConverterAttribute)attr; } } else if (attr is TransparentAttribute) { _transparent = (TransparentAttribute)attr; } else if (attr is SkipNavigableRootAttribute) { _skipNavigableRoot = (SkipNavigableRootAttribute)attr; } else if (attr is ChildXmlElementAttribute) { _childXmlElementName = (ChildXmlElementAttribute)attr; } else if (attr is XmlRootAttribute) { if (_xmlRoot == null) { _xmlRoot = (XmlRootAttribute)attr; } } else if (attr is XmlAttributeAttribute) { if (NodeTypeUndefined()) { _xmlAttribute = (XmlAttributeAttribute)attr; } } else if (attr is XmlElementAttribute) { if (NodeTypeUndefined()) { _xmlElement = (XmlElementAttribute)attr; _isNullable = _xmlElement.IsNullable; } } else if (attr is XmlAnyElementAttribute) { if (NodeTypeUndefined()) { _xmlAnyElement = (XmlAnyElementAttribute)attr; } } else if (attr is XmlTextAttribute) { if (NodeTypeUndefined()) { _xmlText = (XmlTextAttribute)attr; } } else if (attr is XmlIgnoreAttribute) { _xmlIgnore = (XmlIgnoreAttribute)attr; } else if (attr is XmlTypeAttribute) { _xmlType = (XmlTypeAttribute)attr; } }