Note that there are some explanatory texts on larger screens.

plurals
  1. PO"CLR detected an invalid program." when calling Generic Methods
    primarykey
    data
    text
    <p>I write a large static method that takes a generic as a parameter argument. I call this method, and the framework throws a System.InvalidProgramException. This exception is thrown even before the first line of the method is executed.</p> <p>I can create a static class which takes the generic argument, and then make this a method of the static class, and everything works fine.</p> <p>Is this a .NET defect, or is there some obscure generic rule I'm breaking here?</p> <p>For the sake of completeness, I've included the method which fails, and the method which passes. Note that this uses a number of other classes from my own library (eg GridUtils), and these classes are not explained here. I don't think the actual meaning matters: the question is why the runtime crashes before the method even starts.</p> <p>(I'm programming with Visual Studio 2005, so maybe this has gone away in Visual Studio 2008.)</p> <p><b>This throws an exception before the first line is invoked:</b></p> <pre><code> private delegate void PROG_Delegate&lt;TGridLine&gt;(DataGridView dgv, IEnumerable&lt;TGridLine&gt; gridLines, string[] columns); public static void PopulateReadOnlyGrid&lt;TGridLine&gt;(DataGridView dgv, IEnumerable&lt;TGridLine&gt; gridLines, string[] columns) { if (dgv.InvokeRequired) { dgv.BeginInvoke ( new PROG_Delegate&lt;TGridLine&gt;(PopulateReadOnlyGrid&lt;TGridLine&gt;), new object[] { dgv, gridLines, columns } ); return; } GridUtils.StatePreserver statePreserver = new GridUtils.StatePreserver(dgv); System.Data.DataTable dt = CollectionHelper.ConvertToDataTable&lt;TGridLine&gt;((gridLines)); dgv.DataSource = dt; dgv.DataMember = ""; dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; GridUtils.OrderColumns&lt;TGridLine&gt;(dgv, columns); statePreserver.RestoreState(); dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; } </code></pre> <p><b>This works fine:</b></p> <pre><code> public static class Populator&lt;TGridLine&gt; { private delegate void PROG_Delegate(DataGridView dgv, IEnumerable&lt;TGridLine&gt; gridLines, string[] columns); public static void PopulateReadOnlyGrid(DataGridView dgv, IEnumerable&lt;TGridLine&gt; gridLines, string[] columns) { if (dgv.InvokeRequired) { dgv.BeginInvoke ( new PROG_Delegate(PopulateReadOnlyGrid), new object[] { dgv, gridLines, columns } ); return; } GridUtils.StatePreserver statePreserver = new GridUtils.StatePreserver(dgv); System.Data.DataTable dt = CollectionHelper.ConvertToDataTable&lt;TGridLine&gt;((gridLines)); dgv.DataSource = dt; dgv.DataMember = ""; dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; GridUtils.OrderColumns&lt;TGridLine&gt;(dgv, columns); statePreserver.RestoreState(); dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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