可能重复:
.NET: Determine the type of “this” class in its static method
如何使GetType()可以从static方法访问?
我有一个抽象的基类
abstract class MyBase
{
public static void MyMethod()
{
var myActualType = GetType(); // this is an instance method
doSomethingWith(myActualType);
}
}以及那个类的实现。(我可以有许多实现。)
class MyImplementation : MyBase
{
// stuff
}我怎样才能让myActualType成为typeof(MyImplementation)
发布于 2011-11-20 00:46:54
这就是我所用的模式。
abstract class MyBase
{
public static void MyMethod(Type type)
{
doSomethingWith(type);
}
}发布于 2011-10-20 17:25:00
静态方法中的“类型”总是特定的类型,因为不存在虚拟静态方法。
在你的例子中,这意味着你只需要写:
var myActualType = typeof(MyBase);因为MyMethod的“类型”是静态的,所以始终是MyBase的静态方法。
发布于 2011-10-20 17:28:50
那这个呢?
abstract class MyBase<T>
{
public static void MyMethod()
{
var myActualType = typeof(T);
doSomethingWith(myActualType);
}
}
class MyImplementation : MyBase<MyImplementation>
{
// stuff
}https://stackoverflow.com/questions/7839691
复制相似问题