How do I access parameters and methods defined across two interface from a class that implements those two interfaces?

In my factory I'm generating my object based on one interface, but it implements two.

for example

myClass1 : Interface1, Interface2 {
   string blah;
   int num;
}

myClass2 : Interface1, Interface2 {
   string blah;
   int num;
}

myClass3 : Interface1, Interface3 {
   string blah;
   bool truefalse;
}

interface Interface1 {
   string blah;
}

interface Interface2 {
   int num;
}

interface Interface3 {
   bool truefalse;
}

But in my factory I do this:

return Activator.CreateInstance(t) as Interface1;

And then I have this object

Interface1 inter1;

But I want to do inter1.num but only have access to inter1.blah

Well basically I'm getting all the types that implement Interface 1 and getting a new instance of that functionality is the main one I'm interested in.. But after that I know those objects implement Interface 2 so I want access the properties and methods defined in that interface without recasting to the exact class object (because I don't know which one it is exactly)

Is there a way to do this?

Jon Skeet
people
quotationmark

You don't need to cast to the concrete class - just cast to the interface you're interested in (or use as to perform a conditional cast, and check it for success):

Interface1 x = (Interface1) Activator.CreateInstance(type);
Interface2 y = x as Interface2;
if (y != null)
{
     // Use y
}

Of course if you can refactor your interfaces - or provide a common interface which extends both of the other interfaces - that's better, but the above will work even if you're using interfaces which can't be changed.

people

See more on this question at Stackoverflow