Post navigation

What is the difference between Reflection and dynamic keyword in C#?

Reflection and the dynamic keyword. Many developers get confused between them because both of them help us to do dynamic invocation.

What is Reflection and why we need it?

Reflection is needed when you want to determine / inspect contents of an assembly. For example look at your Visual Studio editor intellisense, when you type “.” (dot) before any object, it gives you all the members of the object. This is possible because of Reflection.

Reflection also goes one step further; it can also invoke a member which is inspected. For instance if Reflection detects that there is a method called GetChanges in an object, we can get a reference to that method instance and invoke it on runtime.

In simple words Reflection passes through two steps: “Inspect” and “Invoke” (optional). The “Invoke” process is optional.

How do we implement Reflection?

Implementing reflection in c# is a two step process , 1st get the “type” of the object and then use the type to browse members like “methods” , “properties” etc.

Step 1: The first step is to get the type of the object. So for example you have a DLL ClassLibrary1.dll which has a class called Class1. We can use the Assembly (belongs to the System.Reflection namespace) class to get a reference to the type of the object. Later we can use Activator.CreateInstance to create an instance of the class. The GetType() function helps us to get a reference to the type of the object.

What is the use of the dynamic keyword?

Programming languages can be divided into two categories: strongly typed and dynamically typed. Strongly typed languages are those where checks happen during compile time while dynamic languages are those where type checks are bypassed during compile time. In a dynamic language object types are known only during runtime and type checks are activated only at runtime.

We would like to take advantage of both worlds. Because many times we do not know the object type until the code is executed. In other words we are looking at something like a dynamically and statically typed kind of environment. That’s what the dynamic keyword helps us with.

If you create a variable using the dynamic keyword and if you try to see members of that object, you will get a message as shown below “will be resolved at runtime”.

Now try the below code out. In the code I have created a dynamic variable which is initialized with string data. And in the second line I am trying to have fun by trying to execute a numeric incremental operation. So what will happen now? Think….

Now this code will compile fine without any complaints. But during runtime it will throw an exception complaining that the mathematical operations cannot be executed on the variable as it’s a string type. In other words during runtime the dynamic object gets transformed from the general data type to a specific data type (e.g.: string for the below code).

What are the practical uses of the dynamic keyword?

One of the biggest practical uses of the dynamic keyword is when we operate on MS Office components via interop.

So for example if we are accessing Microsoft Excel components without the dynamic keyword, you can see how complicated the code gets. Lots of casting happening in the below code, right?

// After the introduction of dynamic, the access to the Value property and
// the conversion to Excel.Range are handled by the run-time COM binder.
dynamic excelApp = new Application();
excelApp.Cells[1, 1].Value = "Name";
Excel.Range range2010 = excelApp.Cells[1, 1];

What is the difference between Reflection and dynamic?

Both Reflection and dynamic are used when we want to operate on an object during runtime.

Reflection is used to inspect the meta-data of an object. It also has the ability to invoke members of an object at runtime.

dynamic is a keyword which was introduced in .NET 4.0. It evaluates object calls during runtime. So until the method calls are made the compiler is least bothered if those methods / properties exist or not.