Search code examples
c#c++.netstaticclr

Passing a C# object (containing a static object member) as a paramter to C++/CLI program


In my C# code I have a class

[Serializable] 
public class MySignal: IMySignal
{
    static List<IDescription> m_Descriptions;
    public string m_Comment;
    public IList<string> m_Names;
 }

I have a function in my C# main program calling a C++/CLI external program (using .NET Remoting). I am passing in this function a string, a double and my object "test" of type MySignal.

bool GenSignal(string sSignalName, double pValue, MySignal test);

In the C++/CLI code, I have the implementation of GenSignal function inside a managed class.

bool ManagedScriptInterface::GenSignal(String^ sSignalName, double pValue, MySignal^ test)
{
//Debugging is stopped here
//Some code... 

}

When debugging inside my function, I can get my object (MySignal^ test). Inside it I can access to all public members (m_Names and m_Comment). However, the static object member m_Descriptions (which declared static, and I need absolutely that in my C# program) is equal to nullptr.

My question is: How can I do to get my static object m_Descriptions visible and access it inside my function in C++/CLI.

I hope someone can help on this topic.

Many thanks!


Solution

  • I removed [Serializable] from my classes in C# and used MarshalByRefObject. It seems that fixes my problem. I don't have any solid background in C# but I think that the Serializable attribute is not suitable in my case since I am changing the Domain from one program in C# to another program in C++/CLI using .NetRemoting. Serializable attribute seems to crate a local copy of my object in the current AppDomain.

    This post also helped me in my investigations: Static Variable Instances and AppDomains, what is happening?