一个简单的模板系统的实现(动态载入DLL)

目的:为WEB程序做一套模板系统,每个模板是一个dll,在不影响主程序的情况下,动态加载模板。

假设我们有一个类叫TUser,保存了用户的信息,很简单,只有username和email两个字段

/* **************************************************************
** /user.cs
** 编译参数 csc /t:library /out:bin/user.dll user.cs
**************************************************************
*/
namespace  demo
{
    
public   class  TUser
    {
        
public   string  UserName  =   "" ;
        
public   string  Email  =   "" ;
        
public  TUser( string  userName,  string  email)
        {
            
this .UserName  =  userName;
            
this .Email  =  email;
        }
    }
}

 

下面是我们的模板的接口 ITemplate,只有一个 Print 方法

/* **************************************************************
** /ITemplate.cs
** 编译参数 csc /r:user.dll /t:library /out:bin/ITemplate.dll ITemplate.cs
**************************************************************
*/
namespace  demo
{
    
public   interface  ITemplate
    {
        
void  Print(TUser u);
    }
}


下面是一个其中一个模板的实现,名字叫 table

/* **************************************************************
** /template/table.cs
** 编译参数 csc /r:..\bin\user.dll,..\bin\ITemplate.dll /t:library /out:bin/dll/table.dll table.cs
**************************************************************
*/
namespace  demo
{
    
public   class  Template : ITemplate
    {
        
public   void  Print(TUser u)
        {
            System.Console.WriteLine(
" <template:table> " );
            System.Console.WriteLine(
" <username>{0}</username>\n " , u.UserName);
            System.Console.WriteLine(
" <email>{0}</email>\n " , u.Email);
            System.Console.WriteLine(
" </template:table> " );
        }
    }
}

 

下面是主程序

/* **************************************************************
** /program.cs
** 编译参数 csc /r:bin/user.dll,bin/ITemplate.dll /out:bin/program.exe program.cs
**************************************************************
*/
using  System;
using  System.Reflection;
using  System.Configuration;

namespace  demo
{
    
public   class  Program
    {
        
static   void  Main()
        {
            
string  dll  =  ConfigurationManager.AppSettings[ " template " ];
            
string  className  =  ConfigurationManager.AppSettings[ " className " ];

            TUser u 
=   new  TUser( " ifan " " ifan@cnblogs.com " );

            Assembly ass 
=  Assembly.LoadFrom(dll);
            Type t 
=  ass.GetType(className);
            ITemplate o 
=  (ITemplate)Activator.CreateInstance(t);
            o.Print(u);
        }
    }
}

 

我们把要载入的模板信息放在配置文件里面

<!--
program.exe.config
-->
<? xml version="1.0" ?>
< configuration >
    
< appSettings >
        
< add  key ="template"  value ="dll/table.dll" />
        
< add  key ="className"  value ="demo.Template" />
    
</ appSettings >
</ configuration >

 


编译后的路径结构是

/bin/program.exe
/bin/program.exe.config
/bin/ITemplate.dll
/bin/user.dll
/bin/dll/table.dll


 运行 program.exe 就能看到结果了:

< template:table >
< username > ifan </ username >
< email > ifan@cnblogs.com </ email >
</ template:table >

 

现在我们再写一个模板,名字叫list:

/* **************************************************************
** /template/list.cs
** 编译参数 csc /r:..\bin\user.dll,..\bin\ITemplate.dll /t:library /out:bin/dll/list.dll list.cs
**************************************************************
*/
namespace  demo
{
    
public   class  Template : ITemplate
    {
        
public   void  Print(TUser u)
        {
            System.Console.WriteLine(
" <template:list> " );
            System.Console.WriteLine(
" <username>{0}</username>\n " , u.UserName);
            System.Console.WriteLine(
" <email>{0}</email>\n " , u.Email);
            System.Console.WriteLine(
" </template:list> " );
        }
    }
}

 

编译后将配置文件中的 template 的 value 改成 dll/list.dll,重新运行 program,能看到不一样的结果:
< template:list >
< username > ifan </ username >
< email > ifan@cnblogs.com </ email >
</ template:list >

转载于:https://www.cnblogs.com/ifan/archive/2008/12/19/1358710.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值