第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何解決:“您需要將XmlChoiceIdentifierAttribute添加到成員中”

如何解決:“您需要將XmlChoiceIdentifierAttribute添加到成員中”

C#
元芳怎么了 2022-08-20 17:07:02
我正在嘗試使用來(lái)更改將我的類序列化為 XML 的方式。我需要排除某些屬性,并按特定順序包含其他屬性。XmlAttributeOverrides我在這里有這個(gè)代碼:// XML Attribute Overrridespublic static XmlAttributeOverrides GetXMLAttributeOverrides(Type theType, List<string> propertiesToInlcudeInOrder, List<string> allColumnNames){    try    {        if (propertiesToInlcudeInOrder != null)        {            XmlAttributeOverrides theXMLAttributeOverrides = new XmlAttributeOverrides();            if (propertiesToInlcudeInOrder.Count > 0)            {                XmlAttributes mainNewXMLAttributes = new XmlAttributes();                mainNewXMLAttributes.XmlIgnore = false;                XmlAttributes ignoreXMLAttributes = new XmlAttributes();                ignoreXMLAttributes.XmlIgnore = true;                List<string> propertiesToNotInclude = new List<string>();                foreach (string theColumnName in allColumnNames)                {                    string thePropertyName = theColumnName;                    bool addProperty = true;                    foreach (string propertyToInclude in propertiesToInlcudeInOrder)                    {                        if (thePropertyName == propertyToInclude)                        {                            addProperty = false;                            break;                        }                    }                    if (addProperty)                    {                        propertiesToNotInclude.Add(thePropertyName);                    }                }
查看完整描述

1 回答

?
胡說叔叔

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊

您的基本問題是,您正在為每個(gè)屬性添加多個(gè)覆蓋 [XmlElement] 屬性,因?yàn)槟趯?duì)所有屬性使用單個(gè)實(shí)例,這會(huì)累積為所有屬性定義的對(duì)象。mainNewXMLAttributesXmlElementAttribute


要解決此問題,您需要為循環(huán)中的每個(gè)屬性分配一個(gè) fresh,如以下更正和簡(jiǎn)化的版本所示: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;

    }

}

為什么你的代碼不起作用?在初始代碼中,您需要執(zhí)行以下操作:


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++;

}

現(xiàn)在,方法 XmlAttributeOverrides.Add(Type, String, XmlAttributes) 被記錄為按如下方式工作:


將對(duì)象添加到對(duì)象集合中。該參數(shù)指定要重寫的對(duì)象。該參數(shù)指定被覆蓋的成員的名稱。XmlAttributesXmlAttributestypemember


因此,當(dāng)最終構(gòu)造 時(shí),的內(nèi)容將應(yīng)用于命名參數(shù)。當(dāng)您為所有參數(shù)僅構(gòu)造 實(shí)例時(shí),其數(shù)組將包含與所有參數(shù)對(duì)應(yīng)的元素名稱!即,您的代碼嘗試將多個(gè)屬性應(yīng)用于每個(gè)命名參數(shù),僅在覆蓋名稱和順序上有所不同。這說明了您需要將 XmlChoiceIdentifierAttribute 添加到 'TextColumn' 成員的異常 -- 如果屬性值是多態(tài)的,并且您希望將不同的元素名稱分配給不同的值類型,則只能將多個(gè)元素名稱附加到屬性。mainNewXMLAttributesXmlSerializermainNewXMLAttributesXmlElements[XmlElement]


筆記


生成 with 覆蓋時(shí),必須靜態(tài)緩存它,并在以后重用它以避免嚴(yán)重的內(nèi)存泄漏,如使用 StreamReader 和 XmlSerializer 的內(nèi)存泄漏中所述。XmlSerializer


我不建議無(wú)條件地吞下異常,并在低級(jí)實(shí)用工具方法或?qū)ο髽?gòu)造函數(shù)中將它們作為錯(cuò)誤消息呈現(xiàn)給用戶。


在這里演示工作小提琴。


查看完整回答
反對(duì) 回復(fù) 2022-08-20
  • 1 回答
  • 0 關(guān)注
  • 165 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)