jeudi 8 décembre 2016

C# Get value of a property from an object hidden behind Interface

To explain it better, I have an object that has property of interface type that can be carrying a variety of different objects.

public class RequestParameters {
     public IRequest Request { get; set; }
}

Say the interface looks something like this:

public interface IRequest {
     string Username { get; set; }
     string Password { get; set; }
}

Now upon receiving the RequestParameters object, I want to get the value of a property PersonId from the object inside Request property. The interface doesn't and shouldn't contain this property, as not all of the Request types have that property. And I don't know which of the dozens of types will the Request object correspond to, so casting isn't a solution.

So how can I possibly fetch the property PersonId in such a case? I suppose some sort of reflection is the only way, but how exactly can it be done?





Aucun commentaire:

Enregistrer un commentaire