M cđ chun hóa cao vi tính nng này tuy cơng ngh laszlo phát trin mi cho th h web hin đi nh ng đi v i ngi lp trình s làm quen nhanh chóng d a
Hình 7 Cu trúc cây – Nút con
HU
TE
CH
C u trúc cây là khái ni m quan tr ng đ c s d ng trong CodeDOM. M i m t đ i t ng trong mã ngu n đ c bi u di n nh m t nút c a cây.
Xây d ng đ th CodeDOM
HU
TE
CH
SV Khóa 2001 – Khoa Cơng Ngh Ph n M m Page 28 of 80 using System; using System.Drawing; using System.Collections; using System.ComponentModel; CodeNamespaceImport namespace SamplesNamespace { } CodeNamespace
public class Samples : System.Windows.Forms.Form {
}
CodeTypeDeclaration
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null; private int _numOfStep;
CodeMemberField public Samples() { } get { return this._numOfStep;} public virtual int NumOfStep { } this.InitializeComponent(); CodeConstructor CodeStatement CodeMemberProperty set {this._numOfStep = value;} CodeStatement CodeStatement Hình 8 Kh o sát mã ngu n đ t o đ th CodeDOM
HU
TE
CH
Trong hình trên, ta thy m i thành ph n mã ngu n đ c CodeDOM bi u di n b ng m t l p t ng ng. Namespace đ c bi u di n b i l p CodeNamespace, l p đ c bi u di n b ng CodeTypeDeclaration, hàm dng là CodeConstructor, các
câu l nh là CodeStatement, khai báo các bi n c a l p là CodeMemeberField, …
C u trúc đ th CodeDOM có th đ c minh h a nh sau: CodeCompileUnit CodeNamespace Class CodeNamespaceImport CodeT ypeDeclaration Struct Interface Enumeration CodeT ypeMember Consrtuctor Method Field Property Event CodeStatement Hình 8 C u trúc đ th CodeDOM
HU
TE
CH
SV Khóa 2001 – Khoa Cơng Ngh Ph n M m Page 30 of 80
3.2.2 S d ng CodeDOM
Sinh mã ngu n trang Code-behind
sinh mã ngu n cho trang Code-behind, ta c n t o m t đ th CodeDOM và s d ng hai namespace sau:
System.CodeDom đ nh ngh a các ki u (type) dùng đ trình bày c u trúc logic c a mã ngu n, không ph thu c m t ngơn ng l p trình c th nào.
System.CodeDom.Compiler đ c s d ng đ sinh ra mã ngu n t đ th CodeDOM và biên dch mã ngu n (h tr các ngôn ng C#, JScript, Vb.NET) G c c a đ th CodeDOM là CodeCompileUnit, bi u di n cho m t project hay m t assembly. T t c m i thành ph n mã ngu n khác đ u ph i đ c liên k t v i nhau và có g c chung là CodeCompileUnit.
Minh h a cách s d ng CodeDOM đ sinh mã ngu n: (adsbygoogle = window.adsbygoogle || []).push({});
T o đ th CodeDOM:
//T o g c c a đ th
CodeCompileUnit unit = new CodeCompileUnit();
//T o using namespace
CodeNamespace codeUsing = new CodeNamespace();
codeUsing.Imports.Add(new CodeNamespaceImport("System"));
//T o namespace đ ch a l p
CodeNamespace samplesNamespace = new CodeNamespace("SamplesNamespace");
//Nút con namespace đ c thêm vào g c
unit.Namespaces.Add(codeUsing);
unit.Namespaces.Add(samplesNamespace);
HU
TE
CH
CodeTypeDeclaration myClass = new CodeTypeDeclaration ("Samples"); type1.IsClass = true;
type1.BaseTypes.Add("System.Windows.Forms.Form");
samplesNamespace.Types.Add(type1);
//Thêm khai báo bi n vào l p CodeMemberField button = new
CodeMemberField(typeof(System.Windows.Forms.Button), "button1");
myClass.Members.Add(button);
//Thêm hàm d ng vào l p
CodeConstructor constructor = new CodeConstructor(); constructor.Attributes = MemberAttributes.Public;
myClass.Members.Add(constructor);
//Thêm ph ng th c InitializeComponent vào l p
CodeMemberMethod initMethod = new CodeMemberMethod(); initMethod.Name = "InitializeComponent";
myClass.Members.Add(initMethod);
//G i ph ng th c InitializeComponent trong hàm d ng
constructor.Statements.Add(new CodeMethodInvokeExpression(
new CodeThisReferenceExpression(), "InitializeComponent"));
Sinh mã ngu n: (adsbygoogle = window.adsbygoogle || []).push({});
Sau khi đã có CodeCompileUnit, cn t o m t CodeDomProvider đ phát sinh mã ngu n. N u mu n sinh mã C#, ta chn CsharpCodeProvider, hay ch n VBCodeProvider đ sinh mã VB.NET.
CodeDomProvider provider;
if (language == "CSHARP")
provider = new CSharpCodeProvider();
HU
TE
CH
SV Khóa 2001 – Khoa Cơng Ngh Ph n M m Page 32 of 80 provider = new VBCodeProvider();
StringWriter sw = new StringWriter();
ICodeGenerator generator= provider.CreateGenerator(); generator.GenerateCodeFromCompileUnit(unit, sw, new CodeGeneratorOptions());
Mã ngu n đ c sinh ra nh sau:
C#
using System;
namespace SamplesNamespace
{
public class Samples : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
public Samples()
{
this.InitializeComponent();
}
private void InitializeComponent()
{ } } }
}
VB.NET
Option Strict Off Option Explicit On Imports System
Namespace SamplesNamespace
HU
TE (adsbygoogle = window.adsbygoogle || []).push({});
CH
Inherits System.Windows.Forms.Form
Private button1 As System.Windows.Forms.Button Public Sub New()
MyBase.New()
Me.InitializeComponent() End Sub
Private Sub InitializeComponent() End Sub
End Class
End Namespace
3.2.3 ánh giá gi i pháp
u đi m
. D a vào c u trúc cây đ phát sinh mã ngu n, vì v y mã ngu n đ c sinh ra có th b t kì m t ngơn ng nào (C#, VB.NET…) n u ngơn ng này có h tr đ c t CodeDOM.
. Các ch ng trình đ c t o ra, biên d ch và th c thi đ ng th i gian ch y.
Cho phép bi u di n c u trúc mã ngu n trong b nh b ng mơ hình các đ i t ng đ c l p v i ngơn ng l p trình.
. Trong t ng lai, có th m ra kh n ng “d ch” mã ngu n đ c vi t b ng ngôn ng này sang ngôn ng khác. (Ch ng h n mã ngu n vi t b ng C# đ c “d ch” ra thành d ng vi t b ng VB.NET).
H n ch
Ch a h tr nh ng tr ng h p sau: 1.
HU
TE
CH
SV Khóa 2001 – Khoa Cơng Ngh Ph n M m Page 34 of 80 {
public void MyClass {
} } }
namespace MyNamespace2 {
using MyAlias = MyNamespace.MyClass;
public void MyClass2 : MyAlias { } } 2. namespace OutterNamespace { namespace InnerNamespace {
public void MyClass {
} } } }
3. Khai báo danh sách bi n namespace MyNamespace {
public void MyClass {
public void MyClass() { (adsbygoogle = window.adsbygoogle || []).push({});
int i,j,k;
} } } }
4. khái ni m "unsafe" trong C#. class MyUnsafeClass
{
unsafe static void MyPointerFn(int* ptr)
{
*p *= *p; }
unsafe public static void main()
{
//unsafe operation using address-of operator &
int i = 10; MyPointerFn(&i);
HUTE