Note that there are some explanatory texts on larger screens.

plurals
  1. PO"Invalid type owner for DynamicMethod" error when sorting an Interface
    primarykey
    data
    text
    <p>We're using <a href="http://aboutcode.net/" rel="nofollow">Andrew Davey's</a> <code>BindingListView&lt;T&gt;</code> class via <a href="http://blw.sourceforge.net/" rel="nofollow">sourceforge</a> to bind collections to a <code>DataGridView</code> and allow sorting and filtering. </p> <p>This works fine for normal collections. In one case however the collection we're binding to is an Interface type and we get this error if we try to sort on it:</p> <p><code>Invalid type owner for DynamicMethod</code></p> <p>The error is deep in Andrew Davies' code so it's hard for us to know where to start. </p> <pre><code> private static Comparison&lt;T&gt; BuildValueTypeComparison(PropertyInfo pi, ListSortDirection direction) { MethodInfo getMethod = pi.GetGetMethod(); Debug.Assert(getMethod != null); DynamicMethod dm = new DynamicMethod("Get" + pi.Name, typeof(int), new Type[] { typeof(T), typeof(T) }, typeof(T), true); //^^^ ======== Here's the line reporting the error=========== ^^^ ILGenerator il = dm.GetILGenerator(); // Get the value of the first object's property. il.Emit(OpCodes.Ldarg_0); il.EmitCall(OpCodes.Call, getMethod, null); // Box the value type il.Emit(OpCodes.Box, pi.PropertyType); // Get the value of the second object's property. il.Emit(OpCodes.Ldarg_1); il.EmitCall(OpCodes.Call, getMethod, null); // Box the value type il.Emit(OpCodes.Box, pi.PropertyType); // Cast the first value to IComparable and call CompareTo, // passing the second value as the argument. il.Emit(OpCodes.Castclass, typeof(IComparable)); il.EmitCall(OpCodes.Call, typeof(IComparable).GetMethod("CompareTo"), null); // If descending then multiply comparison result by -1 // to reverse the ordering. if (direction == ListSortDirection.Descending) { il.Emit(OpCodes.Ldc_I4_M1); il.Emit(OpCodes.Mul); } // Return the result of the comparison. il.Emit(OpCodes.Ret); // Create the delegate pointing at the dynamic method. return (Comparison&lt;T&gt;)dm.CreateDelegate(typeof(Comparison&lt;T&gt;)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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