Skip to content

Commit 563820d

Browse files
Bart KoelmanBart Koelman
authored andcommitted
Rephrased AV2230 to provide more context and suggestions
1 parent e540ca5 commit 563820d

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

_rules/2230.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ rule_category: dotnet-framework-usage
44
title: Only use the `dynamic` keyword when talking to a dynamic object
55
severity: 1
66
---
7-
The `dynamic` keyword has been introduced for working with dynamic languages. Using it introduces a serious performance bottleneck because the compiler has to generate some complex Reflection code.
7+
The `dynamic` keyword has been introduced for interop with languages where properties and methods can appear and disappear at runtime. Using it can introduce a serious performance bottleneck, because various compile-time checks (such as overload resolution) need to happen at runtime, again and again on each invocation. You'll get better performance using cached reflection lookups, `Activator.CreateInstance()` or pre-compiled expressions (see [here](https://andrewlock.net/benchmarking-4-reflection-methods-for-calling-a-constructor-in-dotnet/) for examples and benchmark results).
88

9-
Use it only for calling methods or members of a dynamically created instance class (using the `Activator`) as an alternative to `Type.GetProperty()` and `Type.GetMethod()`, or for working with COM Interop types.
9+
While using `dynamic` may improve code readability, try to avoid it in library code (especially in hot code paths). However, keep things in perspective: we're talking microseconds here, so perhaps you'll gain more by optimizing your SQL statements first.

0 commit comments

Comments
 (0)