我怎样才能标志着一个WCF客户端代理生成的类的虚拟方法 [英] How can I mark the methods in a WCF client proxy generated class as virtual

查看:91
本文介绍了我怎样才能标志着一个WCF客户端代理生成的类的虚拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS 2010中我创建它的代码生成WCF客户端代理类Refernce.cs服务引用。我需要在这个类中的方法有虚拟修改,使他们能够在模拟中使用。



当然,我可以用手编辑生成的代码,但我每次更新该代码将被再生和消灭我的变化参考。



我有是怎么生成的WCF客户端代理类更多的控制?有什么办法让代码生成器随时添加虚拟修改?我想这是更多的自动化,这样,当其他开发人员需要更新的参考,他们不必知道或记住手动编辑生成的代码,并添加虚拟修改。


< DIV CLASS =h2_lin>解决方案

另一种方法是创建一个接口。作为部分代理类生成的,这意味着你可以创建该类另一部分文件,并指定类实现你的界面,即使实际执行是在生成的类。然后,你可以模拟接口,并重新生成您的代理你的心内容



例如,您的生成类可能是:



 公共部分类SomeService 
{
公共字符串GetSomething()
{
返回...
}
}

您可以创建一个这样的接口:

 公共接口ISomeService 
{
串GetSomething();
}



然后添加生成的类空的部分文件:

 公共部分类SomeService:ISomeService 
{
}


In VS 2010 I'm creating a service reference which code generates the WCF client proxy class Refernce.cs. I need the methods in this class to have the Virtual modifier so they can be used in Mock.

Of course I can hand edit the generated code, but every time I update the reference the code is going to be regenerated and wipe out my changes.

Do I have more control of how the WCF client proxy class is generated? Is there any way to have the code generator always add the Virtual modifier? I would like this to be more automated so that when other developers need to update the reference, they don't have to know or remember to hand edit the generated code and add the the virtual modifier.

解决方案

An alternative is to create an interface. The proxy classes are generated as partial, which means you can create another partial file for that class, and specify that the class implements your interface, even though the actual implementation is in the generated class. You can then mock the interface, and regenerate your proxy to your hearts content.

For instance, your generated class might be:

public partial class SomeService
{
    public string GetSomething()
    {
        return ... 
    }
}

You can create an interface for this:

public interface ISomeService
{
    string GetSomething();
}

And then add an empty partial file for the generated class:

public partial class SomeService : ISomeService
{
}

这篇关于我怎样才能标志着一个WCF客户端代理生成的类的虚拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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