java expandoobject_无法将带有[]的索引应用于" 对象" 类型的表达式(即使类型为" 动态" ) (Cannot apply indexing with [] to an expre...

2014-02-28 09:56:09

1

I have an ExpandoObject which is created like so:

public ExpandoObject Get()

{

var expando = new ExpandoObject();

var expandoDic = (IDictionary)expando;

// go through the items in the dictionary and copy over the key value pairs)

foreach (var f in GetFieldList())

{

if (f.MaxValues == 1)

{

var val = f.Values.Count > 0 ? f.Values[0] : null;

if (f.displayType == DisplayType.Entity && f.AttachedEntities != null && f.AttachedEntities.Any())

{

if (f.AttachedEntities.Count == 1)

{

expandoDic.Add(new KeyValuePair(f.Code, f.AttachedEntities[0].Get()));

}

else

{

expandoDic.Add(new KeyValuePair(f.Code, f.AttachedEntities.Select(e => e.Get())));

}

}

else

{

expandoDic.Add(new KeyValuePair(f.Code, GetTypedValue(f.GetValueType(), val)));

}

}

else

{

expandoDic.Add(new KeyValuePair(f.Code, (dynamic)f.Values.Select(v => GetTypedValue(f.GetValueType(), v))));

}

}

return expando;

}

The GetTypedValue simply converts the string value into the appropriate type and returns dynamic.

The problem I'm getting is that if I add a collection to expandoDic then I can't access the members without casting it to an ICollection type. Consider the following code, where myPage is an ExpandoObject created by the above method.

Response.Write(myPage.menu.items[0]);

The menu property is a dynamic object, as is items. The latter is a collection of strings, though the type is actually IEnumerable'. If I inspect myPage.menu.items, it tells me the type isdynamic {System.Linq.Enumerable.WhereSelectListIterator}`. The above code produces the following error:

Cannot apply indexing with [] to an expression of type 'object'

If I use First() instead of an index, I instead get this error:

'object' does not contain a definition for 'First'

I understand that I could cast items to IEnumerable and fix the problem right away, but I am writing a development framework and want to remove any barriers to developers using it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值