none
Alternate way to serialize data using Resource Manager RRS feed

  • Question

  • I have an custom control which inherited from component and one of the property of  my custom control is MemoryStream type. 

    this.components = new System.ComponentModel.Container();
                System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
               
                customcontrol  = new MyControl(this.components);
                this.panel2 = new System.Windows.Forms.Panel();
                ((System.ComponentModel.ISupportInitialize)(this.customcontrol )).BeginInit();
                this.SuspendLayout();
                // 
                // customcontrol  
                // 
             
               
                this.customcontrol.LayoutStream = ((System.IO.MemoryStream)(resources.GetObject("customcontrol  .LayoutStream")));

    Form1.Rex

      </metadata>
      <data name="customcontrol.LayoutStream" mimetype="application/x-microsoft.net.object.binary.base64">
        <value>
            AAEAAAD/////AQAAAAAAAAAEAQAAABZTeXN0ZW0uSU8uTWVtb3J5U3RyZWFtCgAAAAdfYnVmZmVyB19v
            cmlnaW4JX3Bvc2l0aW9uB19sZW5ndGgJX2NhcGFjaXR5C19leHBhbmRhYmxlCV93cml0YWJsZQpfZXhw
    
    </value>
      </data>
      <metadata name="panel2.Label" xml:space="preserve">
        <value>panel2</value>
      </metadata>

    I have stored some layout state information in the LayoutStream property using MemoryStream which is generated in resource file. But In NetCore 3.0 MemoryStream is not supported to serialize the data which stored in resource file. Since it is not marked serializable. Please refer this link for this issue ("https://social.msdn.microsoft.com/Forums/windows/en-US/7b16d0c7-4d31-4455-876b-1c757d75882c/not-able-to-serialize-memorystream-in-net-core-3?forum=winforms".

    Please suggestion any alternate solution to serialize the data which stored in resource file.

    https://github.com/dotnet/coreclr/issues/17460




    • Changed type Magi02 Monday, October 14, 2019 6:14 AM
    • Changed type Magi02 Monday, October 14, 2019 6:14 AM
    • Edited by Magi02 Monday, October 14, 2019 8:59 AM
    Monday, October 14, 2019 5:50 AM

All replies

  • Hi Team,

    Any update on this?

    Regards,

    Magi

    Monday, October 14, 2019 9:18 AM
  • I don't suppose it's straight forward to edit that stream via the editors or the resx.  Have you considered setting Localizable(false) on that property then exposing other properties that are more easily editable / localizable?    If that doesn't work, perhaps you can explain in more detail why serializing this as a MemoryStream is a useful representation.

    Thanks,

    Eric

    Monday, October 14, 2019 4:02 PM
  • I think their program already have the saved serialized MemoryStream(s) in embedded data, and they don't want to regerenerate all of them again when porting to .NET Core.

    Btw, I think Viorel_ 's solution on embedding byte array sounds reasonable to me, and easy enough to implement too - just add a form that will enumerate the resources and save the MemoryStream.ToArray() to a file if it is (I mean the is operator) MemoryStream, then embed them to your new .NET Core project. (System.Array is serializable in .NET Core)

    When you have to use it, then just new a MemoryStream with that byte array.


    Tuesday, October 15, 2019 1:17 AM
    Answerer
  • Hi Cheong

    Thanks for your suggestion.

     

    i've missed to convey my requirement clearly and now i'm explaining the exact requirement. If i change anything in designer (Winforms designer), that will be written in resource file(.resx) as Memory stream. My question is how to write the serialization information to the resx file as ByteArray. 

    Thanks,

    Magi

    Tuesday, October 15, 2019 4:55 AM
  • I think if you follow this migration guide, the generated code should show you how VS2019 handles .resx file as embedded resources.

    Note that the Windows Form Designer for .NET Core is a seperate download that you can install with instructions here. (Note that it is still in preview, and as noted in the page it currently supported only a limited set of controls. I would recommand wait until the Gold version of new Form Designer to migrate)

    I suggest you to install it, create a new .NET Core WinForm project, add a new Form and see the code it generated and see how it deals with RESX files.

    My current work don't use C# so I don't have VS2019 to test with, so you have to try it yourself to see how it goes.

    Tuesday, October 15, 2019 6:01 AM
    Answerer
  • Hi Cheong

    I have used byte[] type instead of using MemoryStream, it works as excepted in FrameWork project. But In NetCore project it throws below exception we deserialize the data.

    Exception: https://social.msdn.microsoft.com/Forums/vstudio/en-US/e2a3212e-0a1e-4863-b6eb-6a882bac0f53/unable-to-find-assembly-exceptionwhen-perform-deserialization?forum=csharpgeneral

    Thanks,

    Magi

    Tuesday, October 15, 2019 12:57 PM
  • Did you regenerate the .csproj file as indicated in the migration guide?

    I think your project still try to load mscorlib for full .NET framework. (The mscorlib classes has been moved to System.Runtime and so on in .NET Core)

    You may try to install Compatibility Pack that will forward reference to corresponding libraries and tell you what to fix and see if it helps. (In fact this is also suggested in the migration guide. So if you haven't taken a closer look on that article, I strongly recommand you to find some time to do so)


    Wednesday, October 16, 2019 1:45 AM
    Answerer
  • Hi Cheong

    I'm facing same issue, even after installed Compatibility Pack. Here I have attached sample application which contain both framework project and NetCore project. While run framework project it works as excepected but it fails with NetCore project.

    In the attached project, i have created custom control which has serliazable property called LayoutStream. You can able to reproduce it while run the attached NetCore project.

    Sample Link: https://drive.google.com/file/d/1WbRSuxCSQKKT5tVoH-6jY5m9t3luYRgb/view

    Here CustomControl added as assembly. Please find code for CustomControl

    Class

    [ToolboxItem(true)]
        public class Custom : Control
        {
            public Custom()
            {
                this.Size = new Size(30, 30);
                this.BackColor = Color.Red;
                LayoutStream = new CustomControl.SerializableByteList();
            }
            private CustomControl.SerializableByteList layoutStream;
            
            public CustomControl.SerializableByteList LayoutStream
            {
                get
                {
                    return layoutStream;
                }
                set
                {
                    layoutStream = value;
                }
            }
        }
    
    [Serializable]
        public class SerializableByteList
        {
            public SerializableByteList()
            {
                Bytes = new List<byte>();
            }
    
            public List<byte> Bytes { get; set; }
        }

    Screenshot:

    Please check the sample and suggest any other solution.

    Thanks,


    • Edited by Magi02 Wednesday, October 16, 2019 5:42 AM
    Wednesday, October 16, 2019 5:41 AM
  • With limited tools in hand, the first thing I notice is that your CustomControl.dll neither targets .NET Standard 2+ nor targets .NET Core 3.

    And then your .csproj still targets v4.6, so I installed VS2019 at home at try build a .NET Core Winform application there.

    The use of "dotnew new winforms -o <filename>.csproj -n <namespace>" did successfully generate a blank project template. When I go ahead to try make it locallizable in order to force it generate .resx file which I can then go ahead and add your serialized data, the Windows Form Designer simply crashed. (This is the first thing that I test with, since if this does not work, all changes you successfully put in .Designer.cs file will vanish when you use Windows Form Designer for .NET Core to open it)

    You cannot add .resx file with the same name there because the IDE will create a Form1.Designer.cs that will overwrite the Form Designer generation code. So my experiment will stop here.

    Until there is new version of Windows Form Designer that'll properly handle Form Embedded resources, or unless you will tolerate the pain to manually modify code to load resources from the assembly itself instead of Forms on Form_Load event, maybe you should stick with the full .NET Framework versions.


    Thursday, October 17, 2019 3:22 AM
    Answerer