【问题标题】:invoke method with list of parameter c#使用参数 c# 列表调用方法
【发布时间】:2018-12-24 21:37:45
【问题描述】:

我有一堂课:

命名空间 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 文件中导入这些参数。)

谢谢你:)

【问题讨论】:

  • TestedParameter 的哪个程序集中声明了?这看起来你已经加载了两次相同的程序集。
  • 为什么不在您的主项目中添加对TestLib1 的简单引用?那你就不用惹Assembly.LoadFrom()了。或者,如果您需要 TestLib1 与主解决方案分开,请研究将 TestLib1 转换为 NuGet 包。
  • 是的,我查看了相同的解决方案,当我将 enumarableList 交换为 parametersIn 时,它给出了相同的异常
  • 为什么不在您的主项目中添加对 TestLib1 的简单引用?我希望将来可以灵活地添加具有 XML 可重新配置测试参数和函数名称的 DLL,而不是重新编译整个项目。这将允许其他人为此目的创建测试序列并仅上传 DLL 和 XML。还是我做错了? NuGet 包 嗯,我觉得这个话题很薄 :( 然而

标签: c# methods parameters


【解决方案1】:
SearchedType.GetMethod("Measure1"); 

转换成

   SearchedType.GetMethod("Measure1", new Type[] { typeof(List<Filter>) });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    • 2013-09-07
    • 2017-03-08
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    相关资源
    最近更新 更多