使用C#编写.Net组件
来源:编程爱好者论坛
namespace CompCS
命名空间的使用非常灵活,它可以被嵌套,也可以将其内部的类分别写在多个文件中,相应地,还可以在一个源文件中声明多个非嵌套的命名空间。下面是一个使用嵌套的命名空间的示例代码:
namespace NestIt{ namespace NestedNameSpace { class myClass { public static void DoSth() { ... } } }}
你可以这样引用类myClass:
NestIt.NestedNameSpace.myClass.DoSth();
还是回到我们的命名空间CompCS,我们使用下面的语句声明了一个类StringComponent:
public class StringComponent
命名空间中的类是必需的,因为C#所有的代码都必须封装在一个个类中,所以没有类的命名空间没有任何价值。
下面我们为这个类添加一个公共(public)域:
private string[] StringsSet;
此外,它还可能需要定义一些属性,下面是定义一个只读的属性的例子:
public int StringLength{ get { return StringsSet.Length; }}
C#中的属性更充分地体现了对象的封装性,不直接操作类的数据内容而是通过访问器进行访问,它借助于get 和set访问器对属性的值进行读写。而在C++中,这是需要程序员手工完成的一项工作。
在属性的访问声明中:
.只有set 访问器表明属性的值只能进行设置而不能读出
.只有get 访问器表明属性的值是只读的不能改写
.同时具有set 访问器和get 访问器表明属性的值的读写都是允许的
你或许会发现域和属性是如此相似,确实属性和域的语法比较类似的,但是它们是绝对不同的,区别在于你不能把属性当做变量那样使用,也不能把属性作为引用型参数或输出参数来进行传递,相反的,对于域,没有这些限制。
下面的代码定义了这个类的构造函数:
public StringComponent()
构造函数必须与类同名,它可以重载,但是不能有返回值,因此它也没有返回值类型前缀。当用户新建一个类的实例时,构造函数就会自动执行,同时,C#的垃圾收集机制开始对这个实例进行管理,并且将在适当的时候回收资源。
然后,我们编写了一个GetString()函数,这个函数根据用户传入的index值,返回相应的记录:
public string GetString(int index)
{ … return StringsSet[index];}
要注意的是其中的异常处理的方法:
throw new IndexOutOfRangeException();
作为一个健壮的组件,异常处理机制是不可或缺的,虽然它可能会消耗掉一些资源,但是它带来的安全性的提升会使你觉得消耗的资源简直微不足道。这里使用了一个系统定义的异常类IndexOutOfRangeException(),事实上,更多的情况是你必须自己定义异常类,以适应各种不同的情况。下面的代码示例展示了如何定义一个异常类:
public class MyApplicationException : ApplicationException{ public string AMsg;
public MyApplicatonException(string strMsg)
{
AMsg=strMsg;
}
}
定义一个异常类与定义普通的类并没有什么区别,唯一的区别在于异常类必须继承自System.Exception类。事实上,微软公司推荐把所有用户自定义的异常类作为ApplicationException类的子类。把类MyApplicationException放到命名空间CompCS中,这样你就可以改写GetString()函数中的异常处理方式。下面是一个带有更完善的异常处理机制的GetString()方法:
public string GetString(int index) { try { if ((index < 0) || (index >= StringsSet.Length)) { throw new MyApplicationException("参数超出范围"); } } catch(MyApplicationException mErr) { Console.WriteLine(mErr.AMsg); } catch(Exception Err) { Console.WriteLine(Err.Message); }
return StringsSet[index];
}
采用类似这样的方式,你可以应付比这复杂得多的情况。
下面,我们来考虑给这个类添加事件。事件机制的引入使得开发者可以更灵活地开发程序。下面的代码示例展示了如何定义一个事件:
public event EventHandler Modified;
在C#中使用event关键字定义事件。把这个定义放到我们的类ComponentCS.StringComponent中,然后我们添加一个函数Modify(),这个函数修改字符数组StringsSet中指定位置的值,同时引发OnModify事件,而在Modify事件中,我们调用的是事件Modified所指定的函数:
public void Modify(int index,string value) { if ((index < 0) || (index >= StringsSet.Length)) { throw new IndexOutOfRangeException(); } else { StringsSet[index]=value; OnModify(); } }
private void OnModify()
{
EventArgs e=new EventArgs();
if(!(Modified==null))
Modified(this,e);
}
然后我们可以用如下的方法调用:
private void DoIt(){ StringComponent mysc=new StringComponent(); mysc.Modified+=new EventHandler(Called); mysc.Modify(2,"another string");}public void Called(object o,EventArgs e){ Console.WriteLine("Changed");}
在函数DoIt()中,我们首先建立了一个StringComponent类的对象mysc,然后将它的Mofidied事件关联到Called()方法:
mysc.Modified+=new EventHandler(Called);
注意“+=”符号的使用,相反地,如果使用“-=”符号,可以取消这个事件的绑定。
现在我们得到了一个虽然简单,但是比较完整的组件类:
将产生的.cs文件编译成为.dll文件的方法如下:
csc.exe /t:library /debug+ /out:myCom.dll example.cs
这样就输出了名为myCom.dll的.dll文件。
OK,我们已经完成一个组件,麻雀虽小,五脏俱全,这就是一切组件的基础了,整个过程花不了十分钟。
当然,如果是一个具备实际使用价值的组件,我们要考虑的远远不止这些,但是可以看到,C#对组件的强大支持,可以大大提高我们的开发效率,从而使我们有更多的精力放在算法设计等方面,开发出更加出色的组件。
1
using System;
2
namespace ComponentCS
3
{
4
public class StringComponent
5
{
6
private string[] StringsSet;
7
public int StringLength
8
{
9
get
10
{
11
return StringsSet.Length;
12
}
13
}
14
public void Modify(int index,string value)
15
{
16
if ((index < 0) || (index >= StringsSet.Length))
17
{
18
throw new IndexOutOfRangeException();
19
}
20
else
21
{
22
StringsSet[index]=value;
23
OnModify();
24
}
25
}
26
public StringComponent()
27
{
28
StringsSet = new string[]
29
{
30
"C# String 0",
31
"C# String 1",
32
"C# String 2",
33
"C# String 3"
34
};
35
}
36
public string GetString(int index)
37
{
38
if ((index < 0) || (index >= StringsSet.Length))
39
{
40
throw new IndexOutOfRangeException();
41
}
42
return StringsSet[index];
43
}
44
}
45
}
一般地,我们首先创建一个命名空间(namespace)用来封装这个组件中一系列的类:
using System; 2
namespace ComponentCS 3
{4
public class StringComponent 5
{6
private string[] StringsSet;7
public int StringLength8
{9
get10
{11
return StringsSet.Length;12
}13
}14
public void Modify(int index,string value)15
{16
if ((index < 0) || (index >= StringsSet.Length)) 17
{18
throw new IndexOutOfRangeException();19
}20
else21
{22
StringsSet[index]=value;23
OnModify();24
}25
}26
public StringComponent() 27
{28
StringsSet = new string[] 29
{30
"C# String 0",31
"C# String 1",32
"C# String 2",33
"C# String 3"34
};35
}36
public string GetString(int index) 37
{38
if ((index < 0) || (index >= StringsSet.Length)) 39
{40
throw new IndexOutOfRangeException();41
}42
return StringsSet[index];43
}44
}45
}namespace CompCS
命名空间的使用非常灵活,它可以被嵌套,也可以将其内部的类分别写在多个文件中,相应地,还可以在一个源文件中声明多个非嵌套的命名空间。下面是一个使用嵌套的命名空间的示例代码:
namespace NestIt{ namespace NestedNameSpace { class myClass { public static void DoSth() { ... } } }}
你可以这样引用类myClass:
NestIt.NestedNameSpace.myClass.DoSth();
还是回到我们的命名空间CompCS,我们使用下面的语句声明了一个类StringComponent:
public class StringComponent
命名空间中的类是必需的,因为C#所有的代码都必须封装在一个个类中,所以没有类的命名空间没有任何价值。
下面我们为这个类添加一个公共(public)域:
private string[] StringsSet;
此外,它还可能需要定义一些属性,下面是定义一个只读的属性的例子:
public int StringLength{ get { return StringsSet.Length; }}
C#中的属性更充分地体现了对象的封装性,不直接操作类的数据内容而是通过访问器进行访问,它借助于get 和set访问器对属性的值进行读写。而在C++中,这是需要程序员手工完成的一项工作。
在属性的访问声明中:
.只有set 访问器表明属性的值只能进行设置而不能读出
.只有get 访问器表明属性的值是只读的不能改写
.同时具有set 访问器和get 访问器表明属性的值的读写都是允许的
你或许会发现域和属性是如此相似,确实属性和域的语法比较类似的,但是它们是绝对不同的,区别在于你不能把属性当做变量那样使用,也不能把属性作为引用型参数或输出参数来进行传递,相反的,对于域,没有这些限制。
下面的代码定义了这个类的构造函数:
public StringComponent()
构造函数必须与类同名,它可以重载,但是不能有返回值,因此它也没有返回值类型前缀。当用户新建一个类的实例时,构造函数就会自动执行,同时,C#的垃圾收集机制开始对这个实例进行管理,并且将在适当的时候回收资源。
然后,我们编写了一个GetString()函数,这个函数根据用户传入的index值,返回相应的记录:
public string GetString(int index)
{ … return StringsSet[index];}
要注意的是其中的异常处理的方法:
throw new IndexOutOfRangeException();
作为一个健壮的组件,异常处理机制是不可或缺的,虽然它可能会消耗掉一些资源,但是它带来的安全性的提升会使你觉得消耗的资源简直微不足道。这里使用了一个系统定义的异常类IndexOutOfRangeException(),事实上,更多的情况是你必须自己定义异常类,以适应各种不同的情况。下面的代码示例展示了如何定义一个异常类:
public class MyApplicationException : ApplicationException{ public string AMsg;
public MyApplicatonException(string strMsg)
{
AMsg=strMsg;
}
}
定义一个异常类与定义普通的类并没有什么区别,唯一的区别在于异常类必须继承自System.Exception类。事实上,微软公司推荐把所有用户自定义的异常类作为ApplicationException类的子类。把类MyApplicationException放到命名空间CompCS中,这样你就可以改写GetString()函数中的异常处理方式。下面是一个带有更完善的异常处理机制的GetString()方法:
public string GetString(int index) { try { if ((index < 0) || (index >= StringsSet.Length)) { throw new MyApplicationException("参数超出范围"); } } catch(MyApplicationException mErr) { Console.WriteLine(mErr.AMsg); } catch(Exception Err) { Console.WriteLine(Err.Message); }
return StringsSet[index];
}
采用类似这样的方式,你可以应付比这复杂得多的情况。
下面,我们来考虑给这个类添加事件。事件机制的引入使得开发者可以更灵活地开发程序。下面的代码示例展示了如何定义一个事件:
public event EventHandler Modified;
在C#中使用event关键字定义事件。把这个定义放到我们的类ComponentCS.StringComponent中,然后我们添加一个函数Modify(),这个函数修改字符数组StringsSet中指定位置的值,同时引发OnModify事件,而在Modify事件中,我们调用的是事件Modified所指定的函数:
public void Modify(int index,string value) { if ((index < 0) || (index >= StringsSet.Length)) { throw new IndexOutOfRangeException(); } else { StringsSet[index]=value; OnModify(); } }
private void OnModify()
{
EventArgs e=new EventArgs();
if(!(Modified==null))
Modified(this,e);
}
然后我们可以用如下的方法调用:
private void DoIt(){ StringComponent mysc=new StringComponent(); mysc.Modified+=new EventHandler(Called); mysc.Modify(2,"another string");}public void Called(object o,EventArgs e){ Console.WriteLine("Changed");}
在函数DoIt()中,我们首先建立了一个StringComponent类的对象mysc,然后将它的Mofidied事件关联到Called()方法:
mysc.Modified+=new EventHandler(Called);
注意“+=”符号的使用,相反地,如果使用“-=”符号,可以取消这个事件的绑定。
现在我们得到了一个虽然简单,但是比较完整的组件类:
1
using System;
2
namespace ComponentCS
3
{
4
public class StringComponent
5
{
6
7
private string[] StringsSet;
8
public event EventHandler Modified;
9
public int StringLength
10
{
11
get
12
{
13
return StringsSet.Length;
14
}
15
}
16
public void Modify(int index,string value)
17
{
18
if ((index < 0) || (index >= StringsSet.Length))
19
{
20
throw new IndexOutOfRangeException();
21
}
22
else
23
{
24
StringsSet[index]=value;
25
OnModify();
26
}
27
}
28
private void OnModify()
29
{
30
EventArgs e=new EventArgs();
31
if(!(Modified==null))
32
Modified(this,e);
33
}
34
public StringComponent()
35
{
36
StringsSet = new string[]
37
{
38
"C# String 0",
39
"C# String 1",
40
"C# String 2",
41
"C# String 3"
42
};
43
}
44
public string GetString(int index)
45
{
46
if ((index < 0) || (index >= StringsSet.Length))
47
{
48
throw new IndexOutOfRangeException();
49
}
50
return StringsSet[index];
51
}
52
}
53
}
最后要做的就是把它编译成.dll(动态链接库)文件,以便发布。发布成.dll文件最大的好处就是.dll文件中的内容已经编译,可以大大加快程序运行速度,此外还可以保护源代码。
using System;2
namespace ComponentCS 3
{4
public class StringComponent 5
{6

7
private string[] StringsSet;8
public event EventHandler Modified;9
public int StringLength10
{11
get12
{13
return StringsSet.Length;14
}15
}16
public void Modify(int index,string value)17
{18
if ((index < 0) || (index >= StringsSet.Length)) 19
{20
throw new IndexOutOfRangeException();21
}22
else23
{24
StringsSet[index]=value;25
OnModify();26
}27
}28
private void OnModify()29
{30
EventArgs e=new EventArgs();31
if(!(Modified==null))32
Modified(this,e);33
}34
public StringComponent() 35
{36
StringsSet = new string[] 37
{38
"C# String 0",39
"C# String 1",40
"C# String 2",41
"C# String 3"42
};43
}44
public string GetString(int index) 45
{46
if ((index < 0) || (index >= StringsSet.Length)) 47
{48
throw new IndexOutOfRangeException();49
}50
return StringsSet[index];51
}52
}53
}将产生的.cs文件编译成为.dll文件的方法如下:
csc.exe /t:library /debug+ /out:myCom.dll example.cs
这样就输出了名为myCom.dll的.dll文件。
OK,我们已经完成一个组件,麻雀虽小,五脏俱全,这就是一切组件的基础了,整个过程花不了十分钟。
当然,如果是一个具备实际使用价值的组件,我们要考虑的远远不止这些,但是可以看到,C#对组件的强大支持,可以大大提高我们的开发效率,从而使我们有更多的精力放在算法设计等方面,开发出更加出色的组件。




浙公网安备 33010602011771号