用另一个部分类覆盖部分类的默认构造函数 [英] Override Default Constructor of Partial Class with Another Partial Class

查看:37
本文介绍了用另一个部分类覆盖部分类的默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不认为这是可能的,但如果是,那么我需要它:)

I don't think this is possible, but if is then I need it :)

我有一个来自 Visual Studio 2008 的 wsdl.exe 命令行工具自动生成的代理文件.

I have a auto-generated proxy file from the wsdl.exe command line tool by Visual Studio 2008.

代理输出是部分类.我想覆盖生成的默认构造函数.我宁愿不修改代码,因为它是自动生成的.

The proxy output is partial classes. I want to override the default constructor that is generated. I would rather not modify the code since it is auto-generated.

我尝试创建另一个部分类并重新定义默认构造函数,但这不起作用.然后我尝试使用 override 和 new 关键字,但这不起作用.

I tried making another partial class and redefining the default constructor, but that doesn't work. I then tried using the override and new keywords, but that doesn't work.

我知道我可以从部分类继承,但这意味着我必须更改我们所有的源代码以指向新的父类.我宁愿不必这样做.

I know I could inherit from the partial class, but that would mean I'd have to change all of our source code to point to the new parent class. I would rather not have to do this.

有什么想法、变通办法或技巧吗?

Any ideas, work arounds, or hacks?

//Auto-generated class
namespace MyNamespace {
   public partial class MyWebService : System.Web.Services.Protocols.SoapHttpClientProtocol {
      public MyWebService() {
         string myString = "auto-generated constructor";
         //other code...
      }
   }
}

//Manually created class in order to override the default constructor
namespace MyNamespace {
   public partial class MyWebService : System.Web.Services.Protocols.SoapHttpClientProtocol {
      public override MyWebService() { //this doesn't work
         string myString = "overridden constructor";
         //other code...
      }
   }
}

推荐答案

这是不可能的.部分类本质上是同一类的一部分;任何方法都不能被定义两次或被覆盖,包括构造函数.

This is not possible. Partial classes are essentially parts of the same class; no method can be defined twice or overridden, and that includes the constructor.

你可以在构造函数中调用一个方法,只在其他部分文件中实现它.

You could call a method in the constructor, and only implement it in the other part file.

这篇关于用另一个部分类覆盖部分类的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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