C#属性XmlIgnore和XamlWriter类-XmlIgnore不起作用 [英] C# Attribute XmlIgnore and XamlWriter class - XmlIgnore not working
问题描述
我有一个类,其中包含标记为 [XmlIgnore]
的属性 Brush MyBrush
。尽管如此,它还是在流中序列化的,当尝试通过 XamlReader
进行读取时会造成麻烦。
I have a class, containing a property Brush MyBrush
marked as [XmlIgnore]
. Nevertheless it is serialized in the stream causing trouble when trying to read via XamlReader
.
我做了一些测试,例如更改属性(向内部)的可见性时,它已消失在流中。不幸的是,在我的特定情况下我无法做到这一点。
I did some tests, e.g. when changing the visibility of the Property (to internal) it is gone in the stream. Unfortunately I cannot do this in my particular scenario.
- 有人有同样的问题吗?
- 您看到任何解决方法吗?
备注:据我所知,C#4.0
Remark: C# 4.0 as far I can tell
这是我的单元测试中的一种方法,在该方法中,我确实测试了 XamlSerialization
:
This is a method from my Unit Test where I do test the 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");
在我的一门课中,我使用属性 Brush
。在上面的代码中,单元测试失败,因为值是 Brush
对象(不可序列化)。当我删除设置器(如下所示)时,单元测试通过。
In one of my classes I use the Property Brush
. In the above code this Unit Tests fails because a Brush
object (not serializable) is the value. When I remove the Setter (as below), the Unit Test passes.
使用 XmlWriter
(基本上是同一测试)如上所述)。在 StringBuffer sb
中,我可以看到 Property Brush
在设置器存在时被序列化,而在移除时没有被序列化(很可能是另一个由于没有设置器而忽略属性的检查)。带有 [XmlIgnore]
的其他属性将按预期方式被忽略。
Using the XmlWriter
(basically same test as above) it works. 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). Other Properties with [XmlIgnore]
are ignored as intended.
[XmlIgnore]
public Brush MyBrush
{
get { ..... }
// removed because of problem with Serialization
// set { ... }
}
推荐答案
John的评论是正确的。还有(再次)其他属性。我在这里找到了这篇出色的文章: http:// blogs。 msdn.com/b/mikehillberg/archive/2006/09/16/xamlwriter.aspx
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
我什至遇到了属性[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
在
之前,但是将其误解为设计时间属性。
I even came across the attribute [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
before
, but misinterpreted it as a design time attribute.
这篇关于C#属性XmlIgnore和XamlWriter类-XmlIgnore不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!