180 likes | 377 Views
Alexandru Ghiondea C# Compiler SDET. C# 4.0. The Evolution of C#. C# 4.0. Dynamic Programming. C# 3.0. Language Integrated Query. C# 2.0. Generics. C# 1.0. Managed Code. C# 4.0 Language Innovations. Dynamically Typed Objects Optional and Named Parameters
E N D
Alexandru Ghiondea C# Compiler SDET C# 4.0
The Evolution of C# C# 4.0 Dynamic Programming C# 3.0 Language Integrated Query C# 2.0 Generics C# 1.0 Managed Code
C# 4.0 Language Innovations • Dynamically Typed Objects • Optional and Named Parameters • Improved COM Interoperability
.NET Dynamic Programming IronPython IronRuby C# VB.NET Others… Dynamic Language Runtime Expression Trees Dynamic Dispatch Call Site Caching ObjectBinder JavaScriptBinder PythonBinder RubyBinder COMBinder
Dynamically Typed Objects Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); .NET object object calc = GetCalculator(); TypecalcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, newobject[] { 10, 20 }); int sum = Convert.ToInt32(res); Dynamic Language object ScriptObject calc = GetCalculator(); object res = calc.Invoke("Add", 10, 20); int sum = Convert.ToInt32(res); Statically typed to be dynamic dynamic calc = GetCalculator(); int sum = calc.Add(10, 20); Dynamic conversion Dynamic method invocation
Under the cover • dynamic is a new type only in the compiler • The return type of a dynamic operation is dynamic • There is an assignment conversion from dynamic to any type • We support all types of operations on dynamic
IDynamicObject publicabstractclassDynamicObject : IDynamicObject { publicvirtualobjectGetMember(GetMemberBinder info); publicvirtualobjectSetMember(SetMemberBinder info, object value); publicvirtualobjectDeleteMember(DeleteMemberBinder info); publicvirtualobjectUnaryOperation(UnaryOperationBinder info); publicvirtualobjectBinaryOperation(BinaryOperationBinder info, objectarg); publicvirtualobject Convert(ConvertBinder info); publicvirtualobject Invoke(InvokeBinder info, object[] args); publicvirtualobjectInvokeMember(InvokeMemberBinder info, object[] args); publicvirtualobjectCreateInstance(CreateInstanceBinder info, object[] args); publicvirtualobjectGetIndex(GetIndexBinder info, object[] indices); publicvirtualobjectSetIndex(SetIndexBinder info, object[] indices, object value); publicvirtualobjectDeleteIndex(DeleteIndexBinder info, object[] indices); publicMetaObjectIDynamicObject.GetMetaObject(); }
Improved COM Interoperability • Automatic object dynamic mapping • Optional and named parameters • Optional “ref” modifier • Interop type embedding (“No PIA”)
object -> dynamic mapping We need to cast ((Excel.Range)xl.Cells[1,1]).Value2 = “ID”; xl.Cells[1,1].Value2 = “ID”; You can continue to “dot”
Optional and named parameters xlWorkbook.Worksheets.Add(Type.Missing, xlWorkbook.ActiveSheet, Type.Missing, Type.Missing); Arguments evaluated in order written xlWorkbook.Worksheets.Add(After:xlWorkbook.ActiveSheet); Named arguments can appear in any order
Optional and named parameters Optional parameters publicStreamReaderOpenTextFile( string path, Encodingencoding, booldetectEncoding, intbufferSize); publicStreamReaderOpenTextFile( string path, Encodingencoding = null, booldetectEncoding = true, intbufferSize = 1024); Non-optional must be specified OpenTextFile("foo.txt”); OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096); Named argument
Optional “ref” modifier objectfileName = "Test.docx"; object missing = System.Reflection.Missing.Value; doc.SaveAs(reffileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); doc.SaveAs("Test.docx");
Under the cover • The default value for a parameter is encoded with a DefaultParameterValue attribute • No change in overload resolution • The “omit ref” only works for COM objects • No-PIA encapsulates the used interfaces into the assembly
Additional Resources • C# 4.0 Samples and Whitepaper • http://code.msdn.microsoft.com/csharpfuture • Visual C# Developer Center • http://csharp.net • C# team member’s blogs • http://blogs.msdn.com/ericlippert/ • http://blogs.msdn.com/cburrows/ • http://blogs.msdn.com/samng/ • http://blogs.msdn.com/sreekarc/ • http://blogs.msdn.com/mattwar/ • http://blogs.msdn.com/ed_maurer/ • http://blogs.msdn.com/davsterl/ • http://blogs.msdn.com/alexghi