为什么使用反序列化方法正确构造的对象的失败测试是 Myclass ?或者作为 MyClass 演员?
why a object correctly construct with deserialize method have a failure test is Myclass ? or as MyClass cast?
我序列化父对象 class 并派生自它
namespace CyXml
{
[Serializable]
public class XmlInfo : IFormattable, ISerializable
{
public string Mnemonique { get; set; }
....
}
[Serializable]
public class XmlLabel: XmlInfo
{
public XmlLabel()
: base()
{
}
....
}
[Serializable]
public class XmlVoyant : XmlInfo
{
public XmlVoyant()
: base()
{
}
....
}
[Serializable]
public class XmlButton: XmlInfo
{
public XmlButton()
: base()
{
}
....
}
}
当作为对象类型的 ArrayList 的反序列化正确时,测试不起作用。
ArrayList XmlInfos = new ArrayList ();
public void ReadXmllInfo(int vue)
{
System.Xml.Serialization.XmlSerializer reader =
new System.Xml.Serialization.XmlSerializer(typeof(ArrayList),
new Type[]{
typeof(CyXml.XmlInfo),
typeof(CyXml.XmlLabel),
typeof(CyXml.XmlVoyant),
typeof(CyXml.XmlButton)});
string xml = String.Format(@"C:\XML\Outil{0}.xml", vue);
System.IO.StreamReader file = null;
try
{
file = new System.IO.StreamReader(xml);
XmlInfos = (ArrayList)reader.Deserialize(file);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
finally
{
if (file != null)
file.Close();
}
int TabIndex=this.Controls.Count;
foreach(var elmt in XmlInfos)
{
//MessageBox.Show(elmt.GetType().ToString());
Type serie=elmt.GetType();
XmlInfo elmt1 = new XmlInfo();
Type serie1 = elmt1.GetType();
elmt1 = (XmlInfo)elmt;
if (elmt is XmlInfo )
{
....
}
else if (elmt is XmlLabel)
{
....
}
else if (elmt is XmlVoyant)
{
....
}
else if (elmt is XmlButton)
{
....
}
}
}
我不可能将反序列化中的对象分配给父对象 class。
elmt1 = (XmlInfo)elmt;
System.InvalidCastException 异常未被用户代码处理 Message = [A] CyXml.XmlInfo 无法转换为 [B] CyXml.XmlInfo 。类型 A 来自上下文 'Default' 中位置 'C: \ TwinCAT \ TcApplication \ Plugins \ IHMTest.dll ' 中的“IHMTest,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”。类型 B 来自位置 'C: \ TwinCAT \ Plugins \ IHMTest.dll ' 的上下文“LoadFrom”中的“IHMTest,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”。来源 = IHMTest
此代码在程序中可以正常运行,但在 dll 中无法运行
有人有想法吗?
亲切
您正在尝试将一个 DLL 中声明的 XmlInfo
的实现转换为另一个 DLL 中声明的 XmlInfo
的 不同 实现的实例.
如果 a good, minimal, complete code example 无法可靠地重现问题,则无法确定解决此问题的最佳方法。但是很可能由于某种原因 copy/pasted 从一个 DLL 到另一个 DLL 的实现,可能是通过在两个 DLL 中实际编译相同的 .cs 文件,导致声明了两种不同的类型。
一般来说,解决这个问题的正确方法是只在一个地方声明类型,并让所有依赖于该类型的代码引用 one DLL已声明。
我序列化父对象 class 并派生自它
namespace CyXml
{
[Serializable]
public class XmlInfo : IFormattable, ISerializable
{
public string Mnemonique { get; set; }
....
}
[Serializable]
public class XmlLabel: XmlInfo
{
public XmlLabel()
: base()
{
}
....
}
[Serializable]
public class XmlVoyant : XmlInfo
{
public XmlVoyant()
: base()
{
}
....
}
[Serializable]
public class XmlButton: XmlInfo
{
public XmlButton()
: base()
{
}
....
}
}
当作为对象类型的 ArrayList 的反序列化正确时,测试不起作用。
ArrayList XmlInfos = new ArrayList ();
public void ReadXmllInfo(int vue)
{
System.Xml.Serialization.XmlSerializer reader =
new System.Xml.Serialization.XmlSerializer(typeof(ArrayList),
new Type[]{
typeof(CyXml.XmlInfo),
typeof(CyXml.XmlLabel),
typeof(CyXml.XmlVoyant),
typeof(CyXml.XmlButton)});
string xml = String.Format(@"C:\XML\Outil{0}.xml", vue);
System.IO.StreamReader file = null;
try
{
file = new System.IO.StreamReader(xml);
XmlInfos = (ArrayList)reader.Deserialize(file);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
finally
{
if (file != null)
file.Close();
}
int TabIndex=this.Controls.Count;
foreach(var elmt in XmlInfos)
{
//MessageBox.Show(elmt.GetType().ToString());
Type serie=elmt.GetType();
XmlInfo elmt1 = new XmlInfo();
Type serie1 = elmt1.GetType();
elmt1 = (XmlInfo)elmt;
if (elmt is XmlInfo )
{
....
}
else if (elmt is XmlLabel)
{
....
}
else if (elmt is XmlVoyant)
{
....
}
else if (elmt is XmlButton)
{
....
}
}
}
我不可能将反序列化中的对象分配给父对象 class。
elmt1 = (XmlInfo)elmt;
System.InvalidCastException 异常未被用户代码处理 Message = [A] CyXml.XmlInfo 无法转换为 [B] CyXml.XmlInfo 。类型 A 来自上下文 'Default' 中位置 'C: \ TwinCAT \ TcApplication \ Plugins \ IHMTest.dll ' 中的“IHMTest,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”。类型 B 来自位置 'C: \ TwinCAT \ Plugins \ IHMTest.dll ' 的上下文“LoadFrom”中的“IHMTest,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”。来源 = IHMTest
此代码在程序中可以正常运行,但在 dll 中无法运行
有人有想法吗?
亲切
您正在尝试将一个 DLL 中声明的 XmlInfo
的实现转换为另一个 DLL 中声明的 XmlInfo
的 不同 实现的实例.
如果 a good, minimal, complete code example 无法可靠地重现问题,则无法确定解决此问题的最佳方法。但是很可能由于某种原因 copy/pasted 从一个 DLL 到另一个 DLL 的实现,可能是通过在两个 DLL 中实际编译相同的 .cs 文件,导致声明了两种不同的类型。
一般来说,解决这个问题的正确方法是只在一个地方声明类型,并让所有依赖于该类型的代码引用 one DLL已声明。