C # 4.0 Dynamic Functions

What is the meaning of a dynamic class on which you can call methods that may or may not be there?

+6
c #
source share
6 answers

The fact is that you will usually be sure that this method will be present (or processed dynamically - for example, the FindByAuthor method in the "book repository" class, which is translated into the corresponding SQL query), but you donโ€™t know the static type - or where the interfaces are fairly weakly typed (e.g. API COM COM).

I would not expect dynamic typing to be used very often in C #, but when it is convenient, I suspect that it will be very, very convenient.

+4
source share

First of all, it allows C # 4 to interact much better with objects provided by DLR, using languages โ€‹โ€‹such as Python. It also makes it much easier to interact with typical COM objects without having to create interop assemblies.

+10
source share

One reason is to simplify communication with COM using late binding. That way, you no longer need to use interop assemblies.

This is very nice if you need to call a different version of the COM server. For example, when you need an application to work with different versions of Office.

+3
source share

Of course, I would not use his invasion scripts. When working with assemblies or code written in dynamic languages, this simplifies design time. In these cases, you should still make assumptions about dynamic types. You will get your runtime exception if your assumptions fail anyway.

Think of it as a shorthand for Invoke or reflection.

+1
source share

Another problem may be writing code that should work with some versions of the assembly. Tell the plugin in which assembly bindings are not defined. And now you will need to compile the same source code for several versions of the host assembly.

In this case, a function like duck printing would be a good solution.

+1
source share

My 2 cents: a very useful dynamic -based script is an implicit interface. Take, for example, EndianBinaryReader , which can be used instead of BinaryReader , but these two classes do not share a common interface. Yes, you can also make such an interface and wrap these classes, however, which will not scale (if for some reason you got a third with such an interface, you also have to wrap it). Implicit interfaces (duck printing) are a useful feature in dynamic languages, using dynamic you can also have it in C #.

0
source share

All Articles