Me gustaría ver si un objeto es un builtin data type en C#¿Hay una función para verificar si un objeto es un tipo de datos incorporado?
No quiero consultar con todos ellos si es posible.
Es decir, que No desee hacer esto:
Object foo = 3;
Type type_of_foo = foo.GetType();
if (type_of_foo == typeof(string))
{
...
}
else if (type_of_foo == typeof(int))
{
...
}
...
actualización
Estoy intentando crear un recursiva PropertyDescriptorCollection donde los tipos PropertyDescriptor podrían no ser los valores de orden interna. Así que quería hacer algo como esto (nota: esto no funciona todavía, pero estoy trabajando en ello):
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);
}
Quería crear recursivamente una PropertyDescriptorCollection y necesitaba verificar si el tipo está incorporado o no. Quería crear una nueva colección si una de las propiedades no era un tipo incorporado. Añadiré lo que estoy tratando de hacer en la pregunta, tal vez eso ayude – SwDevMan81
¿Pero por qué esa decisión se basaría en la especificación C#?¿Por qué querrías tratar Decimal de una manera, pero DateTime o Guid de una manera diferente? –
Correcto, no debería ser, eso fue un descuido de mi parte. System.ValueType también se debe verificar. – SwDevMan81