强制施行属性的类/方法的装饰 [英] Enforce Attribute Decoration of Classes/Methods

查看:201
本文介绍了强制施行属性的类/方法的装饰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我最近的问题继上大型,复杂的对象作为Web服务结果。我一直在想我怎么能保证未来所有的子类序列化到XML

Following on from my recent question on Large, Complex Objects as a Web Service Result. I have been thinking about how I can ensure all future child classes are serializable to XML.

现在,很明显,我可以实现的的IXmlSerializable 接口,然后扔下一个读/写机,但我想避免的,因为它意味着然后我需要实例化一个读/写时,我想这样做,而且我要带的是工作时间99.99%字符串的所以我可能只写我自己的。

Now, obviously I could implement the IXmlSerializable interface and then chuck a reader/writer to it but I would like to avoid that since it then means I need to instantiate a reader/writer whenever I want to do it, and 99.99% of the time I am going to be working with a string so I may just write my own.

然而,序列化到XML,我只是装饰类及其成员使用的 XML ??? 的属性( XmlRoot 的XmlElement 的等),然后将它传递给的的XmlSerializer 的和的的StringWriter 的得到的字符串。这是所有好。我打算把方法将字符串返回到一个通用的实用方法,所以我不需要担心等类型

However, to serialize to XML, I am simply decorating the class and its members with the Xml??? attributes ( XmlRoot , XmlElement etc.) and then passing it to the XmlSerializer and a StringWriter to get the string. Which is all good. I intend to put the method to return the string into a generic utility method so I dont need to worry about type etc.

这是我关心的是:如果我不与装饰类(ES)所需的属性是不是抛出一个错误,直到运行时..

The this that concerns me is this: If I do not decorate the class(es) with the required attributes an error is not thrown until run time..

有什么办法来执行属性的装饰?可以这样用的FxCop来完成(我没有使用过的FxCop还)

Is there any way to enforce attribute decoration? Can this be done with FxCop? (I have not used FxCop yet)

对不起,在得到这个延时关闭掉的家伙,许多事情要做!

Sorry for the delay in getting this close off guys, lots to do!

肯定喜欢使用反射来做到这一点在一个测试用例,而不是诉诸的FxCop的想法(喜欢把一切融合在一起).. 弗雷德里克Kalseth的回答是太棒了,谢谢!为包括代码,它可能会采取我有点挖弄清楚如何做我自己

Definately like the idea of using reflection to do it in a test case rather than resorting to FxCop (like to keep everything together).. Fredrik Kalseth's answer was fantastic, thanks for including the code as it probably would have taken me a bit of digging to figure out how to do it myself!

+1到其他人的类似的建议: )

+1 to the other guys for the similar suggestions :)

推荐答案

我会写一个单元/集成测试验证的任何类匹配某个给定的标准(即子类X)是适当的装饰。如果您设置的构建与运行测试,您可以在此测试失败构建失败

I'd write a unit/integration test that verifies that any class matching some given criteria (ie subclassing X) is decorated appropriately. If you set up your build to run with tests, you can have the build fail when this test fails.

更​​新:你说,看来我将不得不卷我的袖子,确保单元测试统称维护 - 你不必。只要写一个使用反射来找到需要被断言所有类的一般测试类。事情是这样的:

UPDATE: You said, "Looks like I will just have to roll my sleeves up and make sure that the unit tests are collectively maintained" - you don't have to. Just write a general test class that uses reflection to find all classes that needs to be asserted. Something like this:

[TestClass]
public class When_type_inherits_MyObject
{
    private readonly List<Type> _types = new List<Type>();

    public When_type_inherits_MyObject()
    {
        // lets find all types that inherit from MyObject, directly or indirectly
        foreach(Type type in typeof(MyObject).Assembly.GetTypes())
        {
            if(type.IsClass && typeof(MyObject).IsAssignableFrom(type))
            {
                _types.Add(type);
            }
        }
    }

    [TestMethod]
    public void Properties_have_XmlElement_attribute
    {
        foreach(Type type in _types)
        {
            foreach(PropertyInfo property in type.GetProperties())
            {
                object[] attribs = property.GetCustomAttributes(typeof(XmlElementAttribute), false);
                Assert.IsTrue(attribs.Count > 0, "Missing XmlElementAttribute on property " + property.Name + " in type " + type.FullName);
            }
        }
    }
}

这篇关于强制施行属性的类/方法的装饰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆