Corrupt Resource file

Posted by: bo-jangles on 8 September 2017, 1:06 pm EST

  • Posted 8 September 2017, 1:06 pm EST

    Hi


    I have a control that inherits from the Win Spread. Any change to the Spread properties results in the following compilation error. Is there a way of recreating the resource file?


    Attached is the project which should compile. Any changes to component property should result in compilation failing.


    Regards


    Bojan


    P.S. This happened before and I resolved it by creating a new class, and then manually copying source code from the original class to the new one.


     


    Error 1 Invalid Resx file. Type  in the data at line 162, position 5, cannot be loaded because it threw the following exception during construction: Object reference not set to an instance of an object. C:\Bojan\VS2008\ClassLibrary1\ClassLibrary1\z9iFps.resx ClassLibrary1


    Error 2 TargetInvocationException: Type  in the data at line 162, position 5, cannot be loaded because it threw the following exception during construction: Object reference not set to an instance of an object.
       at System.Resources.ResXResourceReader.ParseXml(XmlTextReader reader)
       at System.Resources.ResXResourceReader.EnsureResData()
       at System.Resources.ResXResourceReader.GetEnumerator()
       at Microsoft.Build.Tasks.ProcessResourceFiles.ReadResources(IResourceReader reader, String fileName)
       at Microsoft.Build.Tasks.ProcessResourceFiles.ReadResources(String filename, Boolean shouldUseSourcePath)
       at Microsoft.Build.Tasks.ProcessResourceFiles.ProcessFile(String inFile, String outFile)
    XmlException: Type  in the data at line 162, position 5, cannot be loaded because it threw the following exception during construction: Object reference not set to an instance of an object. Line 162, position 5.


    NullReferenceException: Object reference not set to an instance of an object.
       at FarPoint.Win.Spread.FpScrollBar.a()
       at FarPoint.Win.Spread.FpScrollBar.set_Buttons(FpScrollBarButtonCollection value)
       at FarPoint.Win.Spread.FpScrollBar..ctor(SerializationInfo info, StreamingContext context)
       at FarPoint.Win.Spread.FpHorizontalScrollBar..ctor(SerializationInfo info, StreamingContext context)
       at FarPoint.Win.Spread.SpreadHScrollBar..ctor(SerializationInfo info, StreamingContext context)
     C:\Bojan\VS2008\ClassLibrary1\ClassLibrary1\z9iFps.resx ClassLibrary1


    2008/03/SampleApp.zip
  • Replied 8 September 2017, 1:06 pm EST

    Hello,


    What change do I need to make in order to get the exception you are seeing?

  • Replied 8 September 2017, 1:06 pm EST

    Any property change e.g. set EditModeReplace to true.


    I get the same error on two machines, one running XP and VS 2005 and the other runnig Vista and VS 2008.

  • Replied 8 September 2017, 1:06 pm EST

    Thanks,


    That works.


    I had to put


    Public Class FpSpreadBase : Inherits FarPoint.Win.Spread.FpSpread
      _
      Public Overloads ReadOnly Property Controls() As Control.ControlCollection
        Get
          Return MyBase.Controls
        End Get
      End Property
    End Class


    in the spearate file and then inherit z9iFps from there. Otherwise I didn't see context menu in the designer. It's as if the designer so the first public class in the vb file and decided to work whith it.


    Bear in mind that I had this before and that resource doesn't get corrupted immediatelly.


    Regards


    BojanSmile [:)]

  • Replied 8 September 2017, 1:06 pm EST

    That workaround has a side effect of preventing any Control objects from getting serialized, so it will not work to drop controls on the design surface of your subclassed control (well, that part will work, but they won't get saved and reloaded).  That is probably not a problem for most people, but it should work without this, so I will open a bug to get this issue fixed.

    It appears that the framework code serializer for Control (System.Window.Forms.Design.ControlCodeDomSerializer) is checking for an InheritanceAttribute on the control, and if it finds one, goes through some extra code to serialize the Controls collection.  The Controls collection in FpSpread v4 contains the SpreadHScrollBar and SpreadVScrollBar controls, so they are getting serialized into the resx and this is causing problems because they are not intended to be serialized this way.  They are intended to be serialized with DesignerSerializationVisibility.Content, since they are always created and added to the FpSpread internally.


  • Replied 8 September 2017, 1:06 pm EST

    Here is a better workaround that does not break the serialization of other embedded controls:

     
    Imports FarPoint.Win.Spread
    Imports System.ComponentModel.Design.Serialization

    _
    Public Class FpSpreadBase : Inherits FpSpread

    End Class

    Public Class FpSpreadBaseCodeDomSerializer : Inherits CodeDomSerializer
    Public Overrides Function Deserialize(ByVal manager As IDesignerSerializationManager, ByVal codeObject As Object) As Object
    Dim o As Object = Nothing
    Dim base As CodeDomSerializer
    base = CType(manager.GetSerializer(GetType(FpSpread), GetType(CodeDomSerializer)), CodeDomSerializer)
    If Not base Is Nothing Then
    o = base.Deserialize(manager, codeObject)
    End If
    Return o
    End Function
    Public Overrides Function Serialize(ByVal manager As IDesignerSerializationManager, ByVal value As Object) As Object
    Dim o As Object = Nothing
    Dim base As CodeDomSerializer
    base = CType(manager.GetSerializer(GetType(FpSpread), GetType(CodeDomSerializer)), CodeDomSerializer)
    If Not base Is Nothing Then
    Dim spread As FpSpread = CType(value, FpSpread)
    Dim hscroll As SpreadHScrollBar = spread.HorizontalScrollBar
    Dim vscroll As SpreadVScrollBar = spread.VerticalScrollBar
    spread.Controls.Remove(hscroll)
    spread.Controls.Remove(vscroll)
    o = base.Serialize(manager, value)
    spread.Controls.Add(hscroll)
    spread.Controls.Add(vscroll)
    End If
    Return o
    End Function
    End Class
     You will need to add a reference to System.Design to build that.
  • Replied 8 September 2017, 1:06 pm EST

    Hello,


    You sent me a Class Library of an inherited Spread control. Are you using this control on another form and that is where you are setting the EditModeReplace property. I am not sure how you are changing the property in the project you sent to me,

  • Replied 8 September 2017, 1:06 pm EST

    I just open the control, click the design window and spread properties appear in the properties window. See attached screen dump.

  • Replied 8 September 2017, 1:06 pm EST

    I am investigating what is going on with your sample, and it appears that for some reason the scroll bar controls are getting serialized in the InitializeComponent code.  I have not yet figured out why this happens, as it does not happen when you put the control on a form, but I have found something you can do in the meantime to make this stop happening.  If you overload the Controls property and put  a DesignerSerializationVisibility attribute on it to hide it from code serialization in designers, then the problem goes away.  Here is what I did:


     
    Public Class FpSpreadBase : Inherits FarPoint.Win.Spread.FpSpread
    _
    Public Overloads ReadOnly Property Controls() As Control.ControlCollection
    Get
    Return MyBase.Controls
    End Get
    End Property
    End Class
    'Public Class z9iFps : Inherits FarPoint.Win.Spread.FpSpread
    Public Class z9iFps : Inherits FpSpreadBase
     
    The remainder of the code is unchanged.  I tried overloading the Controls property in your class, but that did not work.  After editing the properties with the property grid in design mode, an InitializeComponent method is generated in the FpSpreadBase class, but the scroll bar controls are not serialized anywhere, it it builds successfully.  I will continue investigating the reason why the scroll bar controls are getting serialized in the first place.
  • Replied 8 September 2017, 1:06 pm EST

    It occured to me this morning that with the better workaround using a CodeDomSerializer, you do not need the intervening FpSpreadBase class -- you can put the DesignerSerializer attribute directly on your subclass and it will work.

    EDIT: Scratch that; it doesn't work to do that.  You need to have the parent class there, to make the code serialization work in the ComponentDocumentDesigner correctly until we integrate that code to remove the scroll bars from the Controls collection into our CodeDomSerializer.
  • Replied 8 September 2017, 1:06 pm EST

    Hi,


    thanks for trying to sort this out, but your first solution works for me.


    I've got this in a separate class (and this one does not have a resource file created)


    Public Class FpsBase : Inherits FarPoint.Win.Spread.FpSpread


    _


    Public Overloads ReadOnly Property Controls() As Control.ControlCollection


    Get


    Return MyBase.Controls


    End Get


    End Property


    End Class


    and then my control inherits from FpsBase.


    Public Class z9iFps : Inherits FpsBase


    As a test I've added a image list to the control and it's still there after closing and reopening the project.


    Regards


    Bojan

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels