BDataServices类中,LendRender应用一个委托BorrowReader实现对文件的读操作,LendRender2应用接口类实现文件读操作。
 1namespace datalayer
 2{
 3    public delegate object BorrowReader(string strPath);
 4    /// <summary>
 5    /// The class to provide data
 6    /// </summary>

 7    public class DataServices
 8    {
 9        public DataServices()
10        {
11            // 
12            // TODO: Add constructor logic here
13            //
14        }

15        public static object LendRender(string strFile,BorrowReader borrower)
16        {
17                
18            return borrower(strFile);
19        }

20        public static object LendRender2(string strFile,IBorrowReader borrower)
21        {
22                            
23            return borrower.borrowReader(strFile);
24        }

25    }

26    public interface IBorrowReader
27    {
28        object borrowReader(string strFile);
29    }

30}

31

调用类,定义一个BorrowReader对象br,为br增加相应的代理实体函数。
using System;
using System.Data;
using System.IO;
namespace DP_Interface
{
    
/// <summary>
    
/// Summary description for Class1.
    
/// </summary>

    class Class1
    
{
        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
        
{
            
//
            
// TODO: Add code to start application here
            
//
            Console.WriteLine("----read by delegate");
            datalayer.BorrowReader br;
            br
=new datalayer.BorrowReader(readTxt);
            br
+=new datalayer.BorrowReader(readTxt);
            br
+=new datalayer.BorrowReader(readTxt);
            datalayer.DataServices.LendRender(
"test.txt",br);
            Console.WriteLine(
"----read by interface");
            datalayer.DataServices.LendRender2(
"test.txt",new borrower());
            Console.Read();
            
        }

        
public static object readTxt(string strPath)
        
{
            StreamReader sr
=new StreamReader(strPath,System.Text.Encoding.ASCII,false,8000);
            
string strContent;
            
while((strContent=sr.ReadLine())!=null)
            
{
                Console.WriteLine(strContent);
            }
            
            
return null;
        }

    }

    
class borrower:datalayer.IBorrowReader
    
{
        
public object borrowReader(string strFile)
        
{
            StreamReader sr
=new StreamReader(strFile,System.Text.Encoding.ASCII,false,8000);
            
string strContent;
            
while((strContent=sr.ReadLine())!=null)
            
{
                Console.WriteLine(strContent);
            }

            
            
return null;

        }

    }

}
文件test.txt内容为:
ste str
abcdef
输出结果为:
----read by delegate
ste str
abcdef
ste str
abcdef
ste str
abcdef
----read by interface
ste str
abcdef
posted on 2006-08-01 12:59  赶路人之刚出发  阅读(747)  评论(0编辑  收藏  举报