首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >System.Type到XmlSchema内置类型

System.Type到XmlSchema内置类型
EN

Stack Overflow用户
提问于 2013-09-17 14:28:38
回答 1查看 94关注 0票数 2

我正在为一个XmlSchema编写一些类属性,目前我想在<xs:element type="xs:string">中编写这个类型。

有没有映射类,这样我就不必编写自己的switch-case

代码语言:javascript
复制
public class Foo
{
    public string Bar { get; set; }
}

public void WriteProperty()
{
    // get the property that is a string
    PropertyInfo barProperty;
    XmlSchemaElement barElement;

    // I don't want this huge switch case for all basic properties.
    switch(barProperty.PropertyType.FullName)
    {
        case "System.String":
            barElement.SchemaTypeName = new QualifiedName("xs:string");
            break;
        // also for int, and bool, and long....


        default:
            //do other stuff with types that are not default types
            break;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-18 15:14:14

.NET框架没有映射类或函数来满足您的需要。

我建议创建一个映射字典,而不是使用一个大开关:

代码语言:javascript
复制
static Dictionary<string, string> TypeMap = new Dictionary<string, string>() {
  { "System.String", "xs:string" },
  { "System.Int32", "xs:int" },
  . . . 
};

. . . 

  string schemaTypeName;
  if (TypeMap.TryGetValue(barProperty.PropertyType.FullName, out schemaTypeName)) {
    barElement.SchemaTypeName = new QualifiedName("xs:string");
  } else {
    //do other stuff with types that are not default types
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18852639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档