MJSHARMA
MJSHARMA

Reputation: 53

Using reflection to get properties of an instance of a class at runtime C#

Hi here is what I am trying to do, I have a class (EventType) which can be dynamic and have different members/properties at different times.

class EventType
{
    int id{set;}
    string name{set;}
    DateTime date{set;}
    List<int> list{set;}
    Guid guid{set;}
}

In my main method I am passing an instance on this class to a to a function in a different class and trying reflection to get the properties of the instance but its not successful and giving me null value.

class Program
{
    static void Main(string[] args)
    {
        EventType event1 = new EventType();
        int rate = 100;
        DataGenerator.Generate<EventType>(event1, rate);
    }
    public static byte[] test(EventType newEvent)
    {
        return new byte[1];
    }
}



static class DataGenerator
{
    public static void Generate<T>(T input, int eventRate, Func<T, byte[]> serializer=null)
    {            
        Type t = input.GetType();
        PropertyInfo[] properties = t.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            Console.WriteLine(property.ToString());   
        }
        var bytes = serializer(input);
    }
}

Upvotes: 5

Views: 8836

Answers (4)

lex87
lex87

Reputation: 1326

class EventType
{
    int id { get; set; }
    string name { get; set; }
    DateTime date { get;  set; }
    List<int> list { get; set; }
    Guid guid { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        EventType event1 = new EventType();
        int rate = 100;
        DataGenerator.Generate<EventType>(event1, rate);
    }
    public static byte[] test(EventType newEvent)
    {
        return new byte[1];
    }
}



static class DataGenerator
{
    public static void Generate<T>(T input, int eventRate, Func<T, byte[]> serializer = null)
    {
        Type t = input.GetType();
        PropertyInfo[] properties = t.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
        foreach (PropertyInfo property in properties)
        {
            Console.WriteLine(property.ToString());
        }
        //var bytes = serializer(input);
    }
}

Upvotes: 1

T. Kiley
T. Kiley

Reputation: 2802

Type.GetProperties returns all public properties, in your class they are all private so they won't be returned. You can either make them public or use BindingFlags to search for private ones also, eg:

t.GetProperties(BindingFlags.NonPublic | BindingFlags.Instance);

Upvotes: 2

Dirk
Dirk

Reputation: 10958

Type.GetProperties returns the public properties of a type. But since you haven't specified whether the access type of your properties they are private.

You can use the overloaded Type.GetProperties(BindingFlags) method to get all properties, regardless of their access modifier.

Upvotes: 2

Steve B
Steve B

Reputation: 37660

Your class properties are private by default, and GetProperties only return public properties.

Either promote your properties as public :

class EventType
{
    public int id{set;}
    public string name{set;}
    public DateTime date{set;}
    public List<int> list{set;}
    public Guid guid{set;}
}

Or specify binding flas to get non-public properties :

Type t = input.GetType();
PropertyInfo[] properties = t.GetProperties(
    BindingFlags.NonPublic | // Include protected and private properties
    BindingFlags.Public | // Also include public properties
    BindingFlags.Instance // Specify to retrieve non static properties
    );

Upvotes: 5

Related Questions