Romain

Romain

  • NA
  • 76
  • 39.3k

Refer to an attribute within the serialization

Dec 11 2020 9:54 AM
Good morning,
 
I am working on a software which mix C# and C++/CLI between different application domains. I have implemented a solution with cross app domain singleton and ISerialisable interface in order to send information between two different application domains. It works fine but now I encounter a problem with the object that is serialized. The objet is used for displaying a message ( the message comes from C++/CLI layer and "travel" between the application domains with cross app domain singleton ) in a text box of a windows form. I got the message but I can't display it in the textbox because I don't have any reference on it (due to the process of serialization). Is there any way to do this ?
 
See below the code I am using :
  1. [Serializable]  
  2. class MsgObserver : ISerializable  
  3. {  
  4.     public System.Windows.Forms.RichTextBox m_rTxtBox;  
  5.    
  6.     public MsgObserver(System.Windows.Forms.RichTextBox txtBox)  
  7.     {  
  8.         m_rTxtBox = txtBox;  
  9.     }  
  10.     public void notifyInfoMessage(string infoMessage)  
  11.     {  
  12.         m_rTxtBox.Text += "INFO :" + infoMessage;  
  13.     }  
  14.     protected MsgObserver(SerializationInfo info, StreamingContext context)  
  15.     {  
  16.         m_rTxtBox = (System.Windows.Forms.RichTextBox)info.GetValue("resultBox"
    typeof(System.Windows.Forms.RichTextBox));  
  17.     }  
  18.     void ISerializable.GetObjectData(SerializationInfo oInfo, StreamingContext oContext)  
  19.     {  
  20.         oInfo.AddValue("resultBox", m_rTxtBox);  
  21.     }  
  22. }  
Before the process of calling C#/CLI, I initialize the object like that :
MsgObserver observer = new MsgObserver(txtBox);
What I saw is during the process of serialization, I enter to "protected MsgObserver" constructor and I lost the reference to the text box.
Thank you in advance for any help.
Romain.