如何将 ExpandoObject 的字典设置为不区分大小写?
给定下面的代码
dynamic e = new ExpandoObject();
var d = e as IDictionary<string, object>;
for (int i = 0; i < rdr.FieldCount; i++)
d.Add(rdr.GetName(i), DBNull.Value.Equals(rdr[i]) ? null : rdr[i]);
是否有办法使其不区分大小写,因此给定字段名称employee_name
e.Employee_name 与 e.employee_name 一样工作,
似乎没有明显的方法,也许是黑客?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我一直在使用不区分大小写的“Flexpando”类(用于灵活的expando)。
它类似于 Darin 的 MassiveExpando 答案,因为它为您提供字典支持,但是通过将其公开为字段,它可以节省实现的麻烦IDictionary 大约有 15 个成员。
I've been using this “Flexpando” class (for flexible expando) which is case-insensitive.
It's similar to Darin's MassiveExpando answer in that it gives you dictionary support, but by exposing this as a field it saves having to implement 15 or so members for IDictionary.
您可以查看 Massive 的
MassiveExpando
这是不区分大小写的动态对象。You may checkout Massive's implementation of a
MassiveExpando
which is case insensitive dynamic object.更多的是出于好奇而不是解决方案:
RuntimeOps.ExpandoTryGetValue/ExpandoTrySetValue
使用ExpandoObject
的内部方法来控制区分大小写。null, -1,
参数取自ExpandoObject
内部使用的值(RuntimeOps
直接调用ExpandoObject< 的内部方法) /code>)
请记住,这些方法
此 API 支持 .NET Framework 基础结构,并且不适合直接在您的代码中使用。
More as a curiosity than as a solution:
RuntimeOps.ExpandoTryGetValue/ExpandoTrySetValue
use internal methods ofExpandoObject
that can control the case sensitivity. Thenull, -1,
parameters are taken from the values used internally byExpandoObject
(RuntimeOps
calls directly the internal methods ofExpandoObject
)Remember that those methods are
This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
另一种解决方案是通过从
System.Dynamic.DynamicObject
派生并重写TryGetValue
和TrySetValue
来创建类似ExpandoObject
的类>。Another solution is to create a
ExpandoObject
-like class by deriving fromSystem.Dynamic.DynamicObject
and overridingTryGetValue
andTrySetValue
.