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.