C#动态加载类

using System;
using System.Data;
using System.Reflection;
using System.Web;
using System.Web.UI;

namespace WebApplication1
{
    
/// <summary>
    
/// Class1 的摘要说明。
    
/// </summary>

    public class Servlets:System.Web.UI.Page
    
{
        
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
        
            Response.Write(InitPage());
            AfterLoadPage();
            
//string aliasName="test2.Class2";

            
//result.AppAssembly = ((ClassItem)item).ClassAssembly;
            
//return result;
            
            
//Assembly assem= Assembly.Load(aliasName);
            
            Type[] helloType 
= LoadAssembly("test2");
            
            MethodInfo printMethod 
= helloType[0].GetMethod("reback");
            
            Object obj 
= Activator.CreateInstance(helloType[0]);
            Response.Write(printMethod.Invoke(obj,
null));
            
//Object[] args = new Object[1];
            
            
//args[0] = "From CSharp Late Bound";
            
//printMethod.Invoke(obj, args);
//            
            
//Response.Write(result);


        }


        
public Type[] LoadAssembly(string filename)

        
{

            Type[] functionList 
= new Type[10];

            Assembly assembly 
= AppDomain.CurrentDomain.Load(filename);

                
int i=0;

            
foreach (Type t in assembly.GetTypes())

            
{
                
                functionList[i]
=t;
                i
=i+1;

            }
   

            
return functionList;

        }
 



        
protected virtual void AfterLoadPage()
        
{
        }

        
        
protected virtual string InitPage()
        
{
            
return "BASE";
        }


        
//Web 窗体设计器生成的代码
        Web 窗体设计器生成的代码

    }

}

原文地址:https://www.cnblogs.com/chinatefl/p/279656.html