//同类型的基类.用来确定需动态实例化的类的程序集位置及名称,用以replace用

basepro bp = new basepro();


//获取该类型在程序集完整限定名
string classname = bp.GetType().AssemblyQualifiedName;
Response.Write(classname);

//替换程序集位置及名称为要实例化的类

Type objtype = Type.GetType(classname.Replace("basepro","pro_21"));

//实例化

object o = Activator.CreateInstance(objtype);

//要实例化的类所继承的接口(也可用methodinfo方法来查找该实例化类的方法)

Iproduct protemp = o as Iproduct;
if (protemp.ProductHandle())
{
Response.Write("<br/>is true");
}
else

Response.Write("<br/>false");

 

C# code
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms; 
using System.Reflection;

namespace WindowsApplication13
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            object o = Activator.CreateInstance(Type.GetType("WindowsApplication13.MyClass"));

            MethodInfo MyMethod = o.GetType().GetMethod("Test");
            object[] obj = new object[1];
            obj[0] = "ZengHD";
            MyMethod.Invoke(o, obj);
        }
    }

    public class MyClass
    {
        public void Test(string s)
        {
            MessageBox.Show(s);
        }
    }
}

 

 posted on 2011-10-17 18:17  vingi_苍月  阅读(271)  评论(0编辑  收藏  举报