我有一堂课
命名空间TestLib1
public class TestLib1
{
public IEnumerable<TestedParameter> Measure1(IEnumerable<TestedParameter> _inParameters)
{ // this is just to do something
foreach (TestedParameter item in _inParameters)
{
item.Param_Description = "TestParam";
}
return _inParameters;
}
}
包含
中的类public class Acceptance_Criteria
{
private double _lowest;
private double _highest;
public bool Compare(double _verifiedValue)
{
if ((_verifiedValue >= _lowest) && (_verifiedValue <= _highest))
{
return true;
}
else
{
return false;
}
}
public double Acceptance_Lowest { get =>_lowest;set =>_lowest = value; }
public double Acceptance_Highest {get=>_highest;set =>_highest = value; }
}
public class TestedParameter : Acceptance_Criteria
{
private string _param_Description;
private double _value;
private string _units;
private bool _value_matches_acceptance_critera;
private bool _value_checked_against_criteria;
private string pass_fail_reason;
public string Param_Description { get => _param_Description; set => _param_Description = value; }
public double Value { get => _value; set => _value = value; }
public string Units { get => _units; set => _units = value; }
public bool Value_matches_acceptance_critera { get => _value_matches_acceptance_critera; set => _value_matches_acceptance_critera = value; }
public bool Value_checked_against_criteria { get => _value_checked_against_criteria; set => _value_checked_against_criteria = value; }
public string Pass_fail_reason { get => pass_fail_reason; set => pass_fail_reason = value; }
}
然后我将TestLib放在DLL中,并从其他文件中调用它:
Assembly SampleAssembly = Assembly.LoadFrom("TestLib1.dll");
Type[] types2 = SampleAssembly.GetTypes();//e
Type SearchedType = null;
foreach (Type type in types2)
{
if (type.FullName.Contains("TestLib1.TestLib1")) { SearchedType = SampleAssembly.GetType(type.FullName); }
}
if (SearchedType != null)
{
MethodInfo Method = SearchedType.GetMethod("Measure1");
if (Method != null)
{
object result = null;
ParameterInfo[] parameters = Method.GetParameters();
object classInstance = Activator.CreateInstance(SearchedType, null);
if (parameters.Length == 0)
{
//This works fine
result = Method.Invoke(classInstance, null);
}
else
{
List<TestedParameter> parametersIn = new List<TestedParameter>();
TestedParameter param1 = new TestedParameter();
param1.Param_Description = "testparam1"; // GET NAME FROM XML
parametersIn.Add(param1);
IEnumerable<TestedParameter> enumarableList = parametersIn.AsEnumerable();
object[] parametersArrayx = new object[] { enumarableList };
object resx = Method.Invoke(classInstance, parametersArrayx.ToArray());
List<TestedParameter> res = (List<TestedParameter>)resx;
}
}
}
当我执行Method.Invoke时 我得到例外:
System.ArgumentException:
“类型为'System.Collections.Generic.List'1 [WindowsFormsApp1.TestedParameter]的对象” 不能转换为类型'System.Collections.Generic.List`1 [WindowsFormsApp1.TestedParameter]'。”
我也尝试在没有ToArray()的情况下推送parametersArrayx,但是它也没有起作用
任何人都可以看一下代码,让我知道我做错了什么吗? 我想从DLL lib执行带有推送参数列表的代码(后来我想从XML文件导入这些参数。)
谢谢:)
答案 0 :(得分:0)
SearchedType.GetMethod("Measure1");
转换为
SearchedType.GetMethod("Measure1", new Type[] { typeof(List<Filter>) });