在我的C#项目中,我对第三方库有依赖性。现在我想为我的项目编写一份自动验收测试。但是为了调用测试,我需要来自第三方库的一个类的实例。使用反射,我设法设置的价值,因为我需要他们除了一个。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
}
}
这里是我到目前为止已经试过:
private void InitSystemResults(Type trueSysResType, SystemResults sysRes)
{
List<Position> positions = ImporterTools.GetPositions(PositionsCsv);
trueSysResType.GetProperty("Positions").SetValue(sysRes, positions); // ==> Here I get an exception
}
当我调用SetValue()
方法如下例外是抛出。
System.ArgumentException:找不到属性集方法。
从这些信息中我发现,这个班必须像我上面描述的那样。
现在我想继续这一些如何。有没有人有个想法,当我访问sysRes.Positions
时,我的positions
是由get方法返回的?或者有没有办法改变get方法?
阅读本up.might帮助https://stackoverflow.com/questions/135020/advantages-to-using -private-static-methods –