c# - typeof() 时 无法将类型 'System.Type' 隐式转换为 'string'

标签 c# typeof

我正在执行一些 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 的任何指示吗?

最佳答案

根据documentationDataType 属性用于指定 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/

相关文章:

c# - 如何使用传递给 C# 中当前方法的实现来实例化接口(interface)对象?

C# VS 2005 : How to get a class's public member list during the runtime?

c# - 如何从 Fluent Api 检索实体配置

json - 此 JSON 对象 {yourVariable : "nothing yet"} 中的键的类型是什么

c# - P/调用 const char 指针和 int 引用

c# - 复杂的 Linq C# 查询

javascript - undefined variable 的 typeof 用法

JavaScript:检查对象字段是否未定义而不检查对象是否未定义

c# - 将 json 动态反序列化为传入的任何对象。 c#

c# - .net 反汇编器/反编译器