我正在为一个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;
}
}发布于 2013-09-18 15:14:14
.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
}https://stackoverflow.com/questions/18852639
复制相似问题