我正在执行一些 Xml 序列化,但遇到编译项错误。
有错误的代码是:
public class EPubBody
{
[XmlElement(ElementName = "Image", DataType = typeof(EPubImage))]
public object[] BodyItems;
}
错误出现在 typeof(EPubImage)
部分。错误是无法将类型“System.Type”隐式转换为“string”
。
EPubImage 类位于相同的命名空间中,如下所示:
public class EPubImage
{
[XmlAttribute("imagePath")]
public string ImagePath { get; set; }
}
我猜 typeof(EPubImage)
返回的是 System.Type
而不是 string
。有关如何确保 typeof 语句返回字符串而不是 System.Type 的任何指示吗?
最佳答案
根据documentation ,DataType
属性用于指定 XSD 数据类型,而不是 .NET 类型:
An XML Schema data type, as defined by the World Wide Web Consortium (www.w3.org) document named "XML Schema Part 2: Datatypes".
试试这个:
public class EPubBody
{
[XmlElement(ElementName = "Image")]
public EPubImage[] BodyItems;
}
关于c# - typeof() 时 无法将类型 'System.Type' 隐式转换为 'string',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17915943/