日期:2014-05-20  浏览次数:20715 次

如何调用dll中的方法并获取返回值
有一个未包含在项目中的dll文件,我想调用里面的“GetName”方法,并获得返回值,请问该怎么做
public string GetName(string a)
{
reten a+"a";
}
dll的路径为:D:\\name.dll
我的项目是winform项目,未引用“name.dll”


------解决方案--------------------
C# code


Assembly ab = Assembly.LoadFile("D:\\name.dll");

            Type tp = ab.GetTypes()[0];

            MethodInfo mi = tp.GetMethod("GetName");

            object obj=ab.CreateInstance(tp.Namespace+"."+tp.Name);
            string str =  mi.Invoke(obj, new object[]{"aa"}).ToString();