C # 4.0 do not use dynamic to point to anonymous types across assemblies (reprinted)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.