关于Asp.net中 通过类名所对应的字符串获得类实例的方法的研究

using System;
using System.Reflection;
using System.Web;
using System.IO;

public class site3 : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/html";
        int x = 0;
        int y = 0;
        int z = 0;
        System.Collections.Generic.Dictionary<string, string> dic = new System.Collections.Generic.Dictionary<string, string>();
        dic.Add("+","Add");
        dic.Add("-","Sub");
        dic.Add("*", "Mul");
        dic.Add("/", "Div");
        if (!string.IsNullOrEmpty(context.Request.Form["hidden"]))
        {
            if (int.TryParse(context.Request.Form["txtNum1"], out x) && int.TryParse(context.Request.Form["txtNum2"], out y))
            {
                Add add = new Add();
                Icalcable ical=null;
                string ssssss= Assembly.GetAssembly(add.GetType()).FullName;
                string sssss = Assembly.GetAssembly(add.GetType()).GetName().Name;
                string classname = dic[context.Request.Form["select1"]];
                AssemblyName[] asss= Assembly.GetExecutingAssembly().GetReferencedAssemblies();//加载当前运行程序集所引用的程序集
                foreach (AssemblyName item in asss)//遍历引用的程序集
                {
                    string sss = item.Name;

                    try
                    {
                        ical= Assembly.Load(sss).CreateInstance(classname) as Icalcable;
                        if (ical != null)
                        {
                            break;
                        }
                    }
                    catch (Exception)
                    {
                       
                       
                    }
                    s
                   
                }
                z = ical.Calculate(x,y);
                }
        }
        System.Text.StringBuilder sbHtml = new System.Text.StringBuilder("<html><header><title></title><body><form action='site3.ashx' method='post'><input type='text' name='txtNum1' value=" + x + " /><select name='select1'><option>+</option><option>-</option><option>*</option><option>/</option></select><input type='text' name='txtNum2' value=" + y + ">=<input type='text' value=" + z + " name='txtResult'><input type='submit' value='计算'><input type='hidden' name='hidden' value='1' /></form>");
        context.Response.Write(sbHtml.ToString());

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

通过这两天查的资料终于解决了获得VS下App_Code文件夹里面类的程序集的问题。主要用到了通过当前运行的程序集遍历引用的程序集,通过创建对象转换成接口运行方法成功搞定s

posted @ 2011-07-24 02:53  方向!  阅读(427)  评论(0)    收藏  举报