Newly Edited content (14:49:00)
According to my friend doylecnn on the first floor, I tried again and found that VS2012 had the problem. It was probably a BUG. It seems that new things cannot be used in disorder.
In VS2010, Mono in Mac is normal. It has nothing to do with the operating system.
Why does VS2012 have this problem? . NET 4 does not use. NET 4.5. But when VS2010 is used for compiling, does VS2012 have different results?
I have uploaded the Compilation Program and source code. If you are interested, you can download and play ......
Click to download
Today, I encountered a very strange problem. I wrote a component and asked to input an Action object. So my colleague introduced an anonymous function when calling it, as shown below:
Caller. Process () => {
Base. Delete <News> (id );
});
An exception occurs after running: The type parameter "TEntity" conflicts with the type parameter "TEntity" constraints.
For the first time in many years, after tracking and debugging, the following conditions are met:
1. The method to be called must be a virtual method in the parent class ).
2. in the subclass, you must use a managed function to directly call the base of the parent class. func () virtual method. If the subclass overwrites (override), then base is used in overwrites. no problem with Func.
3. The virtual method of the parent class is generic and must have generic constraints. No constraints will cause problems.
I wrote a simple sample code:
1 using System;
2 using System. Collections. Generic;
3 using System. Linq;
4 using System. Text;
5
6 namespace VirtualFunction
7 {
8 class Program
9 {
10 static void Main (string [] args)
11 {
12 SubClass sub = new SubClass ();
13 sub. CallFunc ();
14 Console. ReadKey ();
15}
16}
17
18 public abstract class SuperClass
19 {
20 public virtual void Func <TEntity> ()
21 where TEntity: class
22 {
23 Console. WriteLine ("super class's function ");
24}
25
26 public void NonVirtualFunc <TEntity> ()
27 where TEntity: class
28 {
29 Console. WriteLine ("super class's none virtual function ");
30}
31}
32
33 public class SubClass: SuperClass
34 {
35 public delegate void Caller ();
36
37 /// the virtual method of the parent class is overwritten here, but it is called in Different calling methods below. The actual test result shows whether the overwriting method works the same. The exception is still abnormal.
38 public override void Func <TEntity> ()
39 {
40 base. Func <TEntity> ();
41}
42
43 public void CallFunc ()
44 {
45 // use the common method to call Func of the parent class
46 base. Func <string> ();
47
48 // use a proxy to call the Func method of the parent class. Note that the Child class does not overwrite the Func method of the parent class.
49 Action action = new Action () =>
50 {
51 base. NonVirtualFunc <string> (); // no exception occurs
52 this. Func <string> (); // no exception occurs
53 base. Func <string> (); // exception
54 });
55 action ();
56
57 // another managed call method with the same effect as the above call through Action
58 Caller caller = () =>
59 {
60 base. NonVirtualFunc <string> ();
61 base. Func <string> ();
62 };
63 caller ();
64}
65}
66}
No reason has been found for the query of a large amount of information. Who can answer this question?