'X []'类型的对象无法转换为'X []'

时间:2017-10-24 16:01:38

标签: visual-studio-2017 windows-forms-designer

我有一个像这样的属性的控件:

public BindingList<I> X { get; private set; } = new SortableBindingList<I>();

我是一个界面。它构建良好,运行正常,但是当我将此控件添加到表单时(如果表单在同一个程序集中无关紧要),然后尝试在visual studio(2017)中打开该表单,表单是未显示,出现以下错误消息:

  

'I []'类型的对象无法转换为'I []'类型。

使用以下调用堆栈:

  

at System.RuntimeType.TryChangeType(Object value,Binder binder,   CultureInfo culture,Boolean needsSpecialCast)at   System.RuntimeType.CheckValue(对象值,Binder活页夹,CultureInfo   culture,BindingFlags invokeAttr)at   System.Reflection.RtFieldInfo.UnsafeSetValue(Object obj,Object value,   BindingFlags invokeAttr,Binder binder,CultureInfo culture)at   System.Runtime.Serialization.FormatterServices.SerializationSetValue(的MemberInfo   fi,对象目标,对象值)at   System.Runtime.Serialization.ObjectManager.CompleteObject(的ObjectHolder   holder,Boolean bObjectFullyComplete)at   System.Runtime.Serialization.ObjectManager.DoNewlyRegisteredObjectFixups(的ObjectHolder   持有人)   System.Runtime.Serialization.ObjectManager.RegisterObject(Object obj,   Int64 objectID,SerializationInfo info,Int64 idOfContainingObj,   MemberInfo成员,Int32 [] arrayIndex)at   System.Runtime.Serialization.Formatters.Binary.ObjectReader.RegisterObject(对象   obj,ParseRecord pr,ParseRecord objectPr,Boolean bIsString)at   System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObjectEnd(ParseRecord   pr)at   System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord   pr)at   System.Runtime.Serialization.Formatters.Binary .__ BinaryParser.Run()at   System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler   handler,__BinaryParser serParser,Boolean fCheck,Boolean   isCrossAppDomain,IMethodCallMessage methodCallMessage)at   System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(流   serializationStream,HeaderHandler handler,Boolean fCheck,Boolean   isCrossAppDomain,IMethodCallMessage methodCallMessage)at   System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(流   serializationStream)at   System.Resources.ResXDataNode.GenerateObjectFromDataNodeInfo(DataNodeInfo   dataNodeInfo,ITypeResolutionService typeResolver)at   System.Resources.ResXDataNode.GetValue(ITypeResolutionService   typeResolver)at   System.Resources.ResXResourceReader.ParseDataNode(XmlTextReader的   reader,Boolean isMetaData)at   System.Resources.ResXResourceReader.ParseXml(XmlTextReader reader)

我尝试添加

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

分别按https://stackoverflow.com/a/29720/68936https://stackoverflow.com/a/29717/68936;

我也尝试用

替换getter-setter
private BindingList<I> myX = new SortableBindingList<I>();
public BindingList<I> myX { get { return myX; } }

根据https://stackoverflow.com/a/16657926/68936

无济于事。我已经尝试关闭所有打开的文档,关闭解决方案,重新启动visual studio,重建两个程序集,似乎没有解决这个问题。

1 个答案:

答案 0 :(得分:0)

我能够使用

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public BindingList<I> myX { get; private set; } = new SortableBindingList<I>();

通过编辑包含表单的 .resx文件(而不是控件)并删除看起来像这样的xml元素:

  <data name="controlName.X" mimetype="application/x-microsoft.net.object.binary.base64">
    <value>
        AAEAAAD/////AQAAAAAAAAAMAgAAAJsBSW50ZXJmYWNlc0ludmVzdG1lbnRzLCBWZXJzaW9uPTEuMC4w
        LjAsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbF1dLCBtc2NvcmxpYiwgVmVyc2lv
        bj00LjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkM
        AwAAAExJbnRlcmZhY2VzSW52ZXN0bWVudHMsIFZlcnNpb249MS4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFs
        LCBQdWJsaWNLZXlUb2tlbj1udWxsBQEAAABOU3lzdGVtLkNvbGxlY3Rpb25zLkdlbmVyaWMuTGlzdGAx
        W1tLYW1hVHJlbmRhLkludGVyZmFjZXMuSW52ZXN0bWVudHMuSVBvc2l0aW9uAwAAAAZfaXRlbXMFX3Np
        emUIX3ZlcnNpb24EAAAtS2FtYVRyZW5kYS5JbnRlcmZhY2VzLkludmVzdG1lbnRzLklQb3NpdGlvbltd
        AwAAAAgIAgAAAAkEAAAAAAAAAAAAAAAHBAAAAAABAAAAAAAAAAQrS2FtYVRyZW5kYS5JbnRlcmZhY2Vz
        LkludmVzdG1lbnRzLklQb3NpdGlvbgMAAAAL
</value>
  </data>

似乎只是添加DesignerSerializationVisibility.Hidden不足以从现有的.resx文件中删除现有数据节点。