2

I have a class and it has some static methods. I have also another class inherits my first class like below;

public class Business
{
    public static DataTable Get()
    {
    }
}

public class Model : Business
{
    public int ID { get; set; }
    public string CompanyName { get; set; }
}

I use it like below;

Model.Get();

Inside of Do method i try to catch type of Model class. I can catch Business class type like below inside of Do method;

public static DataTable Get()
{    
    Type t = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;
}

But i can't catch the Model class type. How can i do that?

Note : Get method in Business class selects columns up to which class it is called from. This is why i need name of Model class. Cause name of Model class will be name of table and it will select columns from table Model.

Note 2 : Normally i use it like;

Business.Get<Model>();

and i can get all data from Model table. But i try to use it like;

Model.Get();
thrashead
  • 337
  • 1
  • 3
  • 16

2 Answers2

5

You can't. There is nothing called B.Do(). It implicitly means that A.Do().

Do is a static method defined in A. There is no metadata of B exist in Do method as it's defined in A.

Being able to call B.Do(); is just a convenience that C# compiler provides you. Note that resharper will warn you for this usage of base class member using a derived type.

Do note that even if you write

B.Do();

Compiler converts it to

call A.Do// Call IL

Update: As for the updated question, I recommend you to use instance methods as opposed to static methods.

Model model = new Model();
model.Get();

Inside of get you can use GetType().Name to get the class name of current instance.

public class Business
{
    public DataTable Get()
    {
         string className = this.GetType().Name;
    }
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • I have a base class that does something up to from which class it is called. This is why i need name of B class. Cause name of B class will be name of table and it will select columns from table B – thrashead Oct 07 '15 at 12:27
  • @thrashead I guess you need some sort of polymorphic behavior in getting the table name. Perhaps, update the question with more info about what you're trying to achieve, or ask a new question with all necessary information to answer the question. Thanks – Sriram Sakthivel Oct 07 '15 at 12:30
  • I updated the question with a note. I try to develop a framework like entity framework and B class is a class has model members and it inherits A class to get it's methods. But i don't want to use virtual methods and override methods. Nothing will be overriden i need just type of table to catch members (properties) to make some functions like select, insert. – thrashead Oct 07 '15 at 12:36
  • Without seeing your code, I can't offer much. I'd suggest to get rid of that static, let it be instance method in `A`. Then a virtual property say `TableName` which is overridden in `B` to return "B". Or attributes also can help. But you need to show us more code to be able to help. – Sriram Sakthivel Oct 07 '15 at 12:40
  • Yes i use Generic Methods like B.Do(); If i do like this i don't need to inherit A class. I can call it like A.Do(); It also makes select and insert methods for table B. But it looks not nice you know. I try to use like B.Do(); instead of B.Do(); or A.Do(); – thrashead Oct 07 '15 at 12:46
0

There is no method inheritance when it is going about static methods. Calling Do() method in B class is allowed only for convenience, I believe. I suppose that your call B.Do() is compiled to A.Do() (because B class doesn't contain Do() method declaration). That is why your snippet returns A.

In order to make your method return B you should write:

public class A
{
    public static void Do()
    {
    }
}

public class B : A
{
    public static void Do()
    {
    }
}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
pwas
  • 3,225
  • 18
  • 40