C# 类 Java 的内联扩展?
我会在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这(显式主格匿名类型)在 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.
我知道你说这不能在 C# 中完成,但是可以将其重写为 C# 但不需要太多复杂的方法吗?
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?
您在 C# 中寻找的功能称为扩展方法。它们很相似,但您可以编写如下代码:
您通常将其包含在您自己的扩展命名空间中,然后在必要时使用 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:
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.