title: F# Marshall structs with field of delegate type
body: <p>I have a native C library and I want do some F# coding with it. The thing is I get exception: </p>
  
  <blockquote>
    <p>System.TypeLoadException: <strong>Cannot marshal field 'log' of type
    'LoggingModel': There is no marshaling support for this type.</strong><br>
    at
    System.StubHelpers.ValueClassMarshaler.ConvertToNative(IntPtr dst,
    IntPtr src, IntPtr pMT, CleanupWorkList&amp; pCleanupWorkList)<br>
    at
    FSI_0009.Initialize(ComponentOverrideFlags flags, LoggingModel&amp;
    loggingModel, ThreadingModel&amp; threadingModel, SchedulingModel&amp;
    schedulingModel, IntPtr memoryModel)<br>
    at
    .$FSI_0011.main@() 
    in
    D:\dev_p\f#\FunBindings\FunExample\Environment.fs:line 16 Stopped due
    to error</p>
  </blockquote>
  
  <p>Here the code:</p>
  
  <pre><code>module Interop
  
  [&lt;CLSCompliant(true); Flags&gt;]
  type LogTarget =
      | None = 0
      | Console = 1
      | Trace = 2
      | Custom = 4 
  
  [&lt;UnmanagedFunctionPointer(CallingConvention.Cdecl)&gt;]
  type LogCallback = delegate of LogTarget * string * string * nativeint -&gt; unit
  
  [&lt;UnmanagedFunctionPointer(CallingConvention.Cdecl)&gt;]
  type ReleaseCallback = delegate of nativeint -&gt; unit
  
  [&lt;Struct&gt;]
  type LoggingModel =
      val mutable targets : LogTarget
      val mutable log : LogCallback
      val mutable deleteModel : ReleaseCallback
      val mutable userparam : IntPtr
  
  [&lt;DllImport("CLIBRARY.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "txInitialize")&gt;]
  [&lt;MethodImpl(MethodImplOptions.ForwardRef)&gt;]
  extern int Initialize(ComponentOverrideFlags flags, LoggingModel&amp; loggingModel, ThreadingModel&amp; threadingModel, SchedulingModel&amp; schedulingModel, IntPtr memoryModel)
  
  module Environment
  
  let initialize =
      let mutable loggingModel = new LoggingModel()
      let mutable threadingModel = new ThreadingModel()
      let mutable schedulingModel = new SchedulingModel()
      Initialize(ComponentOverrideFlags.None, &amp;loggingModel, &amp;threadingModel, &amp;schedulingModel, IntPtr.Zero)
  </code></pre>
  
  <p>Basically, I get the aforementioned error when I try to execute "initialize" function in interactive.</p>
  
  <p>I would really appreciate any help.</p>
  
  <p><strong>Update:</strong> I've checked the code a bit more and noticed that outside of the interactive console it seems to be working, without failing with exceptions. I need to provide a bit more coverage for CLibrary to be sure. Meanwhile, if there anybody who knows what could cause this exception and how it could be prevented, I would really appreciate the answer.</p>