0

I wrote C# code as described below that inherits a class from a generic class with static methods. I want to call the child class for its static methods (inherited from the base class) without having to specify the type.

EDITED! More "real" code

public class Rec
{
    public string Name { get; set; }
    public override string ToString() { return this.Name; }

    public virtual void Load() { /* HERE IT READS A TEXT FILE AND LOAD THE NAME */ }
}

public class BaseClass<T> : Rec
{
    public T Argument { get; set; }
    public override void Load() { /* NOW IT LOADS ALSO THE ARGUMENT */ }

    public static H Method<H>() where H : Rec, new()
    {
        H iH = new H();
        iH.Load();
        iH.Name += " " + iH.Argument.ToString();
        return iH;
    }
}

public class Child : BaseClass<string> { }

public class SomeOtherClass
{
    public void Test()
    {
        Child i = Child.Method();
        //instead of Child.Method<Child>();
    }
}

So, instead of having to call method<h>() i'd like to just call method(), so the code should assume that "h" is the caller type. Like in:

How can I do it?

SammuelMiranda
  • 420
  • 4
  • 29

2 Answers2

3

Static methods are not inherited in C#

See this answer for an idea of a potential implementation: Stack Overflow whats-the-correct-alternative-to-static-method-inheritance

Community
  • 1
  • 1
wvisaacs
  • 188
  • 10
1

You could change method<h> to method and make it an instance method:

public class BaseClass<T> where T, new()
{
   public T method() { /* RETURN SOMETHING */ }
}

And then call it as follows:

public class ABC : Child
{
   public void Test()
   {
      var iABC = this.method();
   }
}
Niels Filter
  • 4,430
  • 3
  • 28
  • 42
  • i know, thanks for the reply, but i'll need it as a static method. i only what to camoflage the generic argument assuming the type passed is the same as the caller. – SammuelMiranda Jun 08 '15 at 13:09