Help on C# custom marshaling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jpogorman
    New Member
    • Oct 2007
    • 1

    Help on C# custom marshaling

    Hello,

    I am trying to get c# custom marshaling working in a particular scenario but it does not appear to be working or not jumping into my marshaling class when I try to debug it. I am try to implement a performance improvement in the transfer of thousands of objects (ClientInfoDesc riptor) over a remote call. The objects all have a model object (IDataDrivenMod el) to describe them but there may only two or three model objects in total. These model object could be large objects. Instead of transfering duplicate models I would like to marshal in the model id and when I retrieve the objects on the client side I will un marshal the model id and lookup the model from the cache.

    I would appreciate it if anyone could tell me where I may be going wrong or give me tips on how to better debug this custom marshaling. Or maybe there is a better way to get this performance improvement without custom marshaling?

    Thanks,
    JP

    [Serializable]
    public abstract class ADDInfoDescript or : IDDInfoDescript or
    {
    [MarshalAs(Unman agedType.Custom Marshaler, MarshalTypeRef = typeof(DDMarsha ler))]
    private IDataDrivenMode l ddModel = null;
    ...
    }

    // Class that has attribute being customed marshaled
    [Serializable]
    public class ClientInfoDescr iptor : ADDInfoDescript or
    {
    ...
    }

    // Marshaling class
    public sealed class DDMarshaler : System.Runtime. InteropServices .ICustomMarshal er
    {
    private static DDMarshaler marshaler = null;
    public static ICustomMarshale r GetInstance(str ing cookie)
    {
    if (marshaler == null)
    {
    marshaler = new DDMarshaler();
    }
    return marshaler;
    }

    public DDMarshaler()
    {
    }

    public object MarshalNativeTo Managed(System. IntPtr pNativeData)
    {
    int id = Marshal.ReadInt 32(pNativeData) ;
    return DDModelCache.In stance.GetModel (id);
    }

    public System.IntPtr MarshalManagedT oNative(object managedObj)
    {
    IDataDrivenMode l ddModel = (IDataDrivenMod el)managedObj;
    IntPtr unmanagedArray = Marshal.AllocHG lobal(sizeof(In t32));
    Marshal.WriteIn t32(unmanagedAr ray, ddModel.ID);
    return unmanagedArray;
    }

    public void CleanUpManagedD ata(object ManagedObj)
    {
    }

    public int GetNativeDataSi ze()
    {
    return sizeof(Int32);
    }

    public void CleanUpNativeDa ta(System.IntPt r pNativeData)
    {
    Marshal.FreeHGl obal(pNativeDat a);
    }
    }
Working...