The advantage of using dynamic type in C #

For example, if an instance method exampleMethod1has only one parameter, the compiler recognizes that the first method call, ec. exampleMethod1(20, 4)is invalid because it contains two arguments.

The call causes a compiler error. The second method call is dynamic_ec.exampleMethod1(10, 4)not checked by the compiler, because the type dynamic_ecis dynamic. Therefore, no compiler error is reported.

So what is the difference between checking compile time and checking runtime and the advantage of using a dynamic type?

+4
source share
2 answers

, python, javascript.

, . , , , . , .

PDC08, :

object calc = GetCalculator();
Type calcType = calc.GetType();
object res = calcType.InvokeMember(
  "Add", BindingFlags.InvokeMethod, 
  null, new object[] { 10, 20 });
int sum = Convert.ToInt32(res);

, . , , , Add.

dynamic , :

dynamic calc = GetCalculator();
int sum = calc.Add(10, 20);

Vs

+3

, ( api whi h is really slow) . , , , . , , .

0

Source: https://habr.com/ru/post/1525157/


All Articles