SHARE
TWEET

VisualStudioWorkaroundSerializer

JeremyLakeman Sep 15th, 2015 (edited) 238 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.ComponentModel.Design.Serialization
  2. Imports System.ComponentModel
  3. Imports System.ComponentModel.Design
  4. Imports System.Reflection
  5. Imports System.CodeDom
  6.  
  7. Public Class VisualStudioWorkaroundSerializer
  8.   Inherits CodeDomSerializer
  9.  
  10.   Private Function GetNormalSerializer(manager As IDesignerSerializationManager, codeObject As Object) As CodeDomSerializer
  11.     ' TODO examine codeObject's ancestors to get the right serializer?
  12.     If TypeOf codeObject Is Control Then
  13.       Return DirectCast(manager.GetSerializer(GetType(Control), GetType(CodeDomSerializer)), CodeDomSerializer)
  14.     End If
  15.     Return DirectCast(manager.GetSerializer(GetType(Component), GetType(CodeDomSerializer)), CodeDomSerializer)
  16.   End Function
  17.  
  18.   Public Overrides Function Deserialize(manager As IDesignerSerializationManager, codeObject As Object) As Object
  19.     Return GetNormalSerializer(manager, codeObject).Deserialize(manager, codeObject)
  20.   End Function
  21.  
  22.   Public Overrides Function Serialize(manager As IDesignerSerializationManager, value As Object) As Object
  23.     Dim componentSerialiser = DirectCast(manager.GetSerializer(GetType(Component), GetType(CodeDomSerializer)), CodeDomSerializer)
  24.     Dim fieldInfo = componentSerialiser.GetType().GetField("_containerConstructor", BindingFlags.GetField Or BindingFlags.NonPublic Or BindingFlags.Instance)
  25.  
  26.     If fieldInfo IsNot Nothing Then
  27.       Dim values As Type() = CType(fieldInfo.GetValue(componentSerialiser), Type())
  28.       If values IsNot Nothing And values.Count > 0 Then
  29.         Dim reflectContType = CodeDomSerializerBase.GetReflectionTypeFromTypeHelper(manager, GetType(IContainer))
  30.         Dim cachedType = values(0)
  31.  
  32.         If Not reflectContType.Equals(cachedType) Then
  33.           fieldInfo.SetValue(componentSerialiser, Nothing)
  34.           Throw New Exception(
  35.             String.Format("Visual Studio was about to save this object without a component collection.{0}" +
  36.                           "I've fixed the problem and you should be able to save this object now.", vbCrLf))
  37.         End If
  38.       End If
  39.     End If
  40.  
  41.     Return GetNormalSerializer(manager, value).Serialize(manager, value)
  42.   End Function
  43. End Class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top