Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Lang. Runtime vs Reflection

I am planning to use dynamic keyword for my new project. But before stepping in, I would like to know about the pros and cons in using dynamic keyword over Reflection.

Following where the pros, I could find in respect to dynamic keyword:

  • Readable\Maintainable code.
  • Fewer lines of code.

While the negatives associated with using dynamic keyword, I came to hear was like:

  • Affects application performance.
  • Dynamic keyword is internally a wrapper of Reflection.
  • Dynamic typing might turn into breeding ground for hard to find bugs.
  • Affects interoperability with previous .NET versions.

Please help me on whether the pros and cons I came across are sensible or not?

like image 447
AbrahamJP Avatar asked Jan 10 '11 12:01

AbrahamJP


People also ask

What is the difference between reflection and dynamic?

Reflection can invoke both public and private members of an object while dynamic can only invoke public members. dynamic is instance specific: you don't have access to static members; you have to use Reflection in those scenarios.

What is reflections in c#?

Reflection provides objects (of type Type) that describe assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.

What is dynamic programming in C#?

Dynamic programming is new addition to . NET 4.0 and C# 2010. C# 4.0 supports dynamic programming by introducing new dynamic typed objects. The type of these objects is resolved at run-time instead of at compile-time.


1 Answers

Please help me on whether the pros and cons I came across are sensible or not?

The concern I have with your pros and cons is that some of them do not address differences between using reflection and using dynamic. That dynamic typing makes for bugs that are not caught until runtime is true of any dynamic typing system. Reflection code is just as likely to have a bug as code that uses the dynamic type.

Rather than thinking of it in terms of pros and cons, think about it in more neutral terms. The question I'd ask is "What are the differences between using Reflection and using the dynamic type?"

First: with Reflection you get exactly what you asked for. With dynamic, you get what the C# compiler would have done had it been given the type information at compile time. Those are potentially two completely different things. If you have a MethodInfo to a particular method, and you invoke that method with a particular argument, then that is the method that gets invoked, period. If you use "dynamic", then you are asking the DLR to work out at runtime what the C# compiler's opinion is about which is the right method to call. The C# compiler might pick a method different than the one you actually wanted.

Second: with Reflection you can (if your code is granted suitably high levels of trust) do private reflection. You can invoke private methods, read private fields, and so on. Whether doing so is a good idea, I don't know. It certainly seems dangerous and foolish to me, but I don't know what your application is. With dynamic, you get the behaviour that you'd get from the C# compiler; private methods and fields are not visible.

Third: with Reflection, the code you write looks like a mechanism. It looks like you are loading a metadata source, extracting some types, extracting some method infos, and invoking methods on receiver objects through the method info. Every step of the way looks like the operation of a mechanism. With dynamic, every step of the way looks like business logic. You invoke a method on a receiver the same way as you'd do it in any other code. What is important? In some code, the mechanism is actually the most important thing. In some code, the business logic that the mechanism implements is the most important thing. Choose the technique that emphasises the right level of abstraction.

Fourth: the performance costs are different. With Reflection you do not get any cached behaviour, which means that operations are generally slower, but there is no memory cost for maintaining the cache and every operation is roughly the same cost. With the DLR, the first operation is very slow indeed as it does a huge amount of analysis, but the analysis is cached and reused. That consumes memory, in exchange for increased speed in subsequent calls in some scenarios. What the right balance of speed and memory usage is for your application, I don't know.

like image 105
Eric Lippert Avatar answered Oct 13 '22 04:10

Eric Lippert