I am trying to use reflection, and iterating through all Class members. How to tell if a member is not another class or regular system item(int, boolean, Datetime, string,etc)?
assembly.GetTypes().GetProperties.
PropertyType.BaseType
String is showing as Value Object, along with Manufacturer class. Trying to detect all non-classes regular system data types. How can this be done with reflection properties?
public class Product
{
public int ProductId { get; set; }
public string ProductDescription{ get; set; }
public bool? Active { get; set; }
public float SalesPrice { get;set;}
public DateTime? EffectiveStartDate { get; set; }
public Manufacturer Manufacturer { get; set; }
public Department Department {get; set; }
public virtual ICollection<ProductHistory> {get; set; }
}
Using Net Core 2,
About .NET Core reflection, you must know that in old versions you need to use
Type.GetTypeInfo(). This is no longer true for .NET Core 2.0 and newer. However if you are multi-targeting I suggest to keep usingType.GetTypeInfo().To identify classes, I suggest to check
Type.IsValueType. Or for old .NET Core platforms useTypeInfo.IsValueType.Classes and delegates are not value types. While structs and enums are.
Of course, you can also use
Type.IsClassorTypeInfo.IsClass. Which also return true for delegates.To distinguish delegates you can use
typeof(Delegate).IsAssignableFrom(type)ortypeof(Delegate).GetTypeInfo().IsAssignableFrom(type).By the way,
stringis a class.We should also consider interface types such as
ICollection<T>. aninterfacecould be implemented by a class or a struct.The interface type is neither, it is an interface. You can use
Type.IsinterfaceorTypeInfo.IsInterfaceto check for it.You might check other properties such as
IsArray,IsPrimitive,IsEnum, etc.However, I suspect, what you want isn't to distinguish classes, but to distinguish your types. In that case you have a couple options:
You could check the namesapce (
Type.Namespace). You are not placing your types inSystem, are you? Well, check that.Alternatively you can check the
AssemblyfromTypeInfo.AssemblyorType.Assemblyin modern .NET Core. If you have a type of which you know it is in your assembly, you can get the assembly from it, and use it to compare the assembly of other types.Of course, you can still check
IsValueTypeorIsClassif you really do not want anystructorenum.