Visual Basic 9.0 cutting-edge broadcast-dynamic Article (2) dynamic interfaces

Source: Internet
Author: User

When talking about the differences between dynamic and Static languages, there is a classic saying: Static typing when possible, dynamic typing when needed. Visual Basic supports post-binding and dynamic identifiers. Therefore, you can perform operations based on the object runtime type. Only later binding or dynamic identifiers do not have constraints on the type of parameters or return values. They only operate according to the member name. For example, in the static article, we have introduced the anonymous type. Suppose there are three anonymous instances (Tuple ):

Dim a = New {Name: = "Robbins", Age: = 25}
Dim B = New {Name: = "Andrei", ID: = 12, Address: = "Beijing "}
Dim c = New {Name: = New NameType ("Mark"), Title: = "PM "}

As you can see, the three Tuple have different types. Since the anonymous type cannot implement interfaces, there is no way to operate the same attribute Name of a and B in a unified syntax. You can use later binding to access the Name attribute in a unified manner. However, the Name attribute of c is different from that of a and B, but later binding allows them to access the attribute in a unified manner, this expands the potential risks. Is there some strong type constraints that allow dynamic access to members? Visual Basic 9.0 introducesDynamic Interface. Dynamic interfaces are different from common interfaces. They do not need to be declared to implement dynamic interfaces. As long as they have Members defined by the corresponding dynamic interfaces, they can be converted to dynamic interface types. This feature is also calledDuck type determination. It is because of the spread of such a dynamic language-type Judgment Method: If a thing is walking like a duck, talking like a duck, then it is a duck. The Dynamic interface must be defined using the Dynamic modifier:

Dynamic Interface IHasName
Property Name As String
End Interface

This interface specifies a String type attribute named Name. Any type of instance, as long as it has a String type attribute Name, is assumed to implement IHasName:

Dim a = New {Name: = "Robbins", Age: = 25}
Dim B = New Button () {Name: = "Button1", Text: = "Button1 "}

Dim ihn As IHasName =
Ihn. Name = "Haward"
Ihn = B
Ihn. Name = "Button2"

Although neither the anonymous type nor the Button type of a is declared to implement the IHasName interface, they all have the property of the String type Name, so you can use the IHasName interface. Dynamic interfaces provide later binding with type constraints and IDE smart sensing, which improves the Dynamic Programming capability of Visual Basic. However, it should be noted that dynamic interfaces are not a type-safe feature.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.