We have a (test) class like this:
class Animal
{
public string AnimalName { get; set; }
public string AnimalType { get; set; }
public int AnimalAgeInMonths { get; set; }
}
... and object (or reference) to 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
forint
System.Single
forfloat
System.String
forstring
- 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);
Just telling, I did search around 10 minutes to find the best matching answer, but found nothing for what I am looking for (for example, I would find an answer, but not how to get data type as well, or the other way around), and also attempted creating this code manually over 3 times, of course with the help of System.Reflection
namespace, but it didn't work, Similar questions also didn't have any similar questions, and I eventually gave up.
I tried this (4th attempt):
using System;
using System.Reflection;
using System.Collections.Generic;
// FYI: Coded on an online compiler to save myself a lot of time
namespace TestAttempt
{
public class Animal
{
public string AnimalName { get; set; }
public string AnimalType { get; set; }
public int AnimalAgeInMonths { get; set; }
}
public class Program
{
static void Main()
{
Animal a = new Animal();
a.AnimalName = "";
a.AnimalType = "Sheep";
a.AnimalAgeInMonths = 7;
List<(string[], string[], string[])> types = new Program().GetClassPropertyInfo(a, typeof(Animal));
foreach (var Iteration in types)
{
for (int i = 0; i < Iteration.Item1.Length; i++)
{
Console.WriteLine($"{Iteration.Item3[i]} {Iteration.Item1[i]} = {Iteration.Item2[i]}");
}
}
}
private List<(string[], string[], string[])> GetClassPropertyInfo(object Class, Type _Class)
{
var @return = new List<(string[], string[], string[])>();
List<string> propertyNames = new List<string>();
List<string> propertyTypes = new List<string>();
List<string> propertyValues = new List<string>();
// https://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class
foreach (var prop in Class.GetType().GetProperties())
{
propertyNames.Add(Convert.ToString(prop.Name));
propertyValues.Add(Convert.ToString(prop.GetValue(Class, null)));
}
// System.Reflection.FieldInfo
// See: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.fieldinfo?view=net-7.0
FieldInfo[] fi = _Class.GetFields(BindingFlags.NonPublic | BindingFlags.Instance
| BindingFlags.Public);
for (int i = 0; i < fi.Length; i++)
propertyTypes.Add(Convert.ToString(fi[i].FieldType));
@return.Add((propertyNames.ToArray(), propertyValues.ToArray(), propertyTypes.ToArray()));
return @return;
}
}
}
It works, but not when an array is a property.
Best regards,
-winscripter.