![](/img/trans.png)
[英]System.MissingMethodException: No parameterless constructor defined for this object
[英]MissingMethodException: no parameterless constructor defined for this object
我正在嘗試從控制台應用程序調用dll類。 我試圖從dll調用的所有類都有參數化的構造函數。 我希望將user並作為兩個參數傳遞給我嘗試調用的任何類。 我知道我如何調用方法有問題,但無法弄清楚是什么。
有人可以更正我的代碼嗎?
該行中發生錯誤-“對象實例= Activator.CreateInstance(type)”
控制台應用-
class Program
{
static void Main(string[] args)
{
string test_method = args[2];
string user = args[0];
string pass = args[1];
Assembly myassembly = Assembly.LoadFrom("Tests.dll");
Type type = myassembly.GetType("Tests." + test_method);
object instance = Activator.CreateInstance(type);
MethodInfo[] methods = type.GetMethods();
methods[0].Invoke(instance, new object[] {user, pass });
Console.WriteLine("\n"+test_method + " has passed.");
}
}
一個被稱為的示例類-
public class Test_3456 : ePO
{
public Test_3456(string a, string b) : base (a, b)
{
MultipleWebclipsSameNameDifferentUrl_3456();
}
[TestMethod]
public void MultipleWebclipsSameNameDifferentUrl_3456()
{
PolicyCatalog.GoTo();
PolicyCatalog.PolCatSelect("iOS (User-Based Policy)");
WebClip.iOSWebClipsTab();
WebClip.Add2WebClipsSameNameDifferentUrl();
WebClip.Ok();
WebClip.Save();
}
}
這里的構造函數是虛擬的構造函數。 基類接受用戶並傳遞參數。
基類-
public class ePO
{
public ePO(string a, string b)
{
Init(a, b);
}
[TestInitialize]
public void Init(string username, string password)
{
Driver.initialize();
Login.GoTo();
Login.LoginAs(username).WithPassword(password).Login();
}
}
當Activator.CreateInstance()
創建對象時,需要在此提供構造函數參數:
object instance = Activator.CreateInstance(type, new object[] {user, pass });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.