C # dynamic type error: "object" does not contain the definition of "xxx,

Source: Internet
Author: User

C # dynamic type error: "object" does not contain the definition of "xxx,

I. cause:

Recently, I am working on a project because the same process is used in many places. For later maintenance, the shared process is required to be detached and created as a public class library. Before the extraction, the program runs normally. However, after the extraction, you get the following error: the object does not contain the definition of xxx. When this error occurs, it is quite depressing. It is simply an inexplicable and all the same code. It is called in the form of a class library.

 

2. Search:

You have to solve the problem. The next step is to debug the problem. After debugging, you are confused. Let's take a look at the two below. When I saw the error in Figure 1, I immediately monitored products. The result is shown in figure 2 (OH, isn't there an IsOk attribute clearly? What a ghost ...). Because there was no idea at all and there was no way to start, we started programming for Baidu and found the cause of this bug.

(Figure 1)

 

(Figure 2)

 

Iii. solution:

It turns out that the anonymous type is compiled, and the access modifier is at the internal level. When cross-assembly access occurs, an error occurs (apiTL. ProductList () returns an anonymous type ). This is why the program runs normally before being detached (because it is in the same set of programs), and an error is reported after being detached. If you know the reason, you can solve it.

 

Example:

Assembly A (the Assembly where the anonymous object is located); Assembly B (call the method of assembly A to receive anonymous objects ). You only need to add the following statement to AssemblyInfo. cs of Assembly A, which means to be visible to the specified assembly (BOTH: visible to assembly B)

[Assembly: InternalsVisibleTo ("assembly B")]

 

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.