简体   繁体   English

C#DynamicObject:有没有办法按索引访问属性?

[英]C# DynamicObject: Is there a way to access properties by index?

Is there a way to access the properties of a dynamic object by index? 有没有办法通过索引访问动态对象的属性?

List<dynamic> list = _dataStagingManager.GetStatusList(consoleSearch);
foreach (var row in list)
{
    Console.WriteLine(row[0]);
    Console.WriteLine(row[1]);
}

In this example, the property name of row[0] is different depending on the results from a stored procedure column alias. 在此示例中,row [0]的属性名称根据存储过程列别名的结果而不同。

Unfortunately, no. 很不幸的是,不行。 Unlike runtime objects (which have an iterable collection of their members that can easily be reflected/queried against) dynamic objects, by default, have no such collection. 与运行时对象(具有可轻松反映/查询的成员的可迭代集合)不同,默认情况下,动态对象没有此类集合。 They can be implemented in any way that allows their members to be accessed by name. 它们可以以任何方式实现,允许通过名称访问其成员。 There is no idea of indices with dynamic objects, provided you don't choose to implement them yourself. 如果你没有选择自己实现它们,那么就不知道带有动态对象的索引。

If you wanted to, you could override the TryGetIndex method on your dynamic type to provide indexing support. 如果您愿意,可以覆盖动态类型的TryGetIndex方法以提供索引支持。

In general, that's not possible. 一般来说,这是不可能的。 For example, you could create a dynamic object that has every possible property: 例如,您可以创建具有所有可能属性的动态对象:

class AnythingGoes : DynamicObject
{
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = binder.Name;
        return true;
    }
}

There is a chance you will be able to get the list of properties. 您有可能获得物业清单。 If the dynamic object is actually normal object (one that does not implement IDynamicMetaObjectProvider ), normal reflection will work fine. 如果动态对象实际上是普通对象(未实现IDynamicMetaObjectProvider ),则正常反射将正常工作。

If the object actually is dynamic (implements IDynamicMetaObjectProvider ), you could try calling GetDynamicMemberNames() on the DynamicMetaObject returned from GetMetaObject() . 如果对象实际上是动态的(实现IDynamicMetaObjectProvider ),您可以尝试在GetDynamicMemberNames()返回的DynamicMetaObject上调用GetMetaObject() But, as the example above shows, this is not guaranteed to work. 但是,正如上面的例子所示,这不能保证有效。

I am not familiar with Dapper Dot Net but after a short look at it, it looks as if it is returning anonymous types as dynamic objects. 我对Dapper Dot Net并不熟悉,但经过短暂的研究后,它看起来好像是将匿名类型作为动态对象返回。 You can look at those as POCOs (plain old CLR objects) meaning that they are classes with individual named properties, probably not implementing any kind of indexer which you would needed to access fields by index. 您可以将它们视为POCO(普通的旧CLR对象),这意味着它们是具有单独命名属性的类,可能不会实现您需要按索引访问字段的任何类型的索引器。

You could try using Reflection to achieve something similar: 您可以尝试使用Reflection来实现类似的功能:

foreach (dynamic propInfo in row.GetType().GetProperties())
{
    Console.WriteLine("{0}: {1}", propInfo.Name, propInfo.GetValue(row, null));
}

I'm not sure you'll get the properties in the same order the stored procedure is returning them, though. 不过,我不确定你会以存储过程返回它们的顺序获取属性。

如果row的运行时类型是一个整数索引器,那么你的代码应该没问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM