c# - 自使用 .Net 4.0 以来 visual studio Release模式中的 InvalidOperationException

标签 c# linq .net-4.0 enums release

我在将现有的 .NET 3.5 应用程序移植到 .NET 4.0 时遇到了一些问题。代码不是我自己写的,所以我不知道为什么会这样。

情况是这样的: 如果应用程序是从 Visual Studio 启动的(发布或 Debug模式无关紧要),并且如果应用程序是从调试文件夹启动的,代码也能正常工作 问题是发布部署,因为自 4.0(以及 4.5)以来它不能正常工作:-/

这是初始调用:

someObject.Text = Elements.GetElement(Int16.Parse(cb1.Text));

代码如下:

public class Elements : EnumBase<int, Elements>
{
    public static readonly Elements Element1 = Create("Number 0", 0);
    public static readonly Elements Element2 = Create("Number 1", 1);

    private static Elements Create(string text, int value) 
    {
        return new Elements() { text = text, value = value };
    }

    public static String GetElement(int id)
    {

        // The Following Code safes the day and let the release deploy work fine.
        // It doesn´t matter if the condition becomes true or not to runtime.
        /* 
        if (id == 999999999)
        {
            Elements el = Element1;
        }
        */

        // Release deploy works also fine if you do the following line in a loop instead of linq.
        return BaseItemList.Single(v => v.Value == id).Text; 
    }
}

[Serializable()]
public class EnumBase<T, E> :  IEqualityComparer<E> 
        where E : EnumBase<T, E>
{
    private static readonly List<E> list = new List<E>();
    protected string text;
    protected T value;

    protected static IList<E> BaseItemList
    {
        get
        {
            return list.Distinct(new EnumBase<T, E>(false)).ToList();
        }
    }

    protected EnumBase()
    {
        list.Add(this as E);
    }

    /// <summary>
    /// Constructor for distinct to avoid empty elements in the list
    /// </summary>   
    private EnumBase(bool egal) {}

    public string Text
    {
        get { return text; }
    }

    public T Value
    {
        get { return value; }
    }


    #region IEqualityComparer<E> Member

    // ...

    #endregion
}

关键是return BaseItemList.Single(v => v.Value == id).Text;。它抛出一个 InvalidOperationException,因为在 Release public static readonly Elements Element1 = Create("Number 0", 0);public static readonly Elements Element2 = Create("Number 1", 1); 不是准备好。在发生异常的那一刻,BaseItemList 为空 (BaseItemList.Count = 0)。 我不确定为什么这发生在发布形式的 bin 文件夹中,而不是在 visual studio 之外的发布中。 对于测试,我在项目属性中停用了“优化代码”,但这没有帮助。

当然,该构造不是最好的,但我想知道 .Net 4.0 中有哪些不同之处使代码更平坦。

感谢帮助

最佳答案

我认为问题是您依赖静态初始值设定项来运行 Elements,尽管您没有引用其中的任何字段。没有静态构造函数的类型中的类型初始值设定项只能保证在第一次访问静态字段之前运行。 C# 5 规范的第 10.5.5.1 节:

If a static constructor (§10.12) exists in the class, execution of the static field initializers occurs immediately prior to executing that static constructor. Otherwise, the static field initializers are executed at an implementation-dependent time prior to the first use of a static field of that class.

10.12 节有:

The static constructor for a closed class type executes at most once in a given application domain. The execution of a static constructor is triggered by the first of the following events to occur within an application domain:

  • An instance of the class type is created.
  • Any of the static members of the class type are referenced.

类型初始化的实现changed in .NET 4 ,但这只是一个实现细节 - 您的代码之前已损坏,只是您不知道而已。

如果您将代码更改为:

static Elements() {}

Elements 类中,then 我相信它会起作用 - 因为静态构造函数强制类型初始化发生在紧接第一个 member 访问之前,而不仅仅是“在第一个字段访问之前的某个时间点”。

就我个人而言,我对一般模式持怀疑态度,但这是一个稍微不同的问题。

关于c# - 自使用 .Net 4.0 以来 visual studio Release模式中的 InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14495757/

相关文章:

c# - imagebutton 图像 url 在中继器上未动态更改

c# - 在控制台关闭时运行代码?

c# - C#中的字符串有多长?

c# - 如何编写扩展方法来返回正在使用的类型?

c# - 将对象列表转换为元组列表而不进行迭代

c# - 如何使用 LINQ Contains(string[]) 而不是 Contains(string)

c# - Distinct() 方法是否保持序列的原始顺序不变?

c# - 对于 C# 中的匹配,Regex 实例线程是否安全

c# - .NET 4 中的比较器<T>.Create

c# - Entity Framework 核心 : Extend Entity without partial