CodeSmith practical skills (5): Generate variable code using inheritance

Source: Internet
Author: User
CodeSmith is used to generate variable code. In fact, CodeSmith is used to generate a base class, and other custom classes inherit from this class. When we re-create a base class, CodeSmith should not touch the code in the inherited subclass. See the following template Script: % @ CodeTemplateLanguageC # targetreceivagec # Desc

CodeSmith is used to generate variable code. In fact, CodeSmith is used to generate a base class, and other custom classes inherit from this class. When we re-create a base class, CodeSmith should not touch the code in the inherited subclass. See the following template Script: % @ CodeTemplateLanguage = "C #" TargetLanguage = "C #" Desc

UseCodeSmithGenerate variable code, which is used firstCodeSmithGenerate a base class and customize other classes to inherit from the class. When we recreate a base classCodeSmithDo not touch the code in the inherited subclass. See the following template script:

<%@ CodeTemplate Language="C #"TargetLanguage="C #"Description="Base class generator." %>
<%@ Property Name="ClassName"Type="System. String"Description="Name of the class." %>
<%@ Property Name="ConstructorParameterName"Type="System. String"Description="Constructor parameter name." %>
<%@ Property Name="ConstructorParameterType"Type="System. String"Description="Data type of the constructor parameter." %>
Class <% =ClassName%>
{
<% =ConstructorParameterType%>M _<% =ConstructorParameterName%>;

Public <% =ClassName%>(<% =ConstructorParameterType%> <% =ConstructorParameterName%>)
{
M _<% =ConstructorParameterName%> = <% =ConstructorParameterName%>
}
}

Run the template and enter the following data:

The code generated by this template may be as follows:

1ClassAccount
2{
3 IntM_balance;
4
5 PublicAccount (IntBalance)
6 {
7M_balance=Balance
8}
9
10}
11
12

Save the generated fileAccount. csFile. In this case, we can write the second class to generateCheck. csFile Code:

1ClassChecking: Account
2{
3 PublicChecking:Base(0)
4 {
5}
6}

If you need to changeAccount BalanceThe data type is floating point. We only need to changeConstructorParameterTypeThe property isFloatAnd regenerateAccount. csFile insteadAccount. csAnd does not need to be modified.Check. csAny code of the file.

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.