简体   繁体   English

C#属性XmlIgnore和XamlWriter类 - XmlIgnore无法正常工作

[英]C# Attribute XmlIgnore and XamlWriter class - XmlIgnore not working

I have a class, containing a property Brush MyBrush marked as [XmlIgnore] . 我有一个类,包含标记为[XmlIgnore]的属性Brush MyBrush Nevertheless it is serialized in the stream causing trouble when trying to read via XamlReader . 然而,它在流中被序列化,在尝试通过XamlReader读取时会造成麻烦。

I did some tests, eg when changing the visibility of the Property (to internal) it is gone in the stream. 我做了一些测试,例如当改变Property的可见性(到内部)时,它在流中消失了。 Unfortunately I cannot do this in my particular scenario. 不幸的是,在我的特定情况下,我不能这样做。

  1. Did anybody have the same issue and? 有没有人有同样的问题?
  2. Do you see any way to work around this? 你有没有办法解决这个问题?

Remark: C# 4.0 as far I can tell 备注:据我所知,C#4.0

This is a method from my Unit Test where I do test the XamlSerialization : 这是我的单元测试中的一个方法,我在那里测试XamlSerialization

            // buffer to a StringBuilder
            StringBuilder sb = new StringBuilder();
            XmlWriter writer = XmlWriter.Create(sb, settings);
            XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(writer) {XamlWriterMode = XamlWriterMode.Expression};

            XamlWriter.Save(testObject, manager);
            xml = sb.ToString();
            Assert.IsTrue(!String.IsNullOrEmpty(xml) && !String.IsNullOrEmpty(xml), "Xaml Serialization failed for " + testObject.GetType() + " no xml string available");

            xml = sb.ToString();
            MemoryStream ms = xml.StringToStream();
            object root = XamlReader.Load(ms);
            Assert.IsTrue(root != null, "After reading from MemoryStream no result for Xaml Serialization");

In one of my classes I use the Property Brush . 在我的一个类中,我使用Property Brush In the above code this Unit Tests fails because a Brush object (not serializable) is the value. 在上面的代码中,单元测试失败,因为Brush对象(不可序列化)是值。 When I remove the Setter (as below), the Unit Test passes. 当我移除Setter(如下所示)时,单元测试通过。

Using the XmlWriter (basically same test as above) it works. 使用XmlWriter (基本上与上面相同的测试)它可以工作。 In the StringBuffer sb I can see that Property Brush is serialized when the Setter is there and not when removed (most likely another check ignoring the Property because of no setter). StringBuffer sb我可以看到Property Brush在Setter存在时被序列化而不是被删除(很可能是因为没有setter而忽略了Property的另一个检查)。 Other Properties with [XmlIgnore] are ignored as intended. [XmlIgnore]其他属性将按预期被忽略。

    [XmlIgnore]
    public Brush MyBrush
    {
        get { ..... }
        // removed because of problem with Serialization
        // set { ... }
    }

John's comment is correct. 约翰的评论是正确的。 There are (again) other attributes. 还有(再次)其他属性。 I found this excellent article here: http://blogs.msdn.com/b/mikehillberg/archive/2006/09/16/xamlwriter.aspx 我在这里找到了这篇优秀的文章: http//blogs.msdn.com/b/mikehillberg/archive/2006/09/16/xamlwriter.aspx

I even came across the attribute [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] before , but misinterpreted it as a design time attribute. 我之前甚至遇到过属性[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] ,但将其误解为设计时属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM