Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
属性的c#反射更改获取方法_C#_Reflection - Fatal编程技术网

属性的c#反射更改获取方法

属性的c#反射更改获取方法,c#,reflection,C#,Reflection,在我的C#项目中,我依赖于第三方库。现在我想为我的项目编写一个自动验收测试。然而,要调用测试,我需要一个来自第三方库的类实例。使用反射,我成功地设置了我需要的值 以下是我遇到问题的相关课程: public class SystemResults { // There must be something like this. However I cannot see it as it is private. private List<Positions> xyz = n

在我的C#项目中,我依赖于第三方库。现在我想为我的项目编写一个自动验收测试。然而,要调用测试,我需要一个来自第三方库的类实例。使用反射,我成功地设置了我需要的值

以下是我遇到问题的相关课程:

public class SystemResults
{
    // There must be something like this. However I cannot see it as it is private.
    private List<Positions> xyz = new List<Positions>();

    public IList<Positions> Positions
    {
        get
        {
            return xyz;
        }

        // This property does not have a set method
    }
}
公共类系统结果
{
//一定有这样的东西,但我看不见,因为它是私人的。
私有列表xyz=新列表();
公务员职位
{
得到
{
返回xyz;
}
//此属性没有set方法
}
}
以下是我迄今为止所尝试的:

private void InitSystemResults(Type trueSysResType, SystemResults sysRes)
{
    List<Position> positions = ImporterTools.GetPositions(PositionsCsv);
    trueSysResType.GetProperty("Positions").SetValue(sysRes, positions); // ==> Here I get an exception
}
private void InitSystemResults(类型trueSysResType,SystemResults sysRes)
{
列表位置=导入工具.GetPositions(位置SCSV);
trueSysResType.GetProperty(“Positions”).SetValue(sysRes,Positions);//=>这里我得到一个异常
}
当我调用
SetValue()
方法时,会引发以下异常

System.ArgumentException:未找到属性集方法

从这些信息中我发现,这个类必须看起来像我上面描述的那样


现在我想继续讨论这个问题。有人知道当我访问
sysRes.Positions
我的
Positions
是通过get方法返回的时,我该如何实现这一点吗?或者是否有方法更改get方法?

{get;}
只有属性可以有支持字段,但是
位置
可能返回完全不同的内容(不是支持字段的值,但可能是函数的结果)


您的代码可以接受您可以模拟的
ISystemResults
,在真实代码中,您可以拥有一个内部调用第三方代码的类
SystemResultsFacade

您可以使用
BindingFlags.NonPublic

FieldInfo[] fields = typeof(SystemResults).GetFields(
                         BindingFlags.NonPublic |
                         BindingFlags.Instance).ToArray(); // gets xyz and the other private fields

List<int> testResults = new List<int>() { 1,23,4,5,6,7}; // list of integer to set

SystemResults sysres = new SystemResults(); // instance of object
fields[0].SetValue(sysres, testResults); // I know that fields[0] is xyz (you need to find it first), 
// sets list of int to object
FieldInfo[]fields=typeof(SystemResults).GetFields(
BindingFlags.NonPublic|
BindingFlags.Instance).ToArray();//获取xyz和其他私有字段
List testResults=new List(){1,23,4,5,6,7};//要设置的整数列表
SystemResults sysres=新的SystemResults();//对象实例
字段[0]。设置值(sysres,testResults);//我知道字段[0]是xyz(您需要先找到它),
//将int的列表设置为对象


希望有帮助,这是真的。没有考虑。@彼得多尼奥好点,谢谢。我在胡说八道——我在考虑基于表达式的属性。回答已更新。请仔细阅读。这可能有助于你成为英雄!;)效果很好。非常感谢!