I would like to see if an object is a builtin data type in C#
I don't want to check against all of them if possible.
That is, I don't want to do this:
Object foo = 3;
Type type_of_foo = foo.GetType();
if (type_of_foo == typeof(string))
{
...
}
else if (type_of_foo == typeof(int))
{
...
}
...
Update
I'm trying to recursively create a PropertyDescriptorCollection where the PropertyDescriptor types might not be builtin values. So I wanted to do something like this (note: this doesn't work yet, but I'm working on it):
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);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…