Find Class Members which are Not Other Classes in C# Net Core with Reflection

304 Views Asked by At

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,

3

There are 3 best solutions below

3
Theraot On

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 using Type.GetTypeInfo().


To identify classes, I suggest to check Type.IsValueType. Or for old .NET Core platforms use TypeInfo.IsValueType.

Classes and delegates are not value types. While structs and enums are.

Of course, you can also use Type.IsClass or TypeInfo.IsClass. Which also return true for delegates.

To distinguish delegates you can use typeof(Delegate).IsAssignableFrom(type) or typeof(Delegate).GetTypeInfo().IsAssignableFrom(type).

By the way, string is a class.


We should also consider interface types such as ICollection<T>. an interface could be implemented by a class or a struct.

The interface type is neither, it is an interface. You can use Type.Isinterface or TypeInfo.IsInterface to 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 in System, are you? Well, check that.

Alternatively you can check the Assembly from TypeInfo.Assembly or Type.Assembly in 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 IsValueType or IsClass if you really do not want any struct or enum.

0
Eriawan Kusumawardhono On

Ok, based on your last comment reply, I think I understand your question now.

To get all properties that are categorized as non .NET types (a.k.a. non .NET Base Class Library (BCL) types), you can simply filter the result of assembly.GetTypes().GetProperties().

A quick sample is by using LINQ:

var proplist = assembly.GetTypes().GetProperties();
var proplistNonBCL = proplist.Where(prop => !prop.PropertyType.FullName.StartsWith("System.").ToList();

So basically it checks for the fullname of the underlying types. If the types' full name starts with "System.", then the types are not part of .NET BCL at all.

2
Donnie Imafidon On

You might want to try this.

 // object here referres to any object you are inspecting
        var props = Object.GetType().GetProperties();
        foreach (var p in props)
        {
            if (p.PropertyType.IsValueType)
            {
                // This is not a class
            }
        }