1 回答
TA貢獻1804條經驗 獲得超8個贊
您的基本問題是,您正在為每個屬性添加多個覆蓋 [XmlElement] 屬性,因為您正在對所有屬性使用單個實例,這會累積為所有屬性定義的對象。mainNewXMLAttributesXmlElementAttribute
要解決此問題,您需要為循環中的每個屬性分配一個 fresh,如以下更正和簡化的版本所示:mainNewXMLAttributesforeach (var propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)GetXMLAttributeOverrides()
public static partial class ReportingManipulation
{
public static XmlAttributeOverrides GetXMLAttributeOverrides(Type theType, IList<string> propertiesToInlcudeInOrder)
{
var allProperties = theType.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance).Select(p => p.Name);
return GetXMLAttributeOverrides(theType, propertiesToInlcudeInOrder, allProperties);
}
// XML Attribute Overrrides
public static XmlAttributeOverrides GetXMLAttributeOverrides(Type theType, IList<string> propertiesToInlcudeInOrder, IEnumerable<string> allProperties)
{
if (propertiesToInlcudeInOrder == null || propertiesToInlcudeInOrder.Count == 0)
return null;
var theXMLAttributeOverrides = new XmlAttributeOverrides();
// To Add In Order
int counter = 1;
foreach (var propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)
{
// Allocate a fresh instance of XmlAttributes for each property, because we are defining a different
// XmlElementAttribute for each
var mainNewXMLAttributes = new XmlAttributes { XmlIgnore = false };
// Specify the element order XmlElementAttribute and attach to the XmlAttributes
var theXMLElementAttributeToAdd = new XmlElementAttribute { Order = counter };
mainNewXMLAttributes.XmlElements.Add(theXMLElementAttributeToAdd);
// Attach the override XmlElementAttribute to the property propertyNameToIncludeInOrder
theXMLAttributeOverrides.Add(theType, propertyNameToIncludeInOrder, mainNewXMLAttributes);
counter++;
}
// To Ignore
// Using System.Linq.Enumerable.Except()
var propertiesToNotInclude = allProperties.Except(propertiesToInlcudeInOrder);
var ignoreXMLAttributes = new XmlAttributes { XmlIgnore = true };
foreach (var propertyNameToNotInlcude in propertiesToNotInclude)
{
// Attach the override XmlElementAttribute to the property propertyNameToIncludeInOrder
// No need to allocate a fresh instance of ignoreXMLAttributes for each, because the instances would all be identical
theXMLAttributeOverrides.Add(theType, propertyNameToNotInlcude, ignoreXMLAttributes);
}
return theXMLAttributeOverrides;
}
}
為什么你的代碼不起作用?在初始代碼中,您需要執行以下操作:
XmlAttributes mainNewXMLAttributes = new XmlAttributes();
mainNewXMLAttributes.XmlIgnore = false;
int counter = 1;
foreach (string propertyNameToIncludeInOrder in propertiesToInlcudeInOrder)
{
XmlElementAttribute theXMLElementAttributeToAdd = new XmlElementAttribute(propertyNameToIncludeInOrder);
theXMLElementAttributeToAdd.ElementName = propertyNameToIncludeInOrder;
theXMLElementAttributeToAdd.Order = counter;
mainNewXMLAttributes.XmlElements.Add(theXMLElementAttributeToAdd);
theXMLAttributeOverrides.Add(theType, propertyNameToIncludeInOrder, mainNewXMLAttributes);
counter++;
}
現在,方法 XmlAttributeOverrides.Add(Type, String, XmlAttributes) 被記錄為按如下方式工作:
將對象添加到對象集合中。該參數指定要重寫的對象。該參數指定被覆蓋的成員的名稱。XmlAttributesXmlAttributestypemember
因此,當最終構造 時,的內容將應用于命名參數。當您為所有參數僅構造 實例時,其數組將包含與所有參數對應的元素名稱!即,您的代碼嘗試將多個屬性應用于每個命名參數,僅在覆蓋名稱和順序上有所不同。這說明了您需要將 XmlChoiceIdentifierAttribute 添加到 'TextColumn' 成員的異常 -- 如果屬性值是多態的,并且您希望將不同的元素名稱分配給不同的值類型,則只能將多個元素名稱附加到屬性。mainNewXMLAttributesXmlSerializermainNewXMLAttributesXmlElements[XmlElement]
筆記
生成 with 覆蓋時,必須靜態緩存它,并在以后重用它以避免嚴重的內存泄漏,如使用 StreamReader 和 XmlSerializer 的內存泄漏中所述。XmlSerializer
我不建議無條件地吞下異常,并在低級實用工具方法或對象構造函數中將它們作為錯誤消息呈現給用戶。
在這里演示工作小提琴。
- 1 回答
- 0 關注
- 178 瀏覽
添加回答
舉報
