簡體   English   中英

System.Type到XmlSchema內置類型

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

我正在向XmlSchema寫一些類屬性,此刻我想在<xs:element type="xs:string">寫類型。

是否有一些映射類,所以我不必編寫自己的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;
    }
}

.NET框架沒有映射類或函數來執行所需的操作。

我建議創建一個映射字典,而不是使用一個大開關:

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