在的GetType静态方法静态、方法、GetType

2023-09-03 03:13:08 作者:Smile°归零

可能重复:   .NET:判断这一类的类型在它的静态方法

我怎样才能让的GetType()静态方法进行访问?

How can I make GetType() accessible from a static method?

我有这个抽象基类

abstract class MyBase
{
   public static void MyMethod()
   {
      var myActualType = GetType(); // this is an instance method
      doSomethingWith(myActualType);
   }
}

和那类的实现。 (我可以有多种实现。)

and an implementation of that class. (I could have many implementations.)

class MyImplementation : MyBase 
{
    // stuff
}

我怎样才能获得 myActualType 的typeof(MyImplementation)

推荐答案

这是我使用的模式。

abstract class MyBase
{
   public static void MyMethod(Type type)
   {
      doSomethingWith(type);
   }
}