简体   繁体   中英

C# static method from object

I have various objects of different types. For all of them, I want to call a static method of their class. All the classes share the same method. How can I call this static method without explicitly calling the class?

You could accomplish this by putting a method in each object that calls the corresponding static method. However, the fact that you want to do this suggests that your design might be able to be improved. If you'd tell us what you're trying to accomplish, someone may be able to suggest a better approach.

If these classes all extend the same base class, then calling the method on the base class will work.

For example:

public class Base
{
  public static DoSomething()
  {
    //something
  }
}

public class A: Base
{
}

public class B: Base
{
}

The following method calls execute the same code:

A.DoSomething();
B.DoSomething();
Base.DoSomething();

You want to call every method on each of the individual classes? You have to call them explicitly, referencing each class individually.

Does the static method for every class have the same common code? Put it into a static class for use by all of the other classes, or create one or more extension methods.

Are you looking for something like you have something like List<object> where all of the objects are guaranteed to have a static method named, say MethodX() ?

If so you could reflect on them, look for the method name, and execute that.

Either that or inheritance like the others mention (which would be the correct way to go).

If you need to have a specific implementation for each type, I don't think a static method is the right approach... Instead, you shoud define an interface implemented by all your classes. You can then call the instance method defined by the interface on each object :

public interface IDoSomething
{
    void DoSomething();
}

public class A: IDoSomething
{
    public void DoSomething()
    {
        // implementation for A
    }
}

public class B: IDoSomething
{
    public void DoSomething()
    {
        // implementation for B
    }
}

Of course, if you don't need a specific implementation for each type, then you can just call Base.DoSomething (as explained by David)

I'm not sure what exactly you're trying to do. But using my imagination I come up with this implementation.

internal class Program
{
  private static void Main(string[] args)
  {
    var staticMethodClasses = new List<StaticMethodClassBase> 
                                         {new ClassA(), new ClassB()};

    foreach (StaticMethodClassBase item in staticMethodClasses)
    {
      Type t = item.GetType();
      MethodInfo staticMethod = 
           t.GetMethod("DoSomething", BindingFlags.Static | BindingFlags.Public);
      staticMethod.Invoke(null, null);
    }
  }
}

public abstract class StaticMethodClassBase
{
}

public class ClassA : StaticMethodClassBase
{
    public static void DoSomething()
    {
        Console.WriteLine("Class A");
    }
}

public class ClassB : StaticMethodClassBase
{
    public static void DoSomething()
    {
        Console.WriteLine("Class B");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM