如何修复“CA1810:内联初始化引用类型静态字段"有一个抽象的基础......? [英] How to fix "CA1810: Initialize reference type static fields inline" with an abstract base...?

查看:12
本文介绍了如何修复“CA1810:内联初始化引用类型静态字段"有一个抽象的基础......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我拥有的代码的简化部分:

Here's the simplified parts of code that I have:

abstract class DataManager<TValue>
{
    protected static Dictionary<string, TValue> Values;
}

然后我有:

class TextManager : DataManager<string>
{
    static TextManager()
    {
        Values = ... // Fill with data
    }
}

而且,现在我得到了 CA1810.我看到了一些解决方案,比如公开 Values 并将它们设置在其他地方,但我不喜欢那样,或者在 TextManager 中创建一个静态方法来做同样的事情,但在程序启动时调用,但我也不喜欢那样.

And, now I'm getting CA1810. I see a few solutions, like making Values public and setting them elsewhere, but I don't like that, or making a static method in TextManager to do the same thing, but is invoked when the program starts, but I don't like that either.

我认为从示例中可以明显看出,每个 TValue 应该只填充一次 Values.那么,您认为这里的最佳解决方案是什么?

I think it's obvious from the example, the Values should only be filled once per TValue. So, what do you think would be the best solution here?

推荐答案

我会关闭规则.问题是,您有一个规则 (AFAIK) 旨在警告您使用静态构造函数的潜在性能影响.我会说静态属性的初始化可以通过静态构造函数或内联来完成(如 MSDN 建议).在您的情况下,您不能内联执行此操作,因为:

I would turn off the rule. The thing is, you have a rule that (AFAIK) is designed to warn you about the potential performance hit of using a static constructor. I would say that initialization of a static property can be done either via a static constructor or inline (as suggested by MSDN). In your case you can't do it inline because:

  1. 您只有子类中的实际值
  2. 没有抽象静态方法之类的东西,因此您不能将实际的内联初始化委托给 TextManager.

这样就留下了静态构造函数选项,这基本上意味着关闭规则(这意味着是的,微软.我知道这可能会影响性能,但我知道我在做什么").

So that leaves the static constructor option, which basically means turning off the rule (which means "yes, Microsoft. I know this might be dangerous for performance, but I know what I'm doing").

MSDN 声明:它如果不考虑性能,则可以安全地取消此规则的警告;或者如果静态初始化引起的全局状态更改代价高昂,或者必须保证在调用该类型的静态方法或该类型的实例之前发生已创建."

MSDN states: "It is safe to suppress a warning from this rule if performance is not a concern; or if global state changes that are caused by static initialization are expensive or must be guaranteed to occur before a static method of the type is called or an instance of the type is created."

========================================================================

=======================================================================

试试这个(警告:在 Mono 2.6.7 上测试,而不是 .NET):

Try this (warning: tested on Mono 2.6.7, not .NET):

abstract class DataManager<TValue>
{
    protected static Dictionary<string, TValue> Values=new Dictionary<string, TValue>();
}

class TextManager : DataManager<string>
{
    static TextManager()
    {
        Values.Add("test","test");
    }

    public static string test()
    {
        return Values["test"];
    }
}

class IntManager : DataManager<int>
{
    static IntManager()
    {
        Values.Add("test",1);
    }

    public static int test()
    {
        return Values["test"];
    }   
}

public static void Main (string[] args)
{
    Console.WriteLine(IntManager.test());    
    Console.WriteLine(TextManager.test());    
}

这篇关于如何修复“CA1810:内联初始化引用类型静态字段"有一个抽象的基础......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆