简体   繁体   English

System.Type到XmlSchema内置类型

[英]System.Type to XmlSchema built-in type

I am writing some class properties to an XmlSchema and at the moment I want to write the type in the <xs:element type="xs:string"> . 我正在向XmlSchema写一些类属性,此刻我想在<xs:element type="xs:string">写类型。

Is there some mapping class so I don't have to write my own switch-case ? 是否有一些映射类,所以我不必编写自己的switch-case

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;
    }
}

The .NET framework does not have mapping classes or functions to do what you need. .NET框架没有映射类或函数来执行所需的操作。

I'd suggest to create a mapping dictionary instead than using a big switch: 我建议创建一个映射字典,而不是使用一个大开关:

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
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM