开发者

Duplicating Code Across Multiple Namespaces and Partial Classes

开发者 https://www.devze.com 2023-04-10 11:00 出处:网络
I\'m using LinqToXSD to generate document models (code) based on a client\'s XSD\'s. Since these XSD\'s share common structure each time I generate a document model some objects get duplicated. To pre

I'm using LinqToXSD to generate document models (code) based on a client's XSD's. Since these XSD's share common structure each time I generate a document model some objects get duplicated. To prevent compile problems, I've put each document model in its own namespace. This has worked great.

But now I want to do some custom validation with these objects which are public partial classes. So I defined a IValidate interface with a single Validate() method. Now I have some low level object generated in more than one object model but each in a different namespace.

namespace1
{
    public partial class foo : XTypedElement
       {........}
}

namespace2
{
    public partial class foo : XTypedElement
       {........}
}

Now I want to write a single partial class file to add IValidate interface and the Validate() method across all my foo classes. But, of course this won't work because they are in reality different objects. I have lots of these because of the generated code and the XSD library used to generate the code.

If I could just shadow the internals (read the '......'s above) this would work. I have many foo's in different namespaces (and I have foo1 through foo100 each with the same issue). I need to duplicate code, I know it's a no-no and I don't want to have to do it. But, if I do, I at least do not want to have to maintain so many copies of the same code.

I'm fairly confident this can be done with dynamics but I'd rath开发者_如何学JAVAer not have to go there.

Can anyone suggest some other way to write these validation methods and not have to manually copy to each namespace.class's partial class?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号