Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of converting interfac to class class?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "what is the method of conversion between interfac and class class". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "what is the method of conversion between interfac and class class" can help you solve the problem.

The interface is the standard of the class (thing), which defines the methods and properties of the class (thing). If a class (thing) wants to implement an interface, it must implement its methods and properties. We can simply understand that for multiple classes (things), the interface not only provides a standard for these classes (things), but also restricts the relationship between them, which is equivalent to a contract.

Our usual practice is that a class implements an interface-"instantiate the class -" converts an instance of the class into an implemented interface-"operating through an interface is basically a process where you can rest assured that you can use methods and properties that are known to the interface without worrying about calling errors, and you don't have to worry about how the code for the method is implemented. However, we can think about a question: an interface is equivalent to a framework and has no physical content (specific code). Can an instance of a class be converted into an interface instance in reverse? If you don't think about it in your head, the answer is no. But what is the truth? Take a look at the results of the analysis:

The following code implements the dynamic addition of a custom control to a form.

/ / the ControlLibrary.UserControl1 control implements the ControlLibrary.IValue interface, and there are only SetValue methods 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 an interface variable

ControlLibrary.UserControl1 c = (ControlLibrary.UserControl1) _ value

UserControl C1 = (UserControl) _ value

This.Controls.Add (c)

The blue font is the main part, and this code ControlLibrary.UserControl1 c = (ControlLibrary.UserControl1) _ value (converting the interface to a custom control) is not possible and violates the basic rules of writing programs.

However, after analysis, the above practice is possible, and an instance of an interface converted into a custom control can be added to the form. Take a look at a decompiled code:

Figure 1: an instance will be returned when the control is created, and there will also be an instance address. Ebp-4ch is a pointer address.

Figure 2: when the created instance is converted into an interface, a new pointer address is assigned, and the original ebp-4ch becomes ebp-3ch.

Figure 3: convert the interface instance to a custom control instance, and the pointer address is still ebp-3ch. This proves that the interface instance and the translated control instance share the same address.

Figure 4: it is possible to add the converted control instance to the form. This address is still ebp-3ch.

The address of the C1 instance is also ebp-3ch. The above analysis shows that after the class instance is converted into an interface instance, the interface instance can be converted in reverse.

This is the end of the content about "what is the method of converting interfac to class class". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report