8

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 ?

Kumar
  • 10,997
  • 13
  • 84
  • 134

5 Answers5

8

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);
    }
}
Community
  • 1
  • 1
  • 2
    I like the simplicity but feel there should be a little more control over the internals. Perhaps two constructors, one empty that defaults to what you have here and one that takes an IDictionary, either constructor sets the Dictionary field, which I'd change into a public property with a private setter. – Jeremy Cook Jun 06 '14 at 20:57
5

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

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Interesting, wasn't aware of this fork of massive, this hasn't been updated since feb so will look into the differences, thanks ! – Kumar Oct 14 '11 at 20:05
1

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.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • obsolete does not compile in .net 4 http://msdn.microsoft.com/en-us/library/dd782193.aspx – Kumar Oct 14 '11 at 21:33
  • @Kumar I have to tell the truth... Yes, I had tested the code on Visual Studio 2010 and it worked. Just to be sure this morning I tested it again and it worked :-) Interestingly the `ExpandoTryGetValue` and `ExpandoTrySetValue` aren't marked obsolete on my computer so I didn't receive any warning. – xanatos Oct 15 '11 at 05:32
  • hmm, interesting, not sure what could be different here! I could not compile, so went looking for answers and found the msdn page ! i have vs2010 ultimate sp1 on xp sp3 ! there's no beta's or anything on this pc either and not familiar of a .net 4 sp1 or preview either, any thoughts ? – Kumar Oct 17 '11 at 14:43
  • @Kumar Using vs 2010 pro on xp sp3 here here, so it isn't the problem. And I don't have the .NET 4.5 or anything similar. Mah. My runtime version is v4.0.30319 (I looked in Visual Studio at the Microsoft.CSharp reference)... MMmh... Do you have the async ctp installed or the mvc 3.0? They modify the runtime, if I remember correctly – xanatos Oct 17 '11 at 14:45
0

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

Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65
0
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()];
  }
}
Brandon
  • 68,708
  • 30
  • 194
  • 223
RBZ
  • 2,034
  • 17
  • 34