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

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

问题描述

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

我从Visual Studio 2008中的Wsdl.exe用命令行工具自动生成的代理文件。

代理输出部分类。我想重写生成的默认构造函数。我宁愿不修改code,因为它是自动生成的。

我试图使另一部分类,并重新定义默认构造函数,但不起作用。然后,我使用覆盖和新的关键字尝试过,但不起作用。

我知道我可以从局部类继承,但是这意味着我不得不改变所有的源$ C ​​$ C的指向新的父类。我宁愿没有这样做。

任何想法,变通或黑客?

  //自动生成的类
命名空间myNameSpace对象{
   公共部分类为MyWebService:System.Web.Services.Protocols.SoapHttpClientProtocol {
      公众为MyWebService(){
         字符串的myString =自动生成的构造;
         //其他code ...
      }
   }
}//人工为了覆盖默认的构造函数创建的类
命名空间myNameSpace对象{
   公共部分类为MyWebService:System.Web.Services.Protocols.SoapHttpClientProtocol {
      公众覆盖为MyWebService(){//这不起作用
         字符串的myString =覆盖构造;
         //其他code ...
      }
   }
}


解决方案

这是不可能的。
部分类基本上是相同的类的零件;没有一种方法可以被定义两次或覆盖,这包括构造。

您可以调用构造函数的方法,只有实现它在另一部分文件。

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

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.

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