Creating an object of the type a static method is called on in an inheritance chain in C# -


I'm trying to do something like this in

  public class ParentClass { Public stable ParentClass GetSomething () {var thing = new // ??? Talk of return; }} Public Class ChildClass: ParentClass {}  

And then I want to be able to call the steady method on the child class like this:

  ChildClass Blah = ChildClass.GetSomething ();  

For example, when the child calls the statutory method on the class, I want to institute an example of a child class. But I just want the static method defined on parents. Is this at all possible? I will be happy even here:

  Child class blah = (ChildClass) ChildClass.GetSomething ();  

Thanks!

You can not override static methods, but you can tell generic usage to the Parent class Which you really mean to get the class is somewhat ugly but it works:

  class ParentClass & lt; T & gt; Where T: ParentClass & lt; T & gt;, New () {Public Static GetSomething () {T Object = New T (); Talk of return; }} Class childclass: parent class & lt; ChildClass & gt; {}  

Test:

ChildClass x = Child Class. GetSomething (); // work

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

jquery - SimpleModal Confirm fails to submit form -