- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
是的,我知道非常通用的标题,但我现在要解释一下。
鉴于下面的类定义,有人可以解释为什么我在调用 Serialize() 方法时会收到此异常。
{“反射(reflect)类型 'FNM.MISMO_3_0.FOREIGN_OBJECT' 时出现错误。”
{“选择标识符‘ItemsElementName’的类型与‘Items’的类型不一致。请使用 System.Collections.Generic.List 数组`1[[MISMO_3_0.ItemsChoiceType, MISMO_3_0, Version=1.0.0.0, Culture=中性,PublicKeyToken=null]]。"
谢谢,斯蒂芬
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.mismo.org/residential/2009/schemas")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.mismo.org/residential/2009/schemas", IsNullable = true)]
public partial class FOREIGN_OBJECT
{
private List<object> itemsField;
private List<ItemsChoiceType> itemsElementNameField;
private MISMOresourceLink typeField;
private bool typeFieldSpecified;
private string roleField;
private string titleField;
private string labelField;
private string sequenceNumberField;
private static System.Xml.Serialization.XmlSerializer serializer;
public FOREIGN_OBJECT()
{
this.itemsElementNameField = new List<ItemsChoiceType>();
this.itemsField = new List<object>();
}
[System.Xml.Serialization.XmlElementAttribute("CharacterEncodingSetType", typeof(CharacterEncodingSetEnum), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("CharacterEncodingSetTypeOtherDescription", typeof(MISMOString), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("EXTENSION", typeof(EXTENSION), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("EmbeddedContentXML", typeof(MISMOXML), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("MIMETypeIdentifier", typeof(MISMOIdentifier), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("ObjectCreatedDatetime", typeof(MISMODatetime), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("ObjectDescription", typeof(MISMOString), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("ObjectEncodingType", typeof(ObjectEncodingEnum), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("ObjectEncodingTypeOtherDescription", typeof(MISMOString), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("ObjectName", typeof(MISMOString), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("ObjectURI", typeof(string), DataType = "anyURI", Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("OriginalCreatorDigestValue", typeof(MISMOValue), Order = 0)]
[System.Xml.Serialization.XmlElementAttribute("REFERENCE", typeof(REFERENCE), Order = 0)]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public List<object> Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("ItemsElementName", Order = 1)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public List<ItemsChoiceType> ItemsElementName
{
get
{
return this.itemsElementNameField;
}
set
{
this.itemsElementNameField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/1999/xlink")]
public MISMOresourceLink type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool typeSpecified
{
get
{
return this.typeFieldSpecified;
}
set
{
this.typeFieldSpecified = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/1999/xlink")]
public string role
{
get
{
return this.roleField;
}
set
{
this.roleField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/1999/xlink")]
public string title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/1999/xlink", DataType = "NMTOKEN")]
public string label
{
get
{
return this.labelField;
}
set
{
this.labelField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute(DataType = "integer")]
public string SequenceNumber
{
get
{
return this.sequenceNumberField;
}
set
{
this.sequenceNumberField = value;
}
}
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(FOREIGN_OBJECT));
}
return serializer;
}
}
#region Serialize/Deserialize
/// <summary>
/// Serializes current FOREIGN_OBJECT object into an XML document
/// </summary>
/// <returns>string XML value</returns>
public virtual string Serialize(System.Text.Encoding encoding)
{
System.IO.StreamReader streamReader = null;
System.IO.MemoryStream memoryStream = null;
try
{
memoryStream = new System.IO.MemoryStream();
System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings();
xmlWriterSettings.Encoding = encoding;
System.Xml.XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
Serializer.Serialize(xmlWriter, this);
memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
streamReader = new System.IO.StreamReader(memoryStream);
return streamReader.ReadToEnd();
}
finally
{
if ((streamReader != null))
{
streamReader.Dispose();
}
if ((memoryStream != null))
{
memoryStream.Dispose();
}
}
}
public virtual string Serialize()
{
return Serialize(Encoding.UTF8);
}
/// <summary>
/// Deserializes workflow markup into an FOREIGN_OBJECT object
/// </summary>
/// <param name="xml">string workflow markup to deserialize</param>
/// <param name="obj">Output FOREIGN_OBJECT object</param>
/// <param name="exception">output Exception value if deserialize failed</param>
/// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
public static bool Deserialize(string xml, out FOREIGN_OBJECT obj, out System.Exception exception)
{
exception = null;
obj = default(FOREIGN_OBJECT);
try
{
obj = Deserialize(xml);
return true;
}
catch (System.Exception ex)
{
exception = ex;
return false;
}
}
public static bool Deserialize(string xml, out FOREIGN_OBJECT obj)
{
System.Exception exception = null;
return Deserialize(xml, out obj, out exception);
}
public static FOREIGN_OBJECT Deserialize(string xml)
{
System.IO.StringReader stringReader = null;
try
{
stringReader = new System.IO.StringReader(xml);
return ((FOREIGN_OBJECT)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
}
finally
{
if ((stringReader != null))
{
stringReader.Dispose();
}
}
}
/// <summary>
/// Serializes current FOREIGN_OBJECT object into file
/// </summary>
/// <param name="fileName">full path of outupt xml file</param>
/// <param name="exception">output Exception value if failed</param>
/// <returns>true if can serialize and save into file; otherwise, false</returns>
public virtual bool SaveToFile(string fileName, System.Text.Encoding encoding, out System.Exception exception)
{
exception = null;
try
{
SaveToFile(fileName, encoding);
return true;
}
catch (System.Exception e)
{
exception = e;
return false;
}
}
public virtual bool SaveToFile(string fileName, out System.Exception exception)
{
return SaveToFile(fileName, Encoding.UTF8, out exception);
}
public virtual void SaveToFile(string fileName)
{
SaveToFile(fileName, Encoding.UTF8);
}
public virtual void SaveToFile(string fileName, System.Text.Encoding encoding)
{
System.IO.StreamWriter streamWriter = null;
try
{
string xmlString = Serialize(encoding);
streamWriter = new System.IO.StreamWriter(fileName, false, Encoding.UTF8);
streamWriter.WriteLine(xmlString);
streamWriter.Close();
}
finally
{
if ((streamWriter != null))
{
streamWriter.Dispose();
}
}
}
/// <summary>
/// Deserializes xml markup from file into an FOREIGN_OBJECT object
/// </summary>
/// <param name="fileName">string xml file to load and deserialize</param>
/// <param name="obj">Output FOREIGN_OBJECT object</param>
/// <param name="exception">output Exception value if deserialize failed</param>
/// <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns>
public static bool LoadFromFile(string fileName, System.Text.Encoding encoding, out FOREIGN_OBJECT obj, out System.Exception exception)
{
exception = null;
obj = default(FOREIGN_OBJECT);
try
{
obj = LoadFromFile(fileName, encoding);
return true;
}
catch (System.Exception ex)
{
exception = ex;
return false;
}
}
public static bool LoadFromFile(string fileName, out FOREIGN_OBJECT obj, out System.Exception exception)
{
return LoadFromFile(fileName, Encoding.UTF8, out obj, out exception);
}
public static bool LoadFromFile(string fileName, out FOREIGN_OBJECT obj)
{
System.Exception exception = null;
return LoadFromFile(fileName, out obj, out exception);
}
public static FOREIGN_OBJECT LoadFromFile(string fileName)
{
return LoadFromFile(fileName, Encoding.UTF8);
}
public static FOREIGN_OBJECT LoadFromFile(string fileName, System.Text.Encoding encoding)
{
System.IO.FileStream file = null;
System.IO.StreamReader sr = null;
try
{
file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read);
sr = new System.IO.StreamReader(file, encoding);
string xmlString = sr.ReadToEnd();
sr.Close();
file.Close();
return Deserialize(xmlString);
}
finally
{
if ((file != null))
{
file.Dispose();
}
if ((sr != null))
{
sr.Dispose();
}
}
}
#endregion
#region Clone method
/// <summary>
/// Create a clone of this FOREIGN_OBJECT object
/// </summary>
public virtual FOREIGN_OBJECT Clone()
{
return ((FOREIGN_OBJECT)(this.MemberwiseClone()));
}
#endregion
}
最佳答案
XmlChoiceIdentifierAttribute
中的多个元素仅在 ChoiceIdentifier 是数组而不是通用列表时才受支持。
[System.Xml.Serialization.XmlElementAttribute("ItemsElementName", Order = 1)]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemsChoiceType[] ItemsElementName
{
// ...
}
该错误令人困惑,因为它假设 Items 是类数组(列表)并且您选择的标识符不是数组,您在标识符选择的类型之前错过了数组限定符。但事实并非如此,因此该建议完全具有误导性。
注意:从理论上看代码(来自微软源服务器)Items 不需要是数组...实际上临时序列化类的生成完全爆炸,因为 List.Length 不是公开(已明确实现)
关于c# - .NET XML 序列化错误(反射(reflect)类型错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6866301/
我有: func NewMethodDescriptor(typ interface{}) *MethodDescriptor { reflectedMethod := reflect.Val
我需要确定地检查 reflect.Type 是否是一个错误。 错误没有反射(reflect)类型。在 go reflect 中检查类型错误的正式/惯用方式是什么? Go Playground Full
根据 reflect 文档 reflect.Value.MapIndex() 应返回一个 reflect.Value,它表示存储在 map 特定键处的数据的值.所以我的理解是以下两个表达式应该是相同的
与 reflect pkg 有点混淆 所有示例都使用 reflect.NewValue() 来获取 var 的 reflect.Value,但是 func NewValue 未记录在 http://g
在计算机语言的上下文中,我从未找到关于反射的词源的明确解释,所以我想在这里澄清一下。 “Reflection”源于拉丁语,有以下definitions : bend back turn back tu
我写了一个漂亮的函数,它可以接受 system.object ,反射(reflect)其属性并将对象序列化为 JSON 字符串。它看起来像这样: public class JSONSerializer
我正在尝试创建一个函数 import Language.Reflection foo : Type -> TT 我尝试使用reflect 策略: foo = proof { intro t
最近我和一位同事谈论 C++,感叹没有办法获取带有类字段名称的字符串并提取具有该名称的字段;换句话说,它缺乏反射(reflection)。他困惑地看着我,并问什么时候有人需要做这样的事情。 除了“嘿,
我正在考虑允许模块与属性文件中的类一起使用的想法;像 availableModules.properties Contact=org.addressbook.ContactMain Business=
这个问题特别与为具有大量字段的对象覆盖 equals() 方法有关。首先,让我说这个大对象不能在不违反 OO 原则的情况下分解成多个组件,所以告诉我“没有类应该有超过 x 个字段”无济于事。 继续前进
例子 router.Get(path, handler) // works fine methodStr = "Get" router.methodStr(path, handler) // e
我一直坚持使用反射库的问题。由于很多推荐,我决定使用它,但我只是在学习,有些部分并不是很容易.. 我有这部分代码: func countDataByName(sourceName string, s
我有一个包含一些 url 参数的特定结构,我想使用 reflect 构建一个 url 参数字符串以遍历结构字段,这样我就不会关心结构真正包含什么。 假设我有一个这样的结构: type Student
我正在尝试从 reflect.Value 中检索字符串值, 我希望 value.String()成为okok但我得到了相反。 我错过了什么吗? package main import ( "f
为了避免创建 org.reflections.Reflections 类的多个实例,我只想创建一个并根据需要重用。有谁知道这个类是否是线程安全的? 如果它不是线程安全的,我知道我可以使用 Java 的
我最近对引用、具体化和反射(reflection)感到困惑。有人可以很好地解释他们的关系和差异(如果有的话)吗? 最佳答案 引用 这可能是最简单的一个。考虑一下当您在 REPL 中键入以下内容时会发生
less main.go输出: ``` package main import ( "reflect" "net/url" "fmt" ) type User struct {
我在 golang 中使用 gorm 包 ( https://github.com/jinzhu/gorm ) 作为我的数据库库。我有很多类(数据库表),如“酒店”或“套餐”。复制代码不是好的编程习惯
我有代码 var t reflect.Type = LaunchController(route.controller) // create controller ptr . var
是否有可能以及如何在不从类型创建对象并调用它的情况下获取类型的 reflect.Type reflect.TypeOf(obj) Java 中的内容是:MyType.class 最佳答案 您可以使用以
我是一名优秀的程序员,十分优秀!