Hi James,
Thank you for your response, I am not fussed about what to use as long as I
can get it working. This is what I have got so far:
public interface IItemPersisor
{
void Save(object item);
}
public interface IItemPersisor<T> : IItemPersisor
{
void Save(T item);
}
but now in implementation I implement Save twice :
class PersonPersistor : IItemPersisor<Person>
{
void IItemPersisor<Person>.Save(Person item)
{
//Implementation of saving of Person
}
void IItemPersisor.Save(object item)
{
(this as IItemPersisor<Person>).Save(item as Person);
//<--Warning Upcasting is this ok??
}
}
Is Upcasting like above OK? or should I use some other method?,
if you know of a book or reference I should look up please let me
know, I'll chase it up myself and stop wasting your time.
Thanks
WVS
On Sun, Nov 7, 2010 at 5:54 PM, James Chapman-Smith
<[email protected]>wrote:
> 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
>