求助 在vs2003中构建dll文件和析构函数应用问题
IOComponent.cs
using System;
using System.Text;
using System.IO;
using System.ComponentModel;
namespace ClassLibrary2
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
public class IOComponent:Component
{
StreamReader myStreamReader;
public void DoRead()
{
string strReadFile;
Console.WriteLine("请输入欲读取的文件名称");
strReadFile=Console.ReadLine();
myStreamReader=new StreamReader(strReadFile);
Console.WriteLine("开始读取文件");
ReadText();
}
private void ReadText()
{
string strBufferText;
do
{
strBufferText=myStreamReader.ReadLine();
Console.WriteLine(strBufferText);
}while(strBufferText!=null);
}
protected override void Dispose(bool pDis)
{
if(pDis==true)
{
Console.WriteLine("此时传入的参数为true");
Console.WriteLine("关闭数据流对象");
myStreamReader.Close();
}
else
{
Console.WriteLine("此时传入的参数为false");
Console.WriteLine("清楚未受管理的代码");
}
base.Dispose (pDis);
}
~IOComponent()
{
Console.WriteLine("析构函数被调用");
Console.WriteLine("调用Dispose(false)");
Dispose(false);
Console.ReadLine();
}
public IOComponent()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
}
UsingIOComponent.cs
using System;
class UsingIOComponent
{
public static void Main(string[] args)
{
IOComponent myIOComponent=new IOComponent();
myIOComponent.DoRead();
myIOComponent.Dispose();
Console.ReadLine();
}
}
我的问题如下
1:如何把IOComponent.cs文件编译成为dll文件 被UsingIOComponent.cs引用 在vs2003应该如何作?
2: protected override void Dispose(bool pDis)
{
if(pDis==true)
{
Console.WriteLine("此时传入的参数为true");
Console.WriteLine("关闭数据流对象");
myStreamReader.Close();
}
else
{
Console.WriteLine("此时传入的参数为false");
Console.WriteLine("清楚未受管理的代码");
}
base.Dispose (pDis);
}
~IOComponent()
{
Console.WriteLine("析构函数被调用");
Console.WriteLine("调用Dispose(false)");
Dispose(false);
Console.ReadLine();
}
既然 Dispose方法可以 释放资源 为什么还要用 ~IOComponent() 这个类的析构函数?
3:pDis==true 和 pDis==false 执行释放资源的操作有什么区别吗
问题点数:10、回复次数:1Top
1 楼litp(天道酬勤)回复于 2006-03-02 17:20:00 得分 0
新建一个类库项目,把文件添加进去编译一下就是dll了。
我得理解是Dispose释放的是里面指定的对象,~IOComponent() 释放该类自己。
这里的pDis==true 和 pDis==false 执行完了都会释放资源,应该没什么
Top




