gpt4 book ai didi

c# - 检查类型是否为 CLR 对象

转载 作者:行者123 更新时间:2023-12-02 05:11:58 25 4
gpt4 key购买 nike

我正在尝试序列化一个对象,想知道某个类型是否可以被 XmlReader.ReadElementContentAsObject()ReadElementContentAs() 使用。

我可以询问一个类型是否是 CLR 类型,以便我知道我可以将它传递给这些方法吗?

if(myType.IsCLRType) // how can I find this property?
myValue = _cReader.ReadElementContentAsObject();

最佳答案

I think I'm looking for this list: http://msdn.microsoft.com/en-us/library/xa669bew.aspx

您可能可以使用 Type.GetTypeCode(type) 完成大部分操作,但坦率地说,我希望您最好的选择更简单:

static readonly HashSet<Type> supportedTypes = new HashSet<Type>(
new[] { typeof(bool), typeof(string), typeof(Uri), typeof(byte[]), ... });

并检查 supportedTypes.Contains(yourType)

没有神奇的预定义列表可以完全您想到的“此列表”。例如,TypeCode 没有注明 byte[]Uri

关于c# - 检查类型是否为 CLR 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15291789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com