To create a macro code builder with Visual Studio 2005

Source: Internet
Author: User
Tags generator visual studio

The current software is not automatically generated, but we all know that the software will be developed over the generations. So why don't we try to generate software automatically? Why not work hard to create software that can grow or develop for generations, but instead work on changing existing ones? What technologies do we now have that can support the software that is generated and what technologies are forgotten? The answers to these questions may not yet exist, but the VS 2005 already has some capacity to support the generated code.

In Visual Studio 2005, you can use macros to write code for you. These macro code generators are not randomly generated, and you don't have to imagine and invent what macros to write. The two independent but still connected areas of research-design patterns and refactoring-explicitly provide many options for writing understandable, code generators with good documentation (code generation is not the same as the generated code, which you must understand when you use the macro engine). This article will show you how to use the macro engine in Visual Studio 2005 to create a code generator to implement refactoring encapsulate fields for vb.net.

Second, refactoring: encapsulate the field

Refactoring is a defining process for improving the quality of code implementation. Specifically, refactoring is to improve the quality and performance of software by adjusting the program code without changing the existing function of the software, to make the program design mode and architecture more reasonable, and to improve the expansibility and maintainability of software. In the simplest sense, refactoring extracts some subjectivity from the code. As software engineers, we don't have to rely on public opinion and willpower to judge whether coding is good or bad; we can use an objective standard and agree that refactoring code is better than non refactoring code.

Like design patterns, refactoring is a named code pattern with full descriptions, instructions, and results to be achieved. Any programmer, regardless of development experience, can read the description and use predetermined instructions as they would follow a doctor's prescription and make predictable progress.

A refactoring example is called a wrapper field. Encapsulating a field means making the field name private and restricting access to those fields through a public property method. Access to the state of one object is better than unrestricted access, encapsulation fields are based on the value of believing in restricted access to data (some people may not agree with the basic premise that constrained access is better than free access--and some people think that object-based design is not much of a benefit, Not to argue whether refactoring is benign or not. However, this article assumes a benign refactoring.

Third, the implementation of macro

If you are in the context of a C # project in Visual Studio 2005, there must be a refactoring menu. In the context of the vb.net project, there is no such menu (at least for the current Beta 2 release). However, you can easily emulate this support behavior for vb.net-for example, by writing a lightweight code generator to implement encapsulate fields (or other refactoring).

To implement the Encapsulate field, automate the following steps:

1. Select a field that does not require a consistent property method.

2. Change the access modifier of the field to private.

3. Change the name of the field slightly to avoid a property conflict (use any habit you like).

4. Generate the Getter/setter property method and some code has provided access to the field.

Tip: A good way to start working with macros is to open the macro recorder, complete a task, and then analyze which macro statements are generated in the integrated development environment. Then, a further generalization of the recorded macro.

Related Article

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.