How is VB.NET to do (five, six)

zhaozj2021-02-17  55

How does VB.NET do (5) - implementation interface

The syntax of VB.NET implements the interface is the imports of VB5 inventions, which is unique in the mainstream language in today's mainstream language. For example, I have two interfaces:

Interface Interface1 Sub Test () End Interface

Interface Interface2 Sub Test () End Interface

These two interfaces have a fully equally members test. Suppose how do I need to implement two interfaces with a class? Think first, if you are java, Jscrip.net can only use a Test function to implement TEST members of the two interfaces. If two TESTs are just accidentally reintegration, their content must be achieved separately, so some designs have been resolved in the design ... In VB, the unique imports statement allows you to achieve how to implement interfaces, such as the following type of IMPLEmentation implements two interfaces with two names.

Public Class Implementation IMPLEments Interface1, Interface2

Public Sub Hello () Implements Interface1.test

End Sub

Private Sub Hi () IMPLEMENTS INTERFACE2.TEST

End Subend Class

That is, VB allows functions in the interface with functions of any name, and the accessor can be arbitrary, such as using public or private.

C # provides an explicit implementation (Explicit Implement) grammar on handling the reverbementation, which implements the syntax of the above two interfaces.

Public class class1: interface1, interface2 {public class1 () {} void interface1.test ()}

Void interface2.test () {}

}

Note that the C # can only name the interface name. The name of the member name is named, and the accessor can only be private and cannot disclose an explicit implementation.

After investigation of IL, I found that .NET supports implicit implementation and explicit implementation. Among them, implicit implementation is as long as it is placed in the class. This type of VB does not support. And explicit implementation is added in the description of the method:

.override testapp.interface1 :: test

Whether it is an explicit implementation of C # or VB's IMPLEMENTS statement is such a principle. That is to say .NET provides the ability to realize the functionality of the interface member, but only VB gives this freedom to the user, and other languages ​​use classic syntax.

How does VB.NET do (6) - Default Properties and Attribute Parameters

In the original VB6, there is a strange function - the default properties. In VB6, the name of the object can directly represent the default properties of the object. For example, the default attribute of TextBox is Text, so the following code

TEXT1.TEXT = "Hello"

It can be simplified to

Text1 = "Hello"

This simplification brings a lot of trouble to VB, and the assignment is required to be two keywords - Let and SET, and the results of the property also require two kinds of LET and SET. And this feature is still working in later binding. In VB.NET, this feature is greatly limited, and now there is only a property with parameters to be used as the default attribute. Such as

List1.item (0) = "Hello"

Can be simplified

List1 (0) = "Hello" syntax allows objects with default properties look like an array. So how does VB determine if an attribute is the default attribute? Look at the following code

Public Class Proptest PUBLIC CLASS PROPTEST

Public Property P1 (Byval Index As Integer) AS String

Get

END GET

Set (byval value as string)

End set

End Property

Default Public Property P2 (Byval Index As Integer) AS String

Get

END GET

Set (byval value as string)

End set

End Property

END CLASS

The two properties of P1 and P2 are substantially identical, and the only difference is that P2 has a default modifier. After disconnecting this class, you can find that the two properties are identical and there is no difference. But the Proptest class has been added a custom meta attribute System.Reflection.defaultmembute. The member specified by this metamorite is the default type used in InvokeMember, that is, the post-binding can also use the default properties. But I tried to add DEFAULTMEMBER meta properties to the type but cannot achieve a feature that makes a property a default attribute. It seems that this feature is a "syntax sweetness" of VB. However, the default attribute of the class generated by the VB or C # to the class generated by others should only be judged by defaultmemberttribute, so I use a VB class to specify a default method with defaultmembute, do not use Default, then compile it to C # Sure enough, C # recognizes it as an indexer!

Since it comes to the C # indexer, we will study the difference between VB and C # properties. The result of the experiment just now is that the default attribute of VB is the indexer in C #. However, VB still can use the syntax of the property to access the default properties, and C # can only access the indexer with the syntax of the array. More particularly, VB can create not the default attribute, but with parameters, such as P1 in the above example, and C # does not support attributes with parameters, if VB is written, including parameter properties, Class C # Use, C # will prompt "The attribute is not supported by the language, please use GET_XXX and SET_XXX Syntax Access". That is, the attribute with the parameters is a function of the CLR, but does not comply with CLS (universal language specification), so there is a cross-language barrier. This also deepens our understanding of CLS - if you want your code to work across language, please pay attention to CLS.

转载请注明原文地址:https://www.9cbs.com/read-30575.html

New Post(0)