C# Reference ยท Reflection, Attributes and Metadata
dynamic and the DLR
Reference for C# dynamic binding and the Dynamic Language Runtime: ExpandoObject, DynamicObject, runtime binder errors, interop uses and performance costs.
The Dynamic Language Runtime (DLR) is the library layer behind the dynamic keyword. When an expression involves a dynamic value, the compiler emits a call site instead of checking types. At run time the DLR asks the object how to handle the operation: ordinary .NET objects are bound with reflection rules by the C# runtime binder, while objects that implement IDynamicMetaObjectProvider (such as ExpandoObject and anything derived from DynamicObject) decide for themselves.
This page is about the runtime side: building dynamic objects and the costs. The language keyword itself is covered in dynamic.
dynamic bag = new ExpandoObject(); bag.Title = "Report"; // members created on the fly Console.WriteLine(bag.Title);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
dynamic d = value; // static type is dynamic, run-time type is whatever it holds class Bag : DynamicObject { /* overrides */ }
| Type or member | Description |
|---|---|
ExpandoObject |
Property bag; implements IDictionary<string, object?> and INotifyPropertyChanged. |
DynamicObject |
Base class; override TryGetMember, TrySetMember, TryInvokeMember, TryBinaryOperation, TryConvert, TryGetIndex and others. Return true if you handled it. |
IDynamicMetaObjectProvider |
The low-level interface the DLR actually uses. |
CSharpArgumentInfo, Binder |
Runtime binder pieces used by compiler-generated call sites. |
Errors: RuntimeBinderException when no member or overload fits (message such as 'int' does not contain a definition for 'NoSuchMember'); if a DynamicObject override returns false, the same exception is thrown.
Usage notes
- No compile-time safety. Typos in member names are found only when that line runs. Use
dynamicin small, well-tested spots (COM, scripting, JSON-like data), not as a general-purpose type. - Performance: the first execution of a call site is slow (it builds a binder and compiles a rule); later calls with the same types use a cache and are fast but still slower than static calls.
- Extension methods and lambdas do not work on
dynamicreceivers. Call the static method directly or cast. - Interop: COM objects (Office), IronPython/other DLR languages and reflection-heavy code were the original motivation;
dynamicremoves a lot ofInvokeMembernoise. - Dynamic results stay dynamic.
var z = x + 1;makeszdynamic too; assign to a typed variable as soon as possible. - Native AOT: the C# runtime binder needs run-time code generation, so
dynamicis not supported there. See Native AOT and trimming. - Alternative: a
Dictionary<string, object?>or a typed record is usually clearer thanExpandoObject.