Search code examples
c#.netclassproperties

Getting all properties, their data types, values and names from specified non-static class (C#)


We have a test class:

class Animal
{
    public string AnimalName { get; set; }
    public string AnimalType { get; set; }
    public int AnimalAgeInMonths { get; set; }
}

and an instance to the class:

Animal animal = new Animal();
animal.AnimalName = null; // No name yet
animal.AnimalType = "Sheep";
animal.AnimalAgeInMonths = 7;

So I need a method that returns System.Collections.Generic.List<(string[], string[], string[])>. First string array represents names of all properties (i.e. AnimalName, AnimalType, etc), second array - values (i.e. null, Sheep, etc), and third array - data type, for example:

  • System.Int32 for int
  • System.Single for float
  • System.String for string
  • etc

(these can be generated with the typeof keyword).

So overall, I need a method like this:

public List<(string[], string[], string[])> GetClassPropertyInfo(object Class)

which can be called like this:

GetClassPropertyInfo(animal);

Best regards,
-winscripter.


Solution

  • The following will get the name, type and value of each property (as strings) - you may use this as a starting point. You can then store the name, type and value in any structure you need and return it. Hope that helps.

    using System;
    using System.Collections;
    using System.Reflection;
    
    class Animal
    {
        public string AnimalName { get; set; }
        public string AnimalType { get; set; }
        public int AnimalAgeInMonths { get; set; }
        public int[] array { get; set; } 
    }
    
    class Program
    {
        static void Main()
        {
    
            Animal animal = new Animal();
            animal.AnimalName = null; // No name yet
            animal.AnimalType = "Sheep";
            animal.AnimalAgeInMonths = 7;
            animal.array = new int[] { 1, 3, 5, 7, 9 };
            
            class_info( animal );
                
        }
        
        static void class_info(object Class)
        {
            String val;
            
            foreach (PropertyInfo propertyInfo in Class.GetType().GetProperties())
            {
                // Property Name ...
                Console.WriteLine( propertyInfo.Name );
    
                // Property type (as string) ...
                Console.WriteLine( propertyInfo.PropertyType.ToString() );
    
                // Property value (as string) ...
                if ( propertyInfo.GetValue(Class, null) is null ) 
                {
                    val = "null";
                    Console.WriteLine( val );
                }
                else
                {
                    // Property is an array, show values (as strings) ...
                    if (propertyInfo.PropertyType.IsArray)
                    {
    
                        IEnumerable array = propertyInfo.GetValue( Class, null ) as IEnumerable;
    
                        if ( array is null )
                        {
                            Console.WriteLine( "Array is null" );
                        }
                        else
                        {
                            Console.WriteLine("Array values");
                            Console.WriteLine("------------");
    
                            foreach (var element in array)
                                Console.WriteLine(element.ToString());
                        }
    
                    }
                    else
                    {
                       val = propertyInfo.GetValue(Class, null).ToString();
                       Console.WriteLine( val );
                    }
                }
    
            }  
            
        }
        
    }