Lazy use of dynamic when writing code today, results encountered problems, the runtime always cannot get the properties of the dynamic object. The original problem is simplified as follows:
Assembly A contains the SampleClass class, with a static method that receives a dynamic type parameter and outputs its Value property.
Public class sampleclass{ publicstaticvoid Output (dynamic result) { Console.WriteLine (Result. Value);} }
Assembly B refers to assembly A and calls the output method:
Sampleclass.output (new {Value = something. CurrentValue});
The operating result is:
"Unhandled Exception:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ' object ' does not contain a definition for ' Value ' "
And when you debug, you can see the value of result in the Values property:
With reflection you can also see that there is a property of value, but this is not the output, what is the reason?
------------------------------------------
A: Because the anonymous type is private by default, the anonymous type defined in the B assembly cannot be exported because the a assembly cannot directly obtain its member information. The workaround is to pre-define the data type of public. So there are many restrictions on the use of anonymous types, and similar problems occur on anonymous methods.
C # 4.0 do not use dynamic to point to anonymous types across assemblies (reprinted)