Hi Wolfgang,

 

The simplest approach, which also works in 3.5, is to create an IPersistor
interface that IPersistor<T> inherits from. Then you can assign any
IPersistor<T> to a variable of IPersistor.

 

In 4.0 though, you can now use co-variance or contravariance (not both) when
designing your interface. So if you have only "in" parameters of type T you
can define the IPersistor<T> interface as IPersistor<in T> which would allow
you to assign IPersistor<B> to IPersistor<C> provided that C inherits from
B. If you only have "out" parameters ie return types of T then you can
define IPersistor<T> as IPersistor<out T> which would allow you to assign
IPersistor<B> to IPersistor<A> provided that B inherits from A.

 

It's this latter case that would allow you to assign persistors to
IPersistor<object>, but I suspect that the interface is not an "out"
interface. If that's correct you need to stick to the 3.5 functionality.

 

Cheers.

 

James.

 

From: [email protected] [mailto:[email protected]]
On Behalf Of Wolfgang Von Steinberg
Sent: Sunday, 7 November 2010 16:01
To: [email protected]
Subject: Generic Interface Question (.net 4.0)

 

Hello

 

I am using .net 4.0

 

I have :

 

IPersistor<T>

 

and 

 

class CausePersistor implementing IPersistor<Cause>

class PersonPersistor implementing IPersistor<Person>

 

now, how can I have something like :

 

IPersistor persistor; where I could assign an object that implements either
IPersistor<Cause> , IPersistor<Person> ?

 

In other words what is the abstraction to be used for IPersistor<object> ?

 

I already tried 

 

 IPersistor<object> persistor = new CausePersistor(); but this doesn't
compile.

 

Thank you

 

WVS

Reply via email to