DLR and reflection

Everywhere I read about the new DLR in .net 4, they say that it’s useful to use reflection for it, and the code fragment is always shown as

dynamic d = GetSomeObject();
d.DoSomething();
d.SomeMember = 1;

What does it look like GetSomeObject()? I cannot find anywhere that explains this.

I understand that it can be anything, but in the context of thinking, what is it? Is this an assembly? type instance?

+3
source share
4 answers

The return type GetSomeObject () will be an instance of some type. For example, here is what it might look like:

public Customer GetSomeObject() {
    return new Customer("John", "Doe", 12345);
}

And then the code will say:

dynamic customer = GetSomeObject();
string s = customer.FirstName;
// now the "s" variable would have "John" in it

GetSomeObject() . Customer Product. ! , dynamic, , , , Reflection, . , . , .

Reflection, .

, Customer Product IDynamicObject, .

+3

, , - dynamic ( ). , :

  • (IDynamicObject)
  • ,

( , ) ; COM-, , ( Silverlight) html DOM. Customer , - , InternalCustomer. , IronPyton.

+1

, GetSomeObject() , , _ComObject. , dynamic.

0

, , , DLR , , 2. :

dynmic,   dynamic d = GetSomeObject();   d.DoSomething();

var d = GetSomeObject();
var mi = d.GetType().GetMethod("DoSomething");
mi.Invoke(d,mi);

As I can see, the first one is more elegant, and we talk about the method less, everything can go crazy when you interact with COM or API with long signature methods. I was there;)

0
source

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


All Articles