我的类方法不是从C#中的程序集调用

时间:2018-06-15 04:24:02

标签: c# asp.net

我有以下代码结构。我想从DLL /汇编调用方法。我试图结果:

string strDLL = "Dll Path";

Assembly assembly = Assembly.LoadFrom(strDLL);
Type type = assembly.GetType("NameSpace.ClassName");
object objplugin = Activator.CreateInstance(type);
var objUserid = objplugin.GetType().GetProperty("UserID"); 
objUserid.SetValue(objplugin,this.txtUserName.Text, null);                     
//objplugin.UserID = this.txtUserName.Text;
//objplugin.Password = this.txtPassword.Text;         
var connection1 =objplugin.GetType().GetMethod("MethodName",null);                  

但是我无法调用Method From Assembly。:

1 个答案:

答案 0 :(得分:0)

也许它会帮助你。

string strDLL = @"Dll Path";
string txtUserName = "test user name";

Assembly assembly = Assembly.LoadFrom(strDLL);
Type type = assembly.GetType("TestLib.MyClass");
object objplugin = Activator.CreateInstance(type);
var objUserIdProp = type.GetProperty("UserID");
objUserIdProp.SetValue(objplugin, txtUserName);
var methodInfo = type.GetMethod("TestMethod");
methodInfo.Invoke(objplugin, null);

namespace TestLib
{
    public class MyClass
    {
        public string UserID { get; set; }

        public void TestMethod()
        {
            Console.WriteLine("TestMethod work");
        }
    }
}