[英]How to get values of fields of List<of_anonymous_type> (plus casting)?
I have this code. 我有这个代码。 At the very end we have List
of anonymous objects with fields FILE_NAME
, DATE
, REPORT_LINK
. 最后,我们List
了包含字段FILE_NAME
, DATE
, REPORT_LINK
的匿名对象。
public ServiceResult Get(string userIdString)
{
var userId = Int32.Parse(userIdString);
var downloadLink = _serviceConfigManager.Parameters[ServiceResources.DownloadService].FirstOrDefault();
var query = SharedUnitOfWork.Context.ReportRequest
.Where(item => item.IS_DELETED == 0
&& item.TENANT_ID == TenantId
&& item.REPORT_STATE_ID == 2
&& item.CREATED_BY_USER_ID == userId)
.OrderByDescending(item => item.ID)
.Take(10)
.ToList()
.Select(item => new
{
FILE_NAME = item.FILENAME,
DATE = item.CREATION_DATE,
REPORT_LINK = string.Format("{0}/{1}", downloadLink, item.ID)
}
);
return new ServiceResult<object>(query.ToList());
}
First of all as you can see we return ServiceResult<object>
object. 首先,您可以看到我们返回ServiceResult<object>
对象。 The second, the returning type of function is ServiceResult
(NOT GENERIC). 第二种,返回的函数类型是ServiceResult
(NOT GENERIC)。 This code i didnt write SO I CANT CHANGE THE SOURCE CODE of this method. 这段代码我没写,因此我无法更改此方法的源代码。
The task: In my unit tests i need to get returning object and check if it is populated with correct data. 任务:在我的单元测试中,我需要返回对象并检查它是否填充了正确的数据。 I wrote my test the way the function Get
returns 2 records. 我以函数Get
返回2条记录的方式编写了测试。 SO, i need to check every field in this records. 所以,我需要检查此记录中的每个字段。 How to do that? 怎么做?
In my tests i get from this function variable of serviceResult
name. 在我的测试中,我从serviceResult
name的这个函数变量中获取。 How to "unwrap" serviceResult
so as it looks like a List<of_something>
and I could check List<of_something>[0].FILE_NAME
, List<of_something>[1].FILE_NAME
and etc? 如何“解包” serviceResult
,使其看起来像List<of_something>
,我可以检查List<of_something>[0].FILE_NAME
, List<of_something>[1].FILE_NAME
等?
serviceResult
has Result
field that equals query.ToList()
. serviceResult
Result
字段等于query.ToList()
。
TO MODERATORS : I read all offered similar topics, non of them describes|solves my task because I can't rewrite source code of Get function. 对于调制器 :我读了所有提供类似的主题,其中没有描述|解决了我的任务,因为我无法重写Get函数的源代码。
You can't get hold of the anonymous type. 你无法掌握匿名类型。 Your options are: 你的选择是:
GetType().GetProperty()
etc) to read the data 使用反射( GetType().GetProperty()
等)来读取数据 dynamic
使用dynamic
The latter is probably more convenient: 后者可能更方便:
foreach(dynamic obj in listOfAnonObjects) {
string filename = obj.FILE_NAME;
DateTime date = obj.DATE;
string link = obj.REPORT_LINK;
// TODO: now use filename, date and link
}
You could use the above to project the data into your own list type that has well advertised properties. 您可以使用上述内容将数据投影到您自己的列表类型中,该列表类型具有良好的广告属性。
Problem solved using Reflection. 使用Reflection解决问题。
Using pattern targetType.GetType().GetProperty(propertyName).GetValue(targetType,index:optional)
I can get value of any field of anonymous type. 使用模式targetType.GetType().GetProperty(propertyName).GetValue(targetType,index:optional)
我可以获得匿名类型的任何字段的值。
In case of indexer I use GetProperty("Item")
and GetValue(targetType,new object[]{0})
, where the second parameter is needed index. 在索引器的情况下,我使用GetProperty("Item")
和GetValue(targetType,new object[]{0})
,其中第二个参数需要索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.