title: C# Define custom UnmanagedType for the MarshalAs attribute class
body: <p>is it possible to define a custom UnmanagedType for the MarshalAs attribute class?
  Specifically I want to convert a long int unix time into a DateTime type. Something like this:</p>
  
  <pre><code>[MarshalAs(UnmanagedType.LongTimeUnix)]
  public DateTime Time;
  </code></pre>
  
  <p>Where do I have to put the custom LongTimeUnix enumeration type and where to put the time conversion code:</p>
  
  <pre><code>public static DateTime ConvertUnix2DateTime(long timeStamp)
  {
          DateTime DT = new DateTime(1970, 1, 1, 0, 0, 0, 0);
          DT = DT.AddSeconds(timeStamp);
          return DT;
  }
  </code></pre>
  
  <p>When transferring the data with</p>
  
  <pre><code>(SomeStruct)Marshal.PtrToStructure(
   IntPtr,
   typeof(SomeStruct));
  </code></pre>
  
  <p>I want that the long time unix is automatically converted with the code sinppet above.
  Do I have to inherit from the MarshalAs class and write the conversion into this class?
  Thanks, Juergen</p>
  
  <p><strong>Update</strong>
  Here is the custom marshaller:</p>
  
  <pre><code>class MarshalTest : ICustomMarshaler
  {
      public void CleanUpManagedData(object ManagedObj)
      {
          throw new NotImplementedException();
      }
  
      public void CleanUpNativeData(IntPtr pNativeData)
      {
          throw new NotImplementedException();
      }
  
      public int GetNativeDataSize()
      {
          return 8;
      }
  
      public IntPtr MarshalManagedToNative(object ManagedObj)
      {
          throw new NotImplementedException();
      }
  
      public object MarshalNativeToManaged(IntPtr pNativeData)
      {
          long UnixTime = 0;
          try
          {
              UnixTime = Marshal.ReadInt64(pNativeData);
          }
          catch (Exception e)
          {
  
             QFXLogger.Error(e, "MarshalNativeToManaged");
          }
          DateTime DT = new DateTime(1970, 1, 1, 0, 0, 0, 0);
          DT = DT.AddSeconds(UnixTime);
          return DT;
      }
   }
  </code></pre>
  
  <p>Here is the class definition:</p>
  
  <pre><code>unsafe public struct MT5ServerAttributes
  {
      /// &lt;summary&gt;
      /// Last known server time.
      /// &lt;/summary&gt;
      [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MarshalTest))]
      public DateTime CurrentTime;
  
      //[MarshalAs(UnmanagedType.U8)]
      [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MarshalTest))]
      public DateTime TradeTime;
  
   }
  </code></pre>
  
  <p>And finally the code to marshal the data from unmanaged memory:</p>
  
  <pre><code>try
  {
     MT5ServerAttributes MT5SrvAttributes = (MT5ServerAttributes)Marshal.PtrToStructure(mMT5Proxy.MT5InformationProxy.ServerData,
                                                                      typeof(MT5ServerAttributes));
  }
  catch (Exception e)
  {
  
  QFXLogger.Error(e, "ConsumeCommand inner");
  }
  </code></pre>
  
  <p>When running this the following excpetion is thrown(which is not a direct exception ot PtrToStructure!)
  Cannot marshal field 'CurrentTime' of type 'QFX_DLL.MT5ServerAttributes': Invalid managed/unmanaged type combination (the DateTime class must be paired with Struct).
  Any ideas?</p>