C# Java-like inline extension of classes?

余生颓废 提交于 2019-12-14 01:44:40

问题


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?)


回答1:


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.




回答2:


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;
    }
};



回答3:


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.



来源:https://stackoverflow.com/questions/5399723/c-sharp-java-like-inline-extension-of-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!