1

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?

Jurijs Kastanovs
  • 685
  • 1
  • 15
  • 35
  • 1
    You should rethink your design, either you *need* the id for all types of `IRequest` or not. The whole purpose off interfaces is to organize functionality into a common thing. So when you have a property that doesn´t fit into this *thing* you need to define your things differently. However you may also create an interface that derives from `IRequest` and also has the id-property. – MakePeaceGreatAgain Dec 08 '16 at 11:55
  • 1
    You can use reflection indeed, but better implement some interface on request types that has PersonId property (let's name it IHasPersonInfo). Then try to cast to IHasPersonInfo and get your property. – Evk Dec 08 '16 at 11:55
  • @Evk Reflection is most of the time the worst solution to get things done. – MakePeaceGreatAgain Dec 08 '16 at 11:56
  • I can't rethink the design, as it's too grand of a work. It's an functioning application support. But I think the idea about the second interface might be worthy, I will possibly try that, thanks. – Jurijs Kastanovs Dec 08 '16 at 11:58

2 Answers2

1

What about creating an intermediate interface?

public class RequestParameters
{
     public IRequest Request { get; set; }
}    
public interface IRequest {
     string Username { get; set; }
     string Password { get; set; }
}
public interface IRequestWithId : IRequest
{
    string ID {get; set; }
}

Now you can check against that interface instead of a concrete class to check if there is an ID or not.

Other than that there is only reflection which is a really bad idea here. When defining interfaces you want to reduce code-coupling which means you don´t want to rely on actual types (that define how things are done) but only their behaviour (what can be done with them). Using reflection would circumvent this as you indirectly rely on the actual types. Furthermore using reflection is bad for maintainance, in particular when you refactor the methods (renaming for example).

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
0

first of all, in c# there is another type called abstract class which in my opinion would be a better suit for your situation. take a look at this question. interfaces are not meant to hold variables, only functions.

 public abstract class ARequest
 {
    string Username { get; set; }
    string Password { get; set; }

    public abstract int GetId() {}

 }
Community
  • 1
  • 1
Mightee
  • 689
  • 7
  • 22
  • Thanks, but I can't afford rethinking the design. – Jurijs Kastanovs Dec 08 '16 at 12:12
  • Well what can you do ? Can you add a New method to the Interface ? – Mightee Dec 08 '16 at 12:44
  • I can't add anything to this specific interface. So I've already went with writing an additional interface for the specific fields I need to fetch and then try-parsing the object into that interface. Worked like a charm. As I've said, I guess I needed a break if I couldn't think of this. – Jurijs Kastanovs Dec 09 '16 at 08:54