Note that there are some explanatory texts on larger screens.

plurals
  1. POInvalidOperationException in release-mode of visual studio since using .Net 4.0
    primarykey
    data
    text
    <p>I have some trouble to port an existing .NET 3.5 Application to .NET 4.0. The Code isn't written by myself so I didn´t know in detail why the things are as they are.</p> <p>This is the Situation: Code works fine if the application is started from Visual Studio (Release or Debug-Mode doesn’t matter) and also if the application is started form Debug-folder The Problem is the Release-deploy, because is doesn’t work well since 4.0 (and also in 4.5) :-/</p> <p>This is the initial call:</p> <pre><code>someObject.Text = Elements.GetElement(Int16.Parse(cb1.Text)); </code></pre> <p>And Here is the code:</p> <pre><code>public class Elements : EnumBase&lt;int, Elements&gt; { 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 =&gt; v.Value == id).Text; } } [Serializable()] public class EnumBase&lt;T, E&gt; : IEqualityComparer&lt;E&gt; where E : EnumBase&lt;T, E&gt; { private static readonly List&lt;E&gt; list = new List&lt;E&gt;(); protected string text; protected T value; protected static IList&lt;E&gt; BaseItemList { get { return list.Distinct(new EnumBase&lt;T, E&gt;(false)).ToList(); } } protected EnumBase() { list.Add(this as E); } /// &lt;summary&gt; /// Constructor for distinct to avoid empty elements in the list /// &lt;/summary&gt; private EnumBase(bool egal) {} public string Text { get { return text; } } public T Value { get { return value; } } #region IEqualityComparer&lt;E&gt; Member // ... #endregion } </code></pre> <p>The key is <code>return BaseItemList.Single(v =&gt; v.Value == id).Text;</code>. It throws a <code>InvalidOperationException</code>, because in Release <code>public static readonly Elements Element1 = Create("Number 0", 0);</code> and <code>public static readonly Elements Element2 = Create("Number 1", 1);</code> aren't ready. In the moment of the Exception is BaseItemList empty (BaseItemList.Count = 0). I am not sure why this happened in release form bin-folder and not in release out of visual studio. For tests I deactivated "Optimize code" in project-properties but it doesn’t help.</p> <p>Surely the construct isn't the best, but I want to know what is different in .Net 4.0 that bring the code to flatter.</p> <p>Thanks for help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload