> You'll have to explain with an example.....it's early
in the
> morning and my brain isn't up to it.
public class Foo : ISomething
{
private List<Bar> _bars;
//... ctor, creation etc.
List<Object> ISomething.GetContainingLists()
{
List<Object> toReturn = new
List<Object>();
toReturn.Add(_bars);
return toReturn;
}
}
Now, in some other code, an ISomething instance is passed
in. This can be a Foo instance, but it can also be a FooToo
instance:
public class FooToo : ISomething
{
private List<BarToo> _bars;
//... ctor, creation etc.
List<Object> ISomething.GetContainingLists()
{
List<Object> toReturn = new
List<Object>();
toReturn.Add(_bars);
return toReturn;
}
}
So, consuming code, which consumes ISomething looks like:
private void ProcessISomething(ISomething toProcess)
{
List<Object> containingLists =
toProcess.GetContainingLists();
// and now what... how to access the lists inside
containingLists ?
// that's right, via a non-generic interface.
because any generic interface
// would require _at compile time_ the generic type.
Which is UNKNOWN.
foreach(IList list in containingLists)
{
// yadda yadda
}
}
You might think: I can solve this with a helper
method:
private void DoProcessing<T>(List<T> toProcess)
{
// yadday yadda
}
and write ProcessISomething as:
However, how would you use T in this method? You've
to specify a where clause, and ProcessISomething, how would
you write
that?
See, with generic interfaces, you don't solve this,
as you then still need a generic type parameter to specify
_at compile
time_ . With NON generic interfaces you solve this. That's
why non-generic interfaces should be used to consume generic
code in
routines which don't know the type of the generic parameter.
Using interfaces is also a way to do generic
programming, i.e. write a program that can work with
multiple types. Mixing
interfaces and generics, makes things only more complicated
instead of more usable and readable.
Frans
>
> -----Original Message-----
> From: Discussion of advanced .NET topics.
> [mailto:ADVANCED-DOTNET DISCUSS.DEVELOP.COM] On
Behalf Of Frans Bouma
> Sent: 17 October 2006 16:39
> To: ADVANCED-DOTNET DISCUSS.DEVELOP.COM
> Subject: Re: [ADVANCED-DOTNET] Type declarations in
ide....
>
> > I agree....its more of an experiment at the
moment.....and they are
> > interfaces....just parametised ones.
>
> Generic interfaces? Isn't that mitigating the
aspect
> of interfaces?
> I mean: if you have an instance of Class<T>, and
pass that to
> some code and inside that code you don't know the type
of T
> (this can happen), you can refer to the instance of
Class<T>
> with the interface, so your code will become very
simple. If
> you have generic interfaces as well, the problem
remains and
> you have to at some point need to know the type of T to
get
> things compiled.
>
> This especially creates problems when T has to
be of
> some type, so where clauses have to be added to the
method.
> With an interface you can solve this problem. so I
always use
> the rule of thumb: use interfaces to use generic
classes in
> code where you don't know the generic parameter type,
and
> thus keep your interfaces non-generic just for this
purpose.
>
> FB
>
> >
> > -----Original Message-----
> > From: Discussion of advanced .NET topics.
> > [mailto:ADVANCED-DOTNET DISCUSS.DEVELOP.COM] On
Behalf Of
> Frans Bouma
> > Sent: 17 October 2006 13:17
> > To: ADVANCED-DOTNET DISCUSS.DEVELOP.COM
> > Subject: Re: [ADVANCED-DOTNET] Type declarations
in ide....
> >
> > > A slightly bizarre question....
> > >
> > > I've got some generic methods that return
generic types,
> > the problem
> > > is that sometimes the types get very
large...e.h.
> > >
> > >
> > >
Foo<Foo<Bar<Foo<Bar>,Foo<Foo>>>&g
t; x =
> > >
a.GetFoo<Foo<Bar<Foo<Bar>,Foo<Foo>>&
gt;>
> > > (b);
> > >
> > > is there a way of getting the IDE to auto
generate the type
> > > declaration, so I don't spend hours foo'ing
and bar'ing.
> >
> > I'm not sure if you should continue with
this. It
> makes code
> > VERY unreadable. Too much of a good is still 'too
much' ;)
> >
> > You might want to use an interface here
and there.
> >
> > Frans
> >
> > ===================================
> > This list is hosted by DevelopMentor(r) http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
> >
> >
************************************************************
**
> > *************
> > CONFIDENTIALITY NOTICE
> >
> > The contents of this e-mail are confidential to
the
> ordinary user of
> > the e-mail address to which it was addressed, and
may also be
> > privileged. If you are not the addressee of this
e-mail
> you may not
> > copy, forward, disclose or otherwise use it or any
part of
> it in any
> > form whatsoever.If you have received this e-mail
in error, please
> > e-mail the sender by replying to this message.
> >
> > It is your responsibility to carry out appropriate
virus and other
> > checks to ensure that this message and any
attachments do
> not affect
> > your systems / data. Any views or opinions
expressed in this e-mail
> > are solely those of the author and do not
necessarily
> represent those
> > of MTV Networks Europe unless specifically stated,
nor does this
> > message form any part of any contract unless so
stated.
> >
> > MTV reserves the right to monitor e-mail
communications from
> > external/internal sources for the purposes of
ensuring correct and
> > appropriate use of MTV communication equipment.
> >
> > MTV Networks Europe
> >
************************************************************
**
> > *************
> >
> >
> > ===================================
> > This list is hosted by DevelopMentorR http://www.develop.com
> >
> > View archives and manage your subscription(s) at
> > http://discuss.develop.com
> >
>
> ===================================
> This list is hosted by DevelopMentor(r) http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
>
>
************************************************************
**
> *************
> CONFIDENTIALITY NOTICE
>
> The contents of this e-mail are confidential to the
ordinary
> user of the e-mail address to which it was addressed,
and may
> also be privileged. If you are not the addressee of
this
> e-mail you may not copy, forward, disclose or otherwise
use
> it or any part of it in any form whatsoever.If you have
> received this e-mail in error, please e-mail the sender
by
> replying to this message.
>
> It is your responsibility to carry out appropriate
virus and
> other checks to ensure that this message and any
attachments
> do not affect your systems / data. Any views or
opinions
> expressed in this e-mail are solely those of the author
and
> do not necessarily represent those of MTV Networks
Europe
> unless specifically stated, nor does this message form
any
> part of any contract unless so stated.
>
> MTV reserves the right to monitor e-mail communications
from
> external/internal sources for the purposes of ensuring
> correct and appropriate use of MTV communication
equipment.
>
> MTV Networks Europe
>
************************************************************
**
> *************
>
>
> ===================================
> This list is hosted by DevelopMentorR http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com
>
===================================
This list is hosted by DevelopMentorŪ http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
|