The document discusses dynamic programming in C# and compares static and dynamic languages. It provides examples of how dynamic features like method missing in Ruby can be achieved in C#. It also summarizes the Dynamic Language Runtime (DLR) and how it allows multiple languages to run on the Common Language Runtime (CLR). Key people who worked on DLR implementations for languages like IronRuby are mentioned.
3. The many faces of “dynamic”
Generate code
at runtime
Changing types
at runtime
No types
at all
Simplified
deployment
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 data
IT’S A DYNAMIC WORLD
10. Action Python Ruby C# VB.NET
GetMember x.Foo x.Foo x.Foo x.Foo
SetMember x.Foo = y x.Foo = y x.Foo = y x.Foo = y
DeleteMember del d.Foo
x.send
:remove_instance_variable
:@foo
No syntax No syntax
UnaryOperation -x -x -x -x
BinaryOperation x + y x + y x + y x + y
Convert 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 syntax
GetIndex 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) = y
DeleteIndex del x[a,b] No syntax No syntax No syntax
Common actions
12. DynamicMetaObject
public 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. System.Dynamic.DynamicObject
public class DynamicObject : IDynamicMetaObjectProvider
{
public virtual bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out
object result);
public virtual bool TryConvert(ConvertBinder binder, out object result);
public virtual bool TryCreateInstance(CreateInstanceBinder binder, object[] args, out
object result);
public virtual bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object
result);
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, out
object result);
public virtual bool TrySetIndex(SetIndexBinder binder, object[] indexes, object
value);
public virtual bool TrySetMember(SetMemberBinder binder, object value);
}
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
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 strongly
typed as
possible
Cache is learning
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. DynamicMetaObject
public 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…
}