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

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

问题描述

继续我最近关于大型复杂对象的问题Web服务结果。我一直在想我该如何确保所有未来的子类都可序列化为XML。



现在,显然我可以实现 IXmlSerializable 接口,然后吸引读写器,但我想避免因为这意味着我需要实例化一个读写器每当我想这样做,和99.99%的时间,我将使用一个字符串,所以我可能只是写我自己。 / p>

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



这关系到我是这样的:如果我不要用所需的属性装饰类,在运行时间之前不会抛出错误。



有什么方法强制实施属性装饰吗?这可以用FxCop吗?(我还没有使用FxCop)



UPDATE:





确实像使用反射在一个测试案例中做的想法,而不是诉诸FxCop (喜欢保持一切)。 Fredrik Kalseth的回答是太棒了,谢谢包括代码,因为它可能会让我一些挖掘,以弄清楚如何做自己!



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

解决方案

我会写一个单位/集成测试来验证任何匹配某个给定标准的类(即子类化X)装饰适当。如果你设置你的构建使用测试运行,你可以让构建失败,当测试失败。



更新:你说,看起来我只需要滚动我的袖子,并确保单元测试是集体维护 - 你不必。只写一个通用的测试类,使用反射找到所有需要断言的类。像这样:

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

public When_type_inherits_MyObject()
{
//允许直接或间接找到从MyObject继承的所有类型
foreach(typeof typeof(MyObject).Assembly .GetTypes())
{
if(type.IsClass&& typeof(MyObject).IsAssignableFrom(type))
{
_types.Add(type);
}
}
}

[TestMethod]
public void Properties_have_XmlElement_attribute
{
foreach(类型类型为_types)
{
foreach(type.GetProperties()中的PropertyInfo属性)
{
object [] attribs = property.GetCustomAttributes(typeof(XmlElementAttribute),false);
Assert.IsTrue(attribs.Count> 0,在属性上缺少XmlElementAttribute+ type.FullName类型中的+ property.Name +);
}
}
}
}


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.

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.

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.

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..

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

UPDATE:

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

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 to the other guys for the similar suggestions :)

解决方案

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天全站免登陆