111,121
社区成员
发帖
与我相关
我的任务
分享
使用反射查看某个dll文件的实现
string _strPath = AppDomain.CurrentDomain.BaseDirectory;
string str = _strPath + "Platform.dll";
Assembly m_Assembly = Assembly.LoadFrom(str);//载入装配件
//枚举一个dll下的成员
static void EnumCls(Assembly m_Assembly)
{
Type[] types = m_Assembly.GetTypes();
foreach (Type t in types)
{
Console.WriteLine(t.Name);
}
}
使用反射调用某个dll文件下的方法
string str = _strPath + "BLL.dll";
Assembly m_Assembly = Assembly.LoadFrom(str);//载入装配件
string clsName = "Bll";
object o = ActivetorMethodOutObject(clsName, m_Assembly);//返回装配件下的类
Bll bl = (Bll)o;
bl.Greeting();
//返回指定dll下的类
static object ActivetorMethodOutObject(string clsName,Assembly m_Assembly)
{
object _result = null;
try
{
string _tmpCls = "BLL." + clsName;
Type _CurrentType = m_Assembly.GetType(_tmpCls,false);
_result = Activator.CreateInstance(_CurrentType);
}
catch { }
return _result;
}
BLL.dll
using System;
using System.Collections.Generic;
using System.Text;
namespace BLL
{
public class Bll
{
public void Greeting()
{
Console.WriteLine("Hello from your classmate DongYi Wang,who is now working in ShangHai " +
"as a coder,he sometimes miss you very much.How time flies,it's almost 4 years since " +
"the last meet.Thanks for your gift.Remember me to your family and wish you success." +
"Enjoy every day.Keep touching.yours sincerely.");
}
}
}