An interface is the standard of a class (thing). It specifies the methods and attributes of a class (thing. to implement an interface, a class (thing) must implement its methods and attributes. we can simply understand a single class (thing). For Multiple classes (things), interfaces not only provide a standard for these classes (things, it also imposes constraints on the relationship between them, which is equivalent to a contract.
Our common practice is: class implementation interface-"instantiate the class-" convert the class instance to the implemented interface-"and perform operations through the interface. This is basically the process, you can use methods and attributes that are known to the interface without worrying about call errors, and you don't have to worry about how the method code is implemented. However, we can think about the following question: an interface is equivalent to a framework and has no entity content (specific code). Can a class instance be converted back to an interface instance after being converted to an interface instance? The answer is no if you don't have to worry about it. But what is the truth? The analysis result is as follows:
The following code dynamically adds a custom control to a form.
// The ControlLibrary. UserControl1 control implements the ControlLibrary. IValue interface, with only the SetValue method in it
Assembly assembly = Assembly. LoadFrom ("ControlLibrary. dll ");
Type t = assembly. GetType ("ControlLibrary. UserControl1 ");
MethodInfo me = t. GetMethod ("InitControl ");
Object obj = Activator. CreateInstance (t );
ControlLibrary. UserControl1 ctrl = (ControlLibrary. UserControl1) me. Invoke (obj, new object [] {10 });
_ Value = (ControlLibrary. IValue) ctrl; // _ value is the interface variable
ControlLibrary. UserControl1 c = (ControlLibrary. UserControl1) _ value;
UserControl c1 = (UserControl) _ value;
This. Controls. Add (c );
The blue font is the main part. It is reasonable to say that the Code ControlLibrary. userControl1 c = (ControlLibrary. userControl1) _ value (converting an interface into a custom control) is not feasible, and violates the basic rules for writing programs.
However, after analysis, the above practices may be implemented, and you can convert the interface into an instance of a custom control and add it to the form. Please refer to a decompiled code:
Figure 1: An instance is returned when the control is created, and an instance address is returned. The ebp-4ch is a pointer address.
Figure 2: When the created instance is converted to an interface, a new pointer address is assigned, and the original ebp-4ch becomes the ebp-3ch
Figure 3: convert an interface instance to a custom control instance, and the pointer address is still a ebp-3ch. This proves that the interface instance shares an address with the converted control instance.
Figure 4: add the converted control instance to the form. This address is still a ebp-3ch.
The address of the c1 instance is also a ebp-3ch. The above analysis shows that the class instance can be converted back to the interface instance after being converted to the interface instance.
Source: [hundreds of trees] C # interface [interface] and instances in the class [class] convert each other for in-depth analysis
Http://www.bmpj.net/forum-viewthread-tid-195-fromuid-15.html
The QQ Group publishes the group number.