您可以使用下面的代碼來創建映射,但它不是一對一的映射。減少這一個對一個將不得不對你的最終實現細節:
var mapping = (from XmlTypeCode cc in Enum.GetValues(typeof(XmlTypeCode))
let xt = XmlSchemaType.GetBuiltInSimpleType(cc)
where xt != null
group cc by xt.Datatype.ValueType into gg
select new { Type = gg.Key, XmlTypeCodes = gg.ToArray() })
.ToDictionary(m => m.Type, m => m.XmlTypeCodes);
輸出示例:
System.Boolean => Boolean
System.Byte => UnsignedByte
System.Byte[] => HexBinary,Base64Binary
System.DateTime => DateTime,Time,Date,GYearMonth,GYear,GMonthDay,GDay,GMonth
System.Decimal => Decimal,Integer,NonPositiveInteger,NegativeInteger,NonNegative
Integer,PositiveInteger
...
一個體面的方法來解決一到一個問題是將代碼表中的第一個條目用於每種類型,但String
。這也可能不適用於較新的 BCL類型,但可能應該繼續。這將是MS重新排列XmlTypeCode
枚舉重大更改,但這並不是說這是傻瓜證明:
// same as above except the ToDictionary
.ToDictionary(
m => m.Type,
m => m.Type != typeof(string) ? m.XmlTypeCodes.First() : XmlTypeCode.String);
這正是我的想法!並且也感謝您獲得一對一映射的想法;我不介意硬編碼字符串的例外。 – ladenedge