Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p> The exception "Cannot implicitly convert type 'void' to 'object'" is thrown when attempting to assign the result of a void method to a variable. While this is normally a compiler error, if it's seen during execution then it usually occurred as a result of dynamic invocation of a method (e.g. using the dynamic keyword or using a dynamic language like IronPython).</p> <p>.NET 4.5.1 introduced a overloads for <code>Marshal.SizeOf</code> and <code>Marshal.PtrToStructure</code> and dynamic method invocation may pick the new overload under certain circumstances. See Microsoft <a href="http://support.microsoft.com/kb/2909958" rel="nofollow">KB2909958</a>. The above exception wouldn't be coming from the P/Invoke itself, but from the subsequent marshaling operations.</p> <p>The easiest solution to these kinds of issues is to just introduce a cast within the call to ensure that the correct method is resolved.</p> <pre class="lang-cs prettyprint-override"><code>#Code from 4.5 dynamic dynamicVar = ... object boxedStruct = Marshal.PtrToStructure(myPtr, dynamicVar.GetType()); // Above resolves to 'object Marshal.PtrToStructure(IntPtr, Type)' in 4.5 // and resolves to 'void Marshal.PtrToStructure&lt;T&gt;(IntPtr, T)' in 4.5.1 int size = Marshal.SizeOf(dynamicVar.GetType()); // Above resolves to 'object Marshal.SizeOf(Type)' in 4.5 // and resolves to 'void Marshal.SizeOf&lt;T&gt;(T)' in 4.5.1 #Fixed for 4.5.1 dynamic dynamicVar = ... object boxedStruct = Marshal.PtrToStructure(myPtr, (Type)dynamicVar.GetType()); // Adding the cast forces the code to resolve to // 'object Marshal.PtrToStructure(IntPtr, Type)' int size = Marshal.SizeOf((Type)dynamicVar.GetType()); // Adding the cast forces the code to resolve to // 'int Marshal.SizeOf(Type)' </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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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