如何保留对自动生成的 C# 分部类中现有方法的调整?

发布于 2024-11-24 08:19:36 字数 828 浏览 3 评论 0原文

我正在使用 Visual Studio 编码的 UI 测试,并希望继续对生成的代码进行调整。

该代码是作为 UIMap.csUIMap.Designer.cs 中的分部类生成的,因此我知道一种解决方案是创建一个名称稍有不同的方法,例如 UIMap.cs 中的 myMethod_persist 并在 UIMap.Designer.cs 中使用它代替 myMethod,这每次源都会被覆盖已再生。

然而,这看起来非常混乱,所以我更喜欢的是重写 UIMap.cs 中的 myMethod 。这样,界面就不会因为很多麻烦而变得复杂,而且我不必记住将调用代码中的每个实例更改为 myMethod_persist

不幸的是,当涉及到C# 我有点菜鸟,甚至不知道这是否可能。

基本上我想要的是这样的:

[UIMap.Designer.cs]
partial class myClass
{
    public override void myMethod( int a ){ somethingHorrible(int a); };
}

[UIMap.cs]
partial class myClass
{
    public sealed override void myMethod( int a ){ somethingNice(int a); };
}

但我知道 seal 和 override 关键字通常与派生子类一起使用,而不是与部分类一起使用。这可能吗?如果没有,那么我最好的选择是什么?

I am working with Visual Studio Coded UI Tests, and wish to persist tweaks to the generated code.

The code is generated as a partial class in UIMap.cs and UIMap.Designer.cs, and so I know one solution would be to create a method with a slightly different name, such as myMethod_persist in UIMap.cs and use this instead of myMethod in UIMap.Designer.cs, which will be overwritten every time the source is regenerated.

This however seems very messy, and so what I'd prefer, is to instead override myMethod in UIMap.cs. This way the interface is not complicated with lots of gumph, and I don't have to remember to change every instance of myMethod in calling code to myMethod_persist

Unfortunately when it comes to C# I'm a bit of a noob, and don't even know if this is possible.

Basically what I want is this:

[UIMap.Designer.cs]
partial class myClass
{
    public override void myMethod( int a ){ somethingHorrible(int a); };
}

[UIMap.cs]
partial class myClass
{
    public sealed override void myMethod( int a ){ somethingNice(int a); };
}

But I know that the sealed and override keywords are usually used with derived subclasses classes, not partial classes. Is this possible? If not then what would be my best alternatives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假装不在乎 2024-12-01 08:19:36

这里只有一个类 - myClass。 partial 关键字只允许将类定义拆分到多个文件中。

您可以使用部分方法,但前提是原始(生成的)方法使用部分关键字。您不能覆盖同一类上的方法。

请参阅部分类和方法(C# 编程指南)< /a>.

There's only one class here - myClass. The partial keyword just allows the class definition to be split across multiple files.

You can use partial methods, but only if the original (generated) method uses the partial keyword. You cannot override a method on the same class.

See Partial Classes and Methods (C# Programming Guide).

依 靠 2024-12-01 08:19:36

如果您无法控制自动生成本身(即框架或第三方生成器),您的选择会受到一定限制。我想到的两种方法是修改生成的代码(正如您所指出的,这对于重大且可能随着时间的推移而累积的更改来说是不切实际的)和/或创建派生类并使用它而不是使用 auto - 直接生成的类(假设您控制将使用它们的代码,这似乎是可能的)。

派生类(继承自动生成的类)将能够在要替换的方法声明中使用 override 或 new 。然而,这种方法也有很多注意事项。您只能“覆盖”在基类中声明为虚拟的方法(或者本身是另一个底层虚拟基的覆盖等)。您还可以在派生类中用“新”方法替换方法,但基类中的其他代码将不知道您的“新”版本,也不会调用它(而它们会调用您的“覆盖”,因为它们知道该方法是虚拟的)。还有可及性问题;您的派生类将无权访问基类的私有成员。

但对于某些你想做的事情,它可能会起作用。在某些情况下,您可能需要稍微调整自动生成的代码,例如添加关键字“virtual”或将“private”成员更改为“protected”,以便您可以从派生类访问它们。

添加:当然,您还可以在自己的永久文件中为同一分部类向原始生成的类添加新成员,并且此代码可以访问该类的私有成员。这可能是让派生类访问私有成员的另一种方法,例如通过创建受保护的属性来包装对私有成员字段的访问。如果您不需要更改现有方法,则不一定需要创建派生类,但您的示例谈到想要“覆盖”自动生成的代码中的方法,因此想必它们已经存在在那里了。

另请注意,Designer 文件(例如 Form 或 UserControl)通常不会被完全覆盖,因此在核心生成的代码之外进行谨慎的更改(例如,不要在“Windows 窗体设计器生成的代码”区域内)可以制作(并保留)。例如,有时需要在 Designer 文件的 Dispose(...) 方法中添加对您自己的自定义清理方法的调用。

If you have no control over the auto-generation itself (ie. a framework or 3rd party generator) your options are somewhat limited. The two approaches that come to mind are to modify the generated code--which as you noted is not practical for changes that are significant and perhaps accumulating over time--and/or to create a derived class and use that instead of using the auto-generated class directly (assuming you control the code which would be using them, which seems likely).

A derived class (inheriting the auto-generated class) would be able to use override or new in the method declarations you want to replace. However, there are a lot of caveats to this approach as well. You can only "override" a method that was delcared as virtual in the base class (or was itself an override of another underlying virtual base, etc). You can also replace a method with a "new" one in the derived class, but the other code in the base class will not know about your "new" version and will not call it (whereas they will call your "override" because they know the method to be virtual). There are also issues of accessiblity; your derived class won't have access to private members of the base class.

But for some set of things you want to do it could work. In some cases you might have to tweak the auto-generated code slightly such as adding the keyword "virtual" or changing "private" members to "protected" so that you can access them from your derived class.

Added: Of course, you can also add new members to the original generated class in your own permanent file for the same partial class, and this code would have access to the class's private members. That can be another way to give your derived class access to the private members, such as by creating a protected property to wrap access to a private member field. If you didn't need to make changes to existing methods you wouldn't necessarily need to create a derived class, but your example talked about wanting to "override" methods from the auto-generated code, so presumably they already exist there.

Also note that a Designer file--such as for a Form or UserControl--does not usally get completely overwritten, so cautious changes outside the core generated code (eg. not inside the "Windows Form Designer generated code" region) can be made (and are persisted). For example, it is sometimes necessary to add a call to your own custom clean-up method in the Dispose(...) method in the Designer file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文