zoukankan      html  css  js  c++  java
  • 一个简单的模板系统的实现(动态载入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>
  • 相关阅读:
    (转)使用vsphere client 克隆虚拟机
    【转】VIM高级用法笔记
    Oracle RAC的Failover
    /dev/shm过小导致ORA00845错误解决方法
    (转)How to use udev for Oracle ASM in Oracle Linux 6
    ORACLE十进制与十六进制的转换
    解决Oracle RAC不能自动启动的问题
    RAC集群时间同步服务
    db link hang的解决方法
    【转载】Oracle数据恢复 Linux / Unix 误删除的文件恢复
  • 原文地址:https://www.cnblogs.com/ifan/p/1358710.html
Copyright © 2011-2022 走看看