0

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!

OpenMask
  • 21
  • 1
  • 4
  • First are you sure that in the C# main function m_Descriptions is not null. Second have you tried passing explicitly the static m_Descriptions in a modified function bool ManagedScriptInterface::GenSignal(String^ sSignalName, double pValue, MySignal^ test, List^ description) – PilouPili Sep 07 '18 at 11:14
  • Of course m_Descriptions is not null in my C# program. I tried to explicitly pass the static m_Descriptions and I succeed to get it without any problem in ManagedScriptInterface::GenSignal function. However, in my object MySignal^ test all the static members are equal to nullptr (of course m_Descriptions is null also). It looks like the static members of my object (MySignal^ test) are re-initialized. I suspect that a static constructor is called and is "erasing" my static members. For your information, I don't have any static constructor implemented in my C# code. – OpenMask Sep 10 '18 at 09:28
  • 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. This post also helped me in my investigations: https://stackoverflow.com/questions/9806372/static-variable-instances-and-appdomains-what-is-happening – OpenMask Sep 10 '18 at 10:21

1 Answers1

1

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?

OpenMask
  • 21
  • 1
  • 4