3.5 extension methods give you the OfType<T> extension on IEnumerable,
so you can now do:

foreach (ReportA item in reports.OfType<ReportA>())

Otherwise, you'd have to do the cast yourself and catch it. Unfortunate
that both C# and VB chose to implement implicit casts in foreach....

--MB

> -----Original Message-----
> From: Discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED] On Behalf Of Bob Provencher
> Sent: Friday, December 14, 2007 1:58 PM
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: Re: [ADVANCED-DOTNET] Method that returns List<BaseClass>
> 
> I think if someone puts a Report in your list, which it's type implies
> is
> allowed, your foreach will throw an invalid cast exception.
> 
> -----Original Message-----
> From: Discussion of advanced .NET topics.
> [mailto:[EMAIL PROTECTED] On Behalf Of Ron Young
> Sent: Friday, December 14, 2007 1:46 PM
> To: ADVANCED-DOTNET@DISCUSS.DEVELOP.COM
> Subject: Re: [ADVANCED-DOTNET] Method that returns List<BaseClass>
> 
> Indeed it does. Thank you.
> 
> I was under the impression that if the method that returned derived
> reports
> ultimately returned a list of the base report, then the client would
> get the
> properties defined in the base report.
> 
> But it's up to the client to determine what types of reports are in
the
> list, so this works:
> 
> // client-side
> List<Report> reports = provider.GetReports();
> foreach (ReportA item in reports)
>     WL(item.MyName);
> 
> ===================================
> This list is hosted by DevelopMentor(r)  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

Reply via email to