我想查看一个对象是否是C#中的内置数据类型。
如果可能的话,我不想检查所有这些类型。
也就是说,我不想这样做:
Object foo = 3;
Type type_of_foo = foo.GetType();
if (type_of_foo == typeof(string))
{
...
}
else if (type_of_foo == typeof(int))
{
...
}
...
更新
我正在尝试递归创建一个 PropertyDescriptorCollection,其中 PropertyDescriptor 类型可能不是内置值。因此,我想做类似于这样的事情(注意:这还没有实现,但我正在努力):
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection cols = base.GetProperties(attributes);
List<PropertyDescriptor> list_of_properties_desc = CreatePDList(cols);
return new PropertyDescriptorCollection(list_of_properties_desc.ToArray());
}
private List<PropertyDescriptor> CreatePDList(PropertyDescriptorCollection dpCollection)
{
List<PropertyDescriptor> list_of_properties_desc = new List<PropertyDescriptor>();
foreach (PropertyDescriptor pd in dpCollection)
{
if (IsBulitin(pd.PropertyType))
{
list_of_properties_desc.Add(pd);
}
else
{
list_of_properties_desc.AddRange(CreatePDList(pd.GetChildProperties()));
}
}
return list_of_properties_desc;
}
// This was the orginal posted answer to my above question
private bool IsBulitin(Type inType)
{
return inType.IsPrimitive || inType == typeof(string) || inType == typeof(object);
}
native int
(System.IntPtr
) 是一个 .NET 原始类型。 - Mehrdad Afshari