430 likes | 529 Views
Required Slide. SESSION CODE: DEV404. C# in the Big World. Mads Torgersen Program Manager Microsoft Corporation. C# in the Big World. New features Dynamic Named and optional arguments COM interop features The design of dynamic. The DLR on the CLR. Common Language Runtime – CLR:
E N D
Required Slide SESSION CODE: DEV404 C# in the Big World Mads Torgersen Program Manager Microsoft Corporation
C# in the Big World • New features • Dynamic • Named and optional arguments • COM interop features • The design of dynamic
The DLR on the CLR • Common Language Runtime – CLR: • Common platform for static languages • Facilitates great interop
The DLR on the CLR • Common Language Runtime – CLR: • Common platform for static languages • Facilitates great interop • Dynamic Language Runtime – DLR: • Common platform for dynamic languages • Facilitates great interop
The DLR on the CLR • Common Language Runtime – CLR: • Common platform for static languages • Facilitates great interop • Dynamic Language Runtime – DLR: • Common platform for dynamic languages • Facilitates great interop
Dynamic Objects • Implement their own binding • Programmatically describes meaning of operations • The DLR caches and optimizes • Built by dynamic languages – or by you!
Why Dynamic in C#? • Build on DLR opportunity • Use code from dynamic languages • Use other dynamic object models • Improve COM interop
Dynamic Language Runtime (DLR) IronPython IronRuby C# VB.NET Others… Dynamic Language Runtime Expression Trees Dynamic Dispatch Call Site Caching ObjectBinder JavaScriptBinder PythonBinder RubyBinder COMBinder
The dynamic Type in C# Calculator calc = GetCalculator(); int sum = calc.Add(10, 20); object calc = GetCalculator(); TypecalcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, newobject[] { 10, 20 }); int sum = Convert.ToInt32(res); 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
Dynamic DEMO
System.Dynamic.DynamicObject publicclassDynamicObject : IDynamicMetaObjectProvider { publicvirtualIEnumerable<string> GetDynamicMemberNames(); publicvirtualDynamicMetaObjectGetMetaObject(Expression parameter); publicvirtualboolTryBinaryOperation(BinaryOperationBinder binder, objectarg, outobject result); publicvirtualboolTryConvert(ConvertBinder binder, outobject result); publicvirtualboolTryCreateInstance(CreateInstanceBinder binder, object[] args, outobject result); publicvirtualboolTryDeleteIndex(DeleteIndexBinder binder, object[] indexes); publicvirtualboolTryDeleteMember(DeleteMemberBinder binder); publicvirtualboolTryGetIndex(GetIndexBinder binder, object[] indexes, outobject result); publicvirtualboolTryGetMember(GetMemberBinder binder, outobject result); publicvirtualboolTryInvoke(InvokeBinder binder, object[] args, outobject result); publicvirtualboolTryInvokeMember(InvokeMemberBinder binder, object[] args, outobject result); publicvirtualboolTrySetIndex(SetIndexBinder binder, object[] indexes, object value); publicvirtualboolTrySetMember(SetMemberBinder binder, object value); publicvirtualboolTryUnaryOperation(UnaryOperationBinder binder, outobject result); }
DynamicObject DEMO
Optional and Named Parameters publicStreamReaderOpenTextFile( string path, Encodingencoding, booldetectEncoding, intbufferSize); Primary method Secondary overloads publicStreamReaderOpenTextFile( string path, Encodingencoding, booldetectEncoding); publicStreamReaderOpenTextFile( string path, Encodingencoding); publicStreamReaderOpenTextFile( string path); Call primary with default values
Optional and Named Parameters Optional parameters publicStreamReaderOpenTextFile( string path, Encodingencoding, booldetectEncoding, intbufferSize); publicStreamReaderOpenTextFile( string path, Encodingencoding = null, booldetectEncoding = true, intbufferSize = 1024); Named argument OpenTextFile("foo.txt", Encoding.UTF8); OpenTextFile("foo.txt", Encoding.UTF8, bufferSize: 4096); Named arguments can be in any order Evaluated inorder written Named arguments must be last OpenTextFile( bufferSize: 4096, path: "foo.txt", detectEncoding: false); Non-optional must be specified
Improved COM Interoperability 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");
Improved COM Interoperability • Optional and named parameters • Indexed properties • Optional “ref” modifier • Interop type embedding (“No PIA”) • Automatic object dynamic mapping
Designing DynamicInitial sentiment • C# is not a dynamic language! • Dynamic is a dangerous foreign element • It should be syntactically explicit
Static Example string[]strings = GetStrings(); string last = strings[strings.Length – 1];
Explicit Dynamic Operations object strings = GetDynamicObject(); string last = strings[strings.Length – 1]; object strings = GetDynamicObject(); string last = strings[strings~.Length – 1]; object strings = GetDynamicObject(); string last = strings~[strings~.Length – 1]; object strings = GetDynamicObject(); string last = strings~[strings~.Length~– 1]; object strings = GetDynamicObject(); string last = (string)strings~[strings~.Length~– 1]; object strings = GetDynamicObject(); string last = ~(string)strings~[strings~.Length~– 1];
Explicit Dynamic Operations • Reads horribly! • Insight: Dynamic operations travel in packs! object strings = GetDynamicObject(); string last = ~(string)strings~[strings~.Length~– 1];
Dynamic Blocks object strings = GetDynamicObject();string last; dynamic {last = strings[strings.Length– 1]; }
Dynamic Blocks • Different “dialect” of C# inside • Opt out with static contexts? • Lose sight of big contexts • Insight: Dynamicness flows from within object strings = GetDynamicObject();string last; dynamic {last = strings[strings.Length– 1]; }
Contagious Dynamic Expressions object strings = GetDynamicObject(); string last = strings[dynamic(strings).Length – 1];
Contagious Dynamic Expressions • Rules of propagation – which operations are dynamic? • Factoring out subexpressions is hard – what is their type? • Insight: Dynamicness follows the objects object strings = GetDynamicObject(); string last = strings[dynamic(strings).Length – 1];
The dynamic Type dynamic strings = GetDynamicObject(); string last = strings[strings.Length– 1];
The dynamic Type • Propagates through the types • Reads like normal C#! dynamic strings = GetDynamicObject(); string last = strings[strings.Length– 1];
Why is this OK?What Happened to Syntactically Explicit? • Embrace dynamicness • You can’t make a language feature that is “built to be ugly” • Dynamic isn’t there to take away safety • But to make ugly code nice • Dynamicness is still explicit – just not in syntax • What better way to express lack of typing than through types?
Type or Type Modifier? • Generality: • Static binding of Foo’s members • Dynamic binding of the rest • Simplicity: • Dynamic binding of all members • Even those on Object dynamic Foo foo = GetDynamicFoo(); dynamic foo = GetDynamicFoo();
Dynamic Binding When? • When the receiver is dynamic: • Forces you to choose a type • When anysubexpression is dynamic: • Softer “landing” back in type land dynamic result = Math.Abs((double)d); dynamic result = Math.Abs(d);
Dynamic Operations • Dynamic result: • Method call Math.Abs(d) • Invocationd(“Hello”) • Member access d.Length • Operator application 4+ d • Indexing d[“Hello”] • Static result: • Conversions (double)d • Object creation newFoo(d)
How Dynamic? • “Just enough” dynamicness • Nobody uses your runtime type unless you let them • Principle of least surprise: • Argument’s contribution to binding is invariant M(d, GetFoo()); Bind with compile-time type Bind with runtime type
Summary: The Meaning of dynamic dynamicmeans “use my runtime type for binding”
Summary: The Reason for dynamicWhich would you rather see – or write? TypecalcType = calc.GetType(); object res = calcType.InvokeMember("Add", BindingFlags.InvokeMethod, null, newobject[] { 10, 20 }); int sum = Convert.ToInt32(res); int sum = calc.Add(10, 20);
Takeaways • C# 4.0 is about interacting with a bigger world • Dynamic allows anything to “look like an object” • Named and optional eliminate overloads and boilerplate • COM interop is finally natural in C#
Required Slide Speakers, please list the Breakout Sessions, Interactive Sessions, Labs and Demo Stations that are related to your session. Related Content – Breakout Sessions DEV307: F# in Microsoft Visual Studio 2010 (6/10 from 9:45am – 11:00am) DEV315: Microsoft Visual Studio 2010 Tips and Tricks (6/8 from 5:00pm – 6:15pm) DEV316: Modern Programming with C++Ox in Microsoft Visual C++ 2010 (6/8 from 3:15pm – 4:30pm) DEV319: Scale and Productivity for C++ Developers with Microsoft Visual Studio 2010 (6/9 from 8:00am – 9:15am) DEV401: Advanced Use of the new Microsoft Visual Basic 2010 Language Features (6/9 from 9:45am – 11:00am) DEV404: C# in the Big World (6/8 from 1:30pm – 2:45pm) DEV406: Integrating Dynamic Languages into Your Enterprise Applications (6/8 from 8am – 9:15am) DEV407: Maintaining and Modernizing Existing Applications with Microsoft Visual Studio 2010 (6/10 from 8:00am – 9:15am)
Required Slide Speakers, please list the Breakout Sessions, Interactive Sessions, Labs and Demo Stations that are related to your session. Related Content – Interactive Sessions and HOL's DEV03-INT: Meet the C# team (6/9 from 1:30-2:45pm) DEV04-INT: Meet the VB team (6/10 from 3:15 – 4:30pm) DEV09-INT: Microsoft Visual Basic and C# IDE Tips and Tricks (6/7 from 4:30pm -5:45pm) DEV10-INT: Using Dynamic Languages to build Scriptable Applications ((6/9 from 8:00am -9:15am) DEV11 –INT: IronPython Tools (6/10 from 5:00pm – 6:15pm) DEV05-HOL: Introduction to F#
Required Slide Track PMs will supply the content for this slide, which will be inserted during the final scrub. Track Resources • Visual Studio – http://www.microsoft.com/visualstudio/en-us/ • Soma’s Blog – http://blogs.msdn.com/b/somasegar/ • MSDN Data Developer Center – http://msdn.com/data • ADO.NET Team Blog – http://blogs.msdn.com/adonet • WCF Data Services Team Blog – http://blogs.msdn.com/astoriateam • EF Design Blog – http://blogs.msdn.com/efdesign
Required Slide Resources Learning • Sessions On-Demand & Community • Microsoft Certification & Training Resources www.microsoft.com/teched www.microsoft.com/learning • Resources for IT Professionals • Resources for Developers • http://microsoft.com/technet • http://microsoft.com/msdn
Required Slide Complete an evaluation on CommNet and enter to win!
Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31st http://northamerica.msteched.com/registration You can also register at the North America 2011 kiosk located at registrationJoin us in Atlanta next year
© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries. The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.