一个简单的模板系统的实现(动态载入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>
posted @ 2008-12-19 22:31  ifanxp  阅读(496)  评论(0编辑  收藏  举报