如何将 ExpandoObject 的字典设置为不区分大小写?

发布于 2024-12-09 11:40:12 字数 354 浏览 0 评论 0 原文

给定下面的代码

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 一样工作,

似乎没有明显的方法,也许是黑客?

given the code below

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]);

Is there a way to make it case insensitive so given the field name employee_name

e.Employee_name works just as well as e.employee_name

there doesn't seem to be an obvious way, perhaps a hack ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尐籹人 2024-12-16 11:40:12

我一直在使用不区分大小写的“Flexpando”类(用于灵活的expando)。

它类似于 Darin 的 MassiveExpando 答案,因为它为您提供字典支持,但是通过将其公开为字段,它可以节省实现的麻烦IDictionary 大约有 15 个成员。

public class Flexpando : DynamicObject {
    public Dictionary<string, object> Dictionary
        = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

    public override bool TrySetMember(SetMemberBinder binder, object value) {
        Dictionary[binder.Name] = value;
        return true;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result) {
        return Dictionary.TryGetValue(binder.Name, out result);
    }
}

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.

public class Flexpando : DynamicObject {
    public Dictionary<string, object> Dictionary
        = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);

    public override bool TrySetMember(SetMemberBinder binder, object value) {
        Dictionary[binder.Name] = value;
        return true;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result) {
        return Dictionary.TryGetValue(binder.Name, out result);
    }
}
蹲墙角沉默 2024-12-16 11:40:12

您可以查看 MassiveMassiveExpando 这是不区分大小写的动态对象。

You may checkout Massive's implementation of a MassiveExpando which is case insensitive dynamic object.

剪不断理还乱 2024-12-16 11:40:12

更多的是出于好奇而不是解决方案:

dynamic e = new ExpandoObject();
var value = 1;
var key = "Key";

var resul1 = RuntimeOps.ExpandoTrySetValue(
    e, 
    null, 
    -1, 
    value, 
    key, 
    true); // The last parameter is ignoreCase

object value2;
var result2 = RuntimeOps.ExpandoTryGetValue(
    e, 
    null, 
    -1, 
    key.ToLowerInvariant(), 
    true, 
    out value2);  // The last parameter is ignoreCase

RuntimeOps.ExpandoTryGetValue/ExpandoTrySetValue 使用 ExpandoObject 的内部方法来控制区分大小写。 null, -1, 参数取自 ExpandoObject 内部使用的值(RuntimeOps 直接调用 ExpandoObject< 的内部方法) /code>)

请记住,这些方法此 API 支持 .NET Framework 基础结构,并且不适合直接在您的代码中使用。

More as a curiosity than as a solution:

dynamic e = new ExpandoObject();
var value = 1;
var key = "Key";

var resul1 = RuntimeOps.ExpandoTrySetValue(
    e, 
    null, 
    -1, 
    value, 
    key, 
    true); // The last parameter is ignoreCase

object value2;
var result2 = RuntimeOps.ExpandoTryGetValue(
    e, 
    null, 
    -1, 
    key.ToLowerInvariant(), 
    true, 
    out value2);  // The last parameter is ignoreCase

RuntimeOps.ExpandoTryGetValue/ExpandoTrySetValue use internal methods of ExpandoObject that can control the case sensitivity. The null, -1, parameters are taken from the values used internally by ExpandoObject (RuntimeOps calls directly the internal methods of ExpandoObject)

Remember that those methods are This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

月依秋水 2024-12-16 11:40:12

另一种解决方案是通过从 System.Dynamic.DynamicObject 派生并重写 TryGetValueTrySetValue 来创建类似 ExpandoObject 的类>。

Another solution is to create a ExpandoObject-like class by deriving from System.Dynamic.DynamicObject and overriding TryGetValue and TrySetValue.

你又不是我 2024-12-16 11:40:12
public static class IDictionaryExtensionMethods
{
  public static void AddCaseInsensitive(this IDictionary dictionary, string key, object value)
  {
    dictionary.Add(key.ToUpper(), value);
  }

  public static object Get(this IDictionary dictionary, string key)
  {
    return dictionary[key.ToUpper()];
  }
}
public static class IDictionaryExtensionMethods
{
  public static void AddCaseInsensitive(this IDictionary dictionary, string key, object value)
  {
    dictionary.Add(key.ToUpper(), value);
  }

  public static object Get(this IDictionary dictionary, string key)
  {
    return dictionary[key.ToUpper()];
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文