jeudi 31 août 2017

Get all properties of an implemented interface

I have the following setup:

public interface IInput
{
}

public class SomeInput : IInput
{
    public int Id { get; set; }
    public string Requester { get; set; }
}

Now I want to write a function that can take anything that implements IInput and use reflection to give me the properties :

public Display(IInput input)
{
    foreach (var property in input.GetType().GetProperties())
    {
        Console.WriteLine($" {property.Name}: {property.GetValue(input)}");
    }
}

Where

var test = new SomeInput(){Id=1,Requester="test"};
Display(test);

shows

Id: 1
Requester: test





Aucun commentaire:

Enregistrer un commentaire