Re: very strange effect with anonymous delegates (.net 2.0)
- From: "Richard Blewett [DevelopMentor]" <richard at nospam dotnetconsult dot co dot uk>
- Date: Thu, 10 Nov 2005 01:37:43 -0000
"cody" <deutronium@xxxxxx> wrote in message
news:OwyyMYY5FHA.2560@xxxxxxxxxxxxxxxxxxxxxxx
> Ok I had a look in reflector and I now see the problem. Since in generated
> code there is no local variable "PropertyInfo prop" but instead just a
> call to "enumerable.Current" so there is nothing to 'lift'.
>
> This maybe right from the view of the ILCode but it is toally wrong from
> the view of the sourcecode since "PropertyInfo prop" is a readonly
> variable from the programmer's view.
>
> Iam the opinion that is is a very dangerous pitfall. If MS won't fix that,
> they should at least throw a compiler error when a foreach iterator is
> used in a anonymous method.
>
> What do you think about that?
>
Surely its simply following standard scoping rules - its declaring the class
at the outermost scope of the local variables that are used within the anon
delegate. It obviously can't do it within the scope as the outermost one
wouldn't be in scope at that point.
So what you are asking for is special casing of your situation (I'm not
lambasting you for this just stating what you are asking). This is an
equally valid construct:
int i = 0;
int[] ii = int[3] { 1, 2, 3};
foreach ( i in ii )
{
// blah
}
this has basically the same scope as your iterator version - what would you
have the compiler do here?
I can understand your frustration, the closure like feature of anon
delegates can lead to some weird behavior if you don't know whats ahppening
behind the scenes. But asking fofr compiler special casig of your construct
is alot to ask (but it has been done before)
Regards
Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
.
- References:
- Re: very strange effect with anonymous delegates (.net 2.0)
- From: Richard Blewett [DevelopMentor]
- Re: very strange effect with anonymous delegates (.net 2.0)
- Prev by Date: Internet Explorer does not Fire events...
- Next by Date: Re: help converting jscript to C#
- Previous by thread: Re: very strange effect with anonymous delegates (.net 2.0)
- Next by thread: Re: COM Interop problem ( Mem leak )
- Index(es):
Relevant Pages
|