Detecting if class property is a reference type

39,598

Solution 1

Well, it sounds like you may be trying to detect the difference between a value type and a reference type. You can find that out using Type.IsValueType... but be aware that value types can easily have properties too. (Think about DateTime for example.) Also, some types which you may want to regard as "not objects" are reference types - string being a prime example.

Another option would be to use Type.IsPrimitive - is that what you're looking for? If so, you should be aware that decimal, DateTime and string are not primitive types.

If you can describe exactly what makes a type an "object" in your way of thinking (or rather, in whatever way makes a semantic difference in what you're trying to do with your type). I suspect you don't currently have a very clear set of criteria - coming up with those criteria may well clarify other aspects of your current task, too.

Solution 2

You can use a little reflection to see if a property is a value type or a class type. Class is probably what you mean by "object". All types in .NET derive from the object type.

Client.GetType().IsClass

Or you can loop through all properties and see which are compound

foreach(var p in ProgrammeClient.GetType().GetProperties())
{
     if(p.PropertyType.IsClass) Console.WriteLine("Found a class");
}

Solution 3

Check if the type is a string and check if it is a class.

        public static bool IsNonStringClass(this Type type)
        {
            if (type == null || type == typeof(string))
                return false;
            return typeof(Type).IsClass;
        }

Solution 4

All properties in your example return objects, as everything is an object in .NET; int and bool are objects. If you mean a reference type, as opposed to value types, then you can do the following:

foreach (PropertyInfo pi in typeof(Client).GetProperties()) {
    if (pi.PropertyType.IsClass) {
        // reference type
        // DoMyFunkyStuff
    }
}

Solution 5

You can enumerate the properties via Reflection, and check them:

bool ContainsOnlyValues() { 
    return typeof(ProgrammeClient).GetProperties().All(x => x.PropertyType.IsValueType);
}
Share:
39,598
4imble
Author by

4imble

Senior .NET / Full Stack Developer Contact: me(at)4imble.net

Updated on July 09, 2022

Comments

  • 4imble
    4imble almost 2 years

    Is it possible when looking at a class' properties to detect if any of them is a reference type.

    Take below as an example:

    public class Client
    {
       public int Id { get; set; }
       public string Name { get; set; }
    }
    
    public class ProgrammeClient
    {
        public int Id { get; set; }
        public bool IsActive { get; set; }
        public IClient Client { get; set; }
    }
    

    ProgrammeClient: -
    Id and IsActive are properties but Client is a reference type. Is there a way of detecting this?

    Many thanks, Kohan.

    Addendum

    The reason i ask is: I am using a mapper that checks types are the same before matching property names and copying the values. My hope is to detect classes and override the type matching and simply copy the classes properties if the THEY type match.

  • 4imble
    4imble over 13 years
    Okay this looks like it's not quite as simple a question as i had hoped. I will create a new question later tonight explaining why i want to detect this and what for. Many thanks.
  • nawfal
    nawfal almost 11 years
    You need p.PropertyType.IsClass in fact, thecoop's answer.
  • Bertie
    Bertie almost 7 years
    A string type will register as a class.
  • ʞᴉɯ
    ʞᴉɯ over 4 years
    for string IsClass = true
  • CularBytes
    CularBytes over 3 years
    Since this is the top answer and top searched by google, I think the most of us can all agree to find a solution to check if a property is a reference to another (own defined) class. What's the best way to check for that?
  • Jon Skeet
    Jon Skeet over 3 years
    @CularBytes: I see no indication that that's what the OP was actually asking for, so I don't think it would be appropriate to include it (or even discuss it in comments) here. I suggest you ask a new question instead.
  • Roger Hill
    Roger Hill over 3 years
    Yeah, this kinda fails if you put it into a recursive loop, any strings wreck it.