Tuesday, February 14, 2012

[Csharp] Dynamic Property to Get Name of Property

In my project, I see a greatly function. I can help developer to get name of property as a string and know it right or wrong in debug or test.

Here is a demo:

   1:      public static class EntityHelper
   2:      {
   3:          public static dynamic GetProperties(this ClassDemo obj)
   4:          {
   5:              return GetDynaObject(obj);
   6:          }
   7:   
   8:          public static dynamic Properties(this Type type)
   9:          {
  10:              PropertyInfo[] properties = type.GetProperties();
  11:              Dictionary<string, string> propertyList = properties.ToDictionary(propertyInfo => propertyInfo.Name,
  12:                                                                                propertyInfo => propertyInfo.Name);
  13:              var myDyna = new DynamicUtility(propertyList, type.Name);
  14:              return myDyna;
  15:          }
  16:   
  17:          private static DynamicUtility GetDynaObject(object targetObject)
  18:          {
  19:              PropertyInfo[] properties = targetObject.GetType().GetProperties();
  20:              Dictionary<string, string> propertyList = properties.ToDictionary(propertyInfo => propertyInfo.Name,
  21:                                                                                propertyInfo => propertyInfo.Name);
  22:              var myDyna = new DynamicUtility(propertyList, targetObject.GetType().Name);
  23:              return myDyna;
  24:          }
  25:      }
  26:   
  27:      public class DynamicUtility : DynamicObject
  28:      {
  29:          private readonly string _baseType;
  30:   
  31:          private readonly Dictionary<string, string> _properties;
  32:   
  33:          public DynamicUtility(Dictionary<string, string> properties, string baseType)
  34:          {
  35:              _properties = properties;
  36:              _baseType = baseType;
  37:          }
  38:   
  39:          public override bool TryGetMember(GetMemberBinder binder, out object result)
  40:          {
  41:              if (_properties.ContainsKey(binder.Name))
  42:              {
  43:                  result = _properties[binder.Name];
  44:                  return true;
  45:              }
  46:              throw new Exception(string.Format("Property '{0}.{1}' is not existed.", _baseType, binder.Name));
  47:          }
  48:      }

And how to run it:
   1:  public class ClassDemo
   2:      {
   3:          public string StringProperty { get; set; }
   4:          public int IntProperty { get; set; }
   5:          public float FloatProperty { get; set; }
   6:   
   7:          private dynamic _properties;
   8:          public virtual dynamic Properties
   9:          {
  10:              get
  11:              {
  12:                  _properties = _properties ?? this.GetProperties();
  13:                  return _properties;
  14:              }
  15:          }
  16:      }
  17:   
  18:      class Program
  19:      {
  20:          static void Main(string[] args)
  21:          {
  22:              var classDemo = new ClassDemo();
  23:   
  24:              Console.WriteLine(classDemo.Properties.StringProperty);
  25:              Console.WriteLine(classDemo.Properties.IntProperty);
  26:              Console.WriteLine(classDemo.Properties.FloatProperty);
  27:   
  28:              Console.ReadKey(true);
  29:          }
  30:      }

So when we use it. Imagine you are working on a big project, with many functions and files. So some functions' parameters are a string (it's an property of object class). So, in normally, you can use constant string like this "MyProperty". Then all your Unit Test will pass, although you add wrong string property. Dynamic property now is useful. Can you agree!
Enjoy it!

No comments:

Post a Comment