如何从 C# 中的静态方法引用当前 class 的类型
How to refer to the type of the current class from a static method in C#
近年来,我一直试图为这个问题多次寻找答案,但我总是以解决方法将它扫到地毯下,通过传递类型来引用 class 类型参数。
我的项目是一个 class 库,实际上不应该允许这样做,因为 classes 负责重要的数据库完整性,用户有时会因为简单地传递错误的类型而严重搞砸事情。我知道有很多方法可以通过运行时检查(就是这种情况)来最小化它,但它仍然会导致反复出现的问题,我想通过适当的解决方案一劳永逸地结束它。
基本上我正在寻找的是 this
关键字或 typeof(this)
表达式 for class 的等价物,而不是它的实例 .
这是一个演示问题的示例:
public abstract class Base
{
protected static void InitializeTaggedFields(Type aType)
{
...
}
}
// User defined custom class inheriting my Base class
public class StoreThis : Base
{
[Tag("something")]
string Field1;
[Tag("something")]
string Field2;
[Tag("something_else")]
string Field3;
// This constructor should be in the Base class because user has
// to pass the type of his current class each time to initialize
// the class.
public StoreThis()
{
InitializeTaggedFields(typeof(StoreThis));
}
}
非常感谢任何建议。
恐怕这是不可能的 - 这是一件好事。
原因很简单,在 OOP 中,基础 class 不应使用派生 class 的固有信息。
虽然我不熟悉你的场景,你可以看看the factory method pattern。可以让工厂根据类类型填写标记字段。
除非我完全误解了一切,否则你想要的只是:
public abstract class Base
{
protected static void InitializeTaggedFields(Type aType)
{
// aType will be of the instantiated type:
// When a "StoreThis" is instantiated,
// aType.Equals(typeof(StoreThis)) is true,
// if a "StoreThat" was instatiated
// aType.Equals(typeof(StoreThat)) is true.
// etc, etc.
}
public Base()
{
InitializeTaggedFields(this.GetType());
}
}
// User defined custom class inheriting my Base class
public class StoreThis : Base
{
public StoreThis()
{
}
}
public class StoreThat : Base
{
public StoreThat()
{
}
}
您可能希望将 InitializeTaggedFields 设为私有,因为它不会在其他地方使用。
近年来,我一直试图为这个问题多次寻找答案,但我总是以解决方法将它扫到地毯下,通过传递类型来引用 class 类型参数。
我的项目是一个 class 库,实际上不应该允许这样做,因为 classes 负责重要的数据库完整性,用户有时会因为简单地传递错误的类型而严重搞砸事情。我知道有很多方法可以通过运行时检查(就是这种情况)来最小化它,但它仍然会导致反复出现的问题,我想通过适当的解决方案一劳永逸地结束它。
基本上我正在寻找的是 this
关键字或 typeof(this)
表达式 for class 的等价物,而不是它的实例 .
这是一个演示问题的示例:
public abstract class Base
{
protected static void InitializeTaggedFields(Type aType)
{
...
}
}
// User defined custom class inheriting my Base class
public class StoreThis : Base
{
[Tag("something")]
string Field1;
[Tag("something")]
string Field2;
[Tag("something_else")]
string Field3;
// This constructor should be in the Base class because user has
// to pass the type of his current class each time to initialize
// the class.
public StoreThis()
{
InitializeTaggedFields(typeof(StoreThis));
}
}
非常感谢任何建议。
恐怕这是不可能的 - 这是一件好事。
原因很简单,在 OOP 中,基础 class 不应使用派生 class 的固有信息。
虽然我不熟悉你的场景,你可以看看the factory method pattern。可以让工厂根据类类型填写标记字段。
除非我完全误解了一切,否则你想要的只是:
public abstract class Base
{
protected static void InitializeTaggedFields(Type aType)
{
// aType will be of the instantiated type:
// When a "StoreThis" is instantiated,
// aType.Equals(typeof(StoreThis)) is true,
// if a "StoreThat" was instatiated
// aType.Equals(typeof(StoreThat)) is true.
// etc, etc.
}
public Base()
{
InitializeTaggedFields(this.GetType());
}
}
// User defined custom class inheriting my Base class
public class StoreThis : Base
{
public StoreThis()
{
}
}
public class StoreThat : Base
{
public StoreThat()
{
}
}
您可能希望将 InitializeTaggedFields 设为私有,因为它不会在其他地方使用。