C# 类 Java 的内联扩展?

发布于 2024-10-26 14:25:37 字数 710 浏览 0 评论 0原文

我会在 Google/MSDN 上查找此内容,但我不知道它叫什么,所以我在这里询问。

在Java中,我似乎记得你可以做这样非常酷的事情,例如:

Class MyClass
{
  int number;

  MyClass() { }

  void setNumber(int number)
  {
    this.number = number;
  }
}

然后做类似的事情:

MyClass myClass = new MyClass()
  {
    override void setNumber(int Number)
    {
      this.number = 2 * number;
    }
  };

...或其他事情。请原谅我在上面犯的任何错误 - 我已经大约 6 年没有真正接触过 Java。

关键是,我记得你可以伪扩展内联类。

现在,我需要扩展一个C# WinForms控件,但我只需要使用它一次,而且修改很小。我需要做的就是重写 CreateParams 属性和 OnPaint() 处理程序。

我的解决方案已经变得巨大,类遍布各处,包含另一个与标准 .Net 控件基本相同的类似乎很遗憾,只是行为略有不同。

是否可以像在 Java 中一样在 C# 中执行此内联扩展?如果是这样,怎么办? (它叫什么名字,以便我可以在 MSDN 上查找它?)

I would look this up on Google/MSDN, but I have no idea what it's called so I'm asking here.

In Java, I seem to remember you can do this really cool thing like:

Class MyClass
{
  int number;

  MyClass() { }

  void setNumber(int number)
  {
    this.number = number;
  }
}

and then do something like:

MyClass myClass = new MyClass()
  {
    override void setNumber(int Number)
    {
      this.number = 2 * number;
    }
  };

...or something. Forgive any mistakes I made above - I haven't actually touched Java in about 6 years.

The point is, I remember you could pseudo-extend a class inline.

Right now, I need to extend a C# WinForms control, but I only need to use it once, and the modifications are very minor. All I need to do is to override the CreateParams property and OnPaint() handler.

My solution is already getting huge with classes all over the place, it seems like a shame to include yet another class which is basically identical to a standard .Net control, just with very slightly different behaviour.

Is it possible to do this inline-extension in C# like you could in Java? If so, how? (and what is it called so I can look it up on MSDN?)

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

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

发布评论

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

评论(3

长伴 2024-11-02 14:25:38

这(显式主格匿名类型)在 C#3/4 中是不可能的。

类型必须显式创建然后构造。在 C# 中,任务有时会通过使用事件和委托(类调用提供诸如“NeedDataSource”之类的实现的事件)来“反转”——可以说正因为如此,尽管它在许多情况下是有意义的。

如果可以创建显式非主格类型:var x = new { P = 1, };,但仅限于本地范围。隐式方法包括委托/lambda/匿名函数,不适用于此处。

快乐编码。

This (explicit nominative anonymous types) is not possible in C#3/4.

The types must be created explicitly and then constructed. Tasks are sometimes "inverted" in C# with the use of Events and Delegates (class invokes Event which supplies implementation such as "NeedDataSource") -- arguably because of this, although it just makes sense in many cases.

If is possible to create explicit non-nominative types: var x = new { P = 1, }; but only in a local scope. Implicit methods include delegates/lambdas/anonymous functions and do not apply here.

Happy coding.

蒲公英的约定 2024-11-02 14:25:38

我知道你说这不能在 C# 中完成,但是可以将其重写为 C# 但不需要太多复杂的方法吗?

new EffectClause("Sleep Clause", SleepEffect.class)
{
    public String getClauseDescription()
    {
        return "Bla bla bla";
    }

    public boolean isEnabledByDefault()
    {
        return true;
    }
};

I understand that you said this cannot be done in C#, but its possible to rewrite this to C# but with not too much complicated method?

new EffectClause("Sleep Clause", SleepEffect.class)
{
    public String getClauseDescription()
    {
        return "Bla bla bla";
    }

    public boolean isEnabledByDefault()
    {
        return true;
    }
};
国粹 2024-11-02 14:25:38

您在 C# 中寻找的功能称为扩展方法。它们很相似,但您可以编写如下代码:

public static void setNumber(int Number, MyClass target)
{
    target.number = 2 * number;
}

您通常将其包含在您自己的扩展命名空间中,然后在必要时使用 MyExtensions 将其纳入范围;当您调用whatever.setNumber(x); 时,C# 能够判断出您的意思是调用此方法。

不过,我不能 100% 确定这是否能让您覆盖类中已存在的方法。您的另一个选择是从目标类继承一个专门用于重写目标方法的类。

The feature you're looking for in C# is called Extension Methods. They are similar, but instead you make code such as:

public static void setNumber(int Number, MyClass target)
{
    target.number = 2 * number;
}

You usually include this in your own extensions namespace, then bring it into scope when necessary by using MyExtensions; C# is able to figure out that you mean to call this method when you call whatever.setNumber(x);

I'm not 100% sure if this will let you override a method that already exists in a class however. Your other option is to inherit from the target class with a class specifically for the purpose of overriding the target method.

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