Microsoft Developer Network >
Página principal de foros
>
Core Coding Experience in Visual Basic and Visual C# in the Visual Studio 2010 and .NET Framework 4.0 CTP
>
C# 4.0 / dynamic / operators
C# 4.0 / dynamic / operators
- According to the "New features in CSharp 4.0" doc, dynamic operator dispatch is supported; is this implemented yet?I was trying to compare to an existing mechanism for using operators with generics (via Expression), but it doesn't compile:using System;using System.Diagnostics;using System.Linq.Expressions;class Program{static void Main(string[] args){TestOperator<int>(10, 2);TestDynamic<int>(10,2);}const int COUNT = 10000000;static void TestOperator<T>(T seed, T inc){T value = seed;Stopwatch watch = Stopwatch.StartNew();for (int i = 0; i < COUNT; i++){value = OperatorCache<T>.Add(value, inc);}watch.Stop();Console.WriteLine("Operator: {0} ({1})", watch.ElapsedMilliseconds, value);}static void TestDynamic<T>(T seed, T inc){dynamic value = seed;//dynamic tmp = inc;Stopwatch watch = Stopwatch.StartNew();for (int i = 0; i < COUNT; i++){// Error 1 Operator '+' cannot be applied to operands of type '::dynamic' and 'T'// (note I also tried with dynamic + dynamic, via "tmp")value = value + inc; // tmp}watch.Stop();Console.WriteLine("Dynamic: {0} ({1})", watch.ElapsedMilliseconds, value);}static class OperatorCache<T>{public static readonly Func<T, T, T> Add;static OperatorCache(){var p1 = Expression.Parameter(typeof(T), "x");var p2 = Expression.Parameter(typeof(T), "y");var body = Expression.Add(p1, p2);Add = Expression.Lambda<Func<T, T, T>>(body, p1, p2).Compile();}}}
Marc [C# MVP]- EditadoMarc GravellMVPlunes, 03 de noviembre de 2008 16:02missed call
Respuestas
- Chris Burrow mentioned:
If you're wondering what does work for dynamic C# in the CTP build, here's the short story: (a) dynamic method calls, (b) dynamic property gets/sets, and (c) dynamic conversion sites.
So I guess the answer is dynamic operator dispatch is not yet implemented in the CTP, and so value + inc will fail because it's a UnaryOperationAction (the receiver of method call doesn't count, so it's a UnaryOperationAction instead of a BinaryOperationAction).- EditadoRednaxelaFX domingo, 09 de noviembre de 2008 9:16
- Marcado como respuestaMarc GravellMVPmartes, 04 de noviembre de 2008 7:44
Todas las respuestas
- Chris Burrow mentioned:
If you're wondering what does work for dynamic C# in the CTP build, here's the short story: (a) dynamic method calls, (b) dynamic property gets/sets, and (c) dynamic conversion sites.
So I guess the answer is dynamic operator dispatch is not yet implemented in the CTP, and so value + inc will fail because it's a UnaryOperationAction (the receiver of method call doesn't count, so it's a UnaryOperationAction instead of a BinaryOperationAction).- EditadoRednaxelaFX domingo, 09 de noviembre de 2008 9:16
- Marcado como respuestaMarc GravellMVPmartes, 04 de noviembre de 2008 7:44
- Cheers
Marc [C# MVP]

