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")]