Search code examples
c#serializationhalcon

How to Serialize HTuple in C#?


Define the class:

    [Serializable]
    public class HalconConfig
    {

        public HTuple mNumber;
        public HObject mImage;

        public HalconConfig()
        {
            mNumber = 6;
            HalconFunction.BitmapToHObject(Resources.TestImage, out mImage);
        }
    }

And I want to save the HalconConfig object to a local file that can be read later

            HalconConfig _config1 = new HalconConfig();
            string _path = "./test.bin";

            BinaryFormatter _formatter = new BinaryFormatter();
            using (FileStream s = File.Create(_path))
            {
                _formatter.Serialize(s, _config1);
            }

            using (FileStream s = File.OpenRead(_path))
            {
                HalconConfig _config2 = (HalconConfig)_formatter.Deserialize(s);
                BaseFunction.SendLog($"{_config2.mNumber}", LogLevel.Debug);
            }

But I failed。HTuple cannot be used after deserialization。

enter image description here

I tried binary serialization and Json serialization,but both failed。


Solution

  • This problem has been solved. Essentially the reason was because I was using Halcon 18.11 and the provided dll was buggy and could not be serialized. I replaced it with Halcon 21.05 and the serialization function was implemented.