Dynamic C#

1,455 views
1,309 views

Published on

Dynamic C#

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,455
On SlideShare
0
From Embeds
0
Number of Embeds
156
Actions
Shares
0
Downloads
12
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Dynamic C#

  1. 1. by @AntyaDevDYNAMIC C#
  2. 2. STATIC VS DYNAMIC
  3. 3. The many faces of “dynamic”Generate codeat runtimeChanging typesat runtimeNo typesat allSimplifieddeployment
  4. 4. • Trend on non-schematized data• Remember WSDL, SOAP, XSD?• How about REST, JSON?• The next browser war• JavaScript on the web• Optimized scripting engines• Towards metaprogramming?• Ruby community• Code as dataIT’S A DYNAMIC WORLD
  5. 5. DynamicLanguagesSimple and succinctImplicitly typedMeta-programmingNo compilationStaticLanguagesRobustPerformantIntelligent toolsBetter scalingSTATIC VS DYNAMIC
  6. 6. DLR Hero• Jim HuguninCo-designer ASpectJJython implementation• Python on the JVMIronRuby implementation• Python on the .NET
  7. 7. DLR Hero• John LamRubyCLRbridge between the Rubyinterpreter and the CLRIronRubyRuby on the .NET
  8. 8. DLR HeroMartin MalyCommon LanguageRuntimeDynamic LanguageRuntime
  9. 9. C# compiler teamChris Burrow, Sam Ng
  10. 10. Action Python Ruby C# VB.NETGetMember x.Foo x.Foo x.Foo x.FooSetMember x.Foo = y x.Foo = y x.Foo = y x.Foo = yDeleteMember del d.Foox.send:remove_instance_variable:@fooNo syntax No syntaxUnaryOperation -x -x -x -xBinaryOperation x + y x + y x + y x + yConvert No syntax No syntax (Foo)x CType(x,Foo)InvokeMember x.Foo(a,b) x.Foo(a,b) x.Foo(a,b) x.Foo(a,b)Invoke x(a,b) x.call(a,b) x(a,b) x(a,b)CreateInstance X(a,b) X.new(a,b) No syntax No syntaxGetIndex x[a,b] x[a,b] x[a,b] x(a,b)SetIndex x[a,b] = y x[a,b] = y x[a,b] = y X(a,b) = yDeleteIndex del x[a,b] No syntax No syntax No syntaxCommon actions
  11. 11. IDynamicMetaObjectProviderpublic interface IDynamicMetaObjectProvider{DynamicMetaObject GetMetaObject(Expression parameter);}
  12. 12. DynamicMetaObjectpublic class DynamicMetaObject{public BindingRestrictions Restrictions { get; }public Expression Expression { get; }public bool HasValue { get; }public object Value { get; }public Type RuntimeType { get; }public virtual DynamicMetaObject BindGetMember(GetMemberBinder b);public virtual DynamicMetaObject BindSetMember(SetMemberBinder b,DynamicMetaObject value);public virtual DynamicMetaObject BindDeleteMember(DeleteMemberBinder b);// Other bind operations…}
  13. 13. System.Dynamic.DynamicObjectpublic class DynamicObject : IDynamicMetaObjectProvider{public virtual bool TryBinaryOperation(BinaryOperationBinder binder, object arg, outobject result);public virtual bool TryConvert(ConvertBinder binder, out object result);public virtual bool TryCreateInstance(CreateInstanceBinder binder, object[] args, outobject result);public virtual bool TryGetIndex(GetIndexBinder binder, object[] indexes, out objectresult);public virtual bool TryGetMember(GetMemberBinder binder, out object result);public virtual bool TryInvoke(InvokeBinder binder, object[] args, out object result);public virtual bool TryInvokeMember(InvokeMemberBinder binder, object[] args, outobject result);public virtual bool TrySetIndex(SetIndexBinder binder, object[] indexes, objectvalue);public virtual bool TrySetMember(SetMemberBinder binder, object value);}
  14. 14. PythonBinderRubyBinderCOMBinderJavaScriptBinderObjectBinderDYNAMIC LANGUAGE RUNTIME (DLR)Dynamic Language RuntimeDLR Trees Dynamic Dispatch Call Site CachingIronPython IronRuby C# VB.NET Others…
  15. 15. CALL SITES• Old Idea: Polymorphic Inline Cache• Implemented with delegates and generics• No changes in CLR runtime engine (today)• Major Addition: Multiple languages on CLR• Interop for sharing objects across languages• Customization to work for each language• Customization for library writers• System.Runtime.CompilerServices
  16. 16. CLRCompile RunBind callExpressionTreeDLRCacheCOM BinderIronPythonBinderC# RuntimeBinder…HOW DYNAMIC WORKS
  17. 17. CALLSITE<T>static CallSite<Func<CallSite, object, int, bool>> _site = …;if (_site.Target(_site, x, 0)) { … }if (x == 0) { … }static bool _0(Site site, object x, int y) {return site.Update(site, x, y); //tailcall}As stronglytyped aspossibleCache is learning
  18. 18. CALLSITE<T>static CallSite<Func<CallSite, object, int, bool>> _site = …;if (_site.Target(_site, x, 0)) { … }if (x == 0) { … }static bool _2(Site site, object x, int y) {if (x is int) {return (int)x == y;} else if (x is BigInteger) {return BigInteger.op_Equality((BigInteger)x, y);} else {return site.Update(site, x, y); //tailcall}}
  19. 19. DynamicMetaObjectpublic class DynamicMetaObject{public BindingRestrictions Restrictions { get; }public Expression Expression { get; }public bool HasValue { get; }public object Value { get; }public Type RuntimeType { get; }public virtual DynamicMetaObject BindGetMember(GetMemberBinder b);public virtual DynamicMetaObject BindSetMember(SetMemberBinder b,DynamicMetaObject value);public virtual DynamicMetaObject BindDeleteMember(DeleteMemberBinder b);// Other bind operations…}
  20. 20. Show me Dynamic C#
  21. 21. Ruby’s Markup Builder is an example ofwhat can be achieved withmethod missingb = Builder::XmlMarkup.newhtml = b.html {b.head {b.title "XML Builder Test"}b.body {b.h1 "Title of Page“b.p "Sample paragraph text“b.p "Sample paragraph text“}}<head><title>XML Builder Test</title>- <body><h1>Title of Page</h1><p>Sample paragraph text</p><p>Sample paragraph text</p></body></head>
  22. 22. class User < ActiveRecord::Base; endusers = User.find_all_by_state("TX")user = User.find_or_create_by_email("foo@bar.com")
  23. 23. by @AntyaDev
  24. 24. OakFrictionless developmentfor ASP.NET MVC singlepage web apps.Prototypical and dynamiccapabilities brought to C#.
  25. 25. STATIC TYPING WHERE POSSIBLE,DYNAMIC TYPING WHEN NEEDED
  26. 26. LINKS• KingAOP: https://github.com/AntyaDev/KingAOP• Simple.Data: https://github.com/markrendle/Simple.Data• Massive: https://github.com/robconery/massive• CarealBox: https://github.com/JonCanning/CerealBox• Oak: http://amirrajan.github.io/Oak/
  27. 27. https://twitter.com/AntyaDevhttps://github.com/AntyaDevhttp://antyadev.blogspot.com/THANK YOU!

×