简体   繁体   English

模板方法访问静态变量的问题

[英]Template method accessing static variables issue

Suppose I have two classes that both contain a static variable, XmlTag. 假设我有两个类都包含一个静态变量XmlTag。 The second class inherits from the first class. 第二类继承自第一类。 I have a template method that needs to obtain the XmlTag depending on the which type it is using. 我有一个模板方法,需要根据使用的类型获取XmlTag。 What would be the best way to accomplish this, without having to create an instance of the type? 无需创建该类型的实例的最佳方法是什么? Here is an example(that won't compile), that should hopefully illustrate what I'm talking about. 这是一个示例(不会编译),希望可以说明我在说什么。

class A{
public static readonly string XmlTag = "AClass";
}

class B : A {
public static readonly string XmlTag = "BClass";
}

This method is currently invalid.. Static variables can't be accessed from Type paramaters apparently. 该方法当前无效。显然,无法从Type参数访问静态变量。

string GetName<T>(T AClass) where T : A
{
    return T.XmlTag;
}

First off, stop thinking of generic methods as templates. 首先,不要将通用方法视为模板。 They are NOT templates. 它们不是模板。 They have very different behaviour from templates; 它们的行为与模板完全不同。 your expectation that they behave like templates is possibly what is leading you astray in this situation. 您对它们的行为像模板的期望可能会使您在这种情况下误入歧途。

Here is a series of articles I wrote about your scenario and why it is illegal. 这是我写的一系列有关您的情况及其非法原因的文章。

http://blogs.msdn.com/ericlippert/archive/2007/06/14/calling-static-methods-on-type-variables-is-illegal-part-one.aspx http://blogs.msdn.com/ericlippert/archive/2007/06/14/calling-static-methods-on-type-variables-is-illegal-part-one.aspx

http://blogs.msdn.com/ericlippert/archive/2007/06/18/calling-static-methods-on-type-variables-is-illegal-part-two.aspx http://blogs.msdn.com/ericlippert/archive/2007/06/18/calling-static-methods-on-type-variables-is-illegal-part-two.aspx

http://blogs.msdn.com/ericlippert/archive/2007/06/21/calling-static-methods-on-type-variables-is-illegal-part-three.aspx http://blogs.msdn.com/ericlippert/archive/2007/06/21/calling-static-methods-on-type-variables-is-illegal-part-three.aspx

Note that of course the "dynamic" feature that I hint at in part three is in fact shipping with C# 4.0. 注意,当然,我在第三部分中暗示的“动态”功能实际上是C#4.0附带的。

To address your actual question: "what's the best way to do this?" 要解决您的实际问题:“最好的方法是什么?” Presumably you have some problem which you believe a mechanism like this would solve. 大概您有一些问题,您相信这样的机制会解决。 This mechanism doesn't actually exist in C#. 这种机制实际上在C#中并不存在。 It is impossible for us to deduce what problem you are actually trying to solve from the fact that you would like this mechanism to exist. 我们不可能从您希望这种机制存在的事实中得出您实际上要解决的问题。 Instead of asking "how can I make this impossible thing work in C#?" 而不是问“如何使这不可能的事情在C#中起作用”? instead describe the real problem you have , and we can take a crack at trying to come up with an existing C# mechanism that better solves your real problem. 而是描述您遇到的实际问题 ,我们可以尝试提出一个现有的C#机制,以更好地解决您的实际问题。

There's no direct way you could do that without resorting to reflection. 如果不依靠反思,就没有直接的方法可以做到这一点。

If you really want to do that (I encourage you to consider changing your high level design first): 如果您确实想这样做(我建议您先考虑更改高级设计):

To get a field value: 要获取字段值:

var returnValue = typeof(T).GetField("FieldName").GetValue(null);

To get a property: 要获得财产:

var returnValue = typeof(T).GetProperty("PropertyName").GetValue(null, null);

To invoke a method: 调用方法:

typeof(T).InvokeMember("MethodName", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.NonPublic, null, null, null);

Why don't you give the bas class a virtual GetTagName class, and have the inheriting classes override that one? 为什么不给bas类一个虚拟的GetTagName类,并让继承的类覆盖该类呢?

class A{
public virtual string GetXmlTagName()
  {
   return "AClass";
  }
}

it would solve your immediate symptom but Im not sure about solving your problem. 它可以解决您的直接症状,但是我不确定要解决您的问题。 It has a destinct code smell but without knowing more I might be wrong when I state that the notion of XML should exist in a differrent class and that having that in A and B is violating single responsibility principle. 它具有预定的代码味道,但是在我不知道更多的情况下,当我声明XML的概念应该存在于不同的类中并且在A和B中具有XML的概念违反了单一职责原则时,我可能是错的。 Basically I would know from the information Im just affraid of it 基本上我将从信息中得知

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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