Showing posts with label Generics for Dictionary conversion. Show all posts
Showing posts with label Generics for Dictionary conversion. Show all posts

Tuesday, December 18, 2012

Convert Dictionary into Strongly Typed Class


There are few things you have to consider before conversion of dictionary into strongly typed class object.

1 - To detect in which class you want to convert into dictionary. For this purpose you need generic (Template) where you will specify in which class you want to convert.

2 - Get the properties of the strongly typed class using reflection.

3 - Create the object of the Generic . and get the reference of it.

4 - Iterate the properties of object and search the dictionary contains that field or column, if its find and writeable then set the value using reflection but cast the values into the strongly typed of a column too.

Here's the demo.

   class Emp {
      public string Name { get; set; }
      public string TechnologyExpert { get; set; }
      public int Salary { get; set; }
      public override string ToString(){
         return Name + "-----" + TechnologyExpert + "-----" + Salary;
      }
   }
   class Cust{
      public string Name { get; set; }
      public string Company { get; set; }
      public override string ToString(){
         return Name + "------" + Company;
      }
   }

/// Please use angle bracket here too.
   class ConvertIntoStronglyTyped  where T: new() {
      public static T ConvertMe(Dictionary objectDict)
      {
         var properties = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.DeclaredOnly);
       
         Object obj = new T();
       
         foreach (var property in properties)
            if (property.CanWrite && objectDict.ContainsKey(property.Name))            
               property.SetValue(obj, Cast(property.PropertyType.FullName, objectDict[property.Name]), null);                  

         return (T)obj;
      }

      private static object Cast(string propertyTypeName, object value) {
         string strFullName = typeof(string).FullName;
         string intFullName = typeof(int).FullName;
         string floatFullName = typeof(float).FullName;

         if (strFullName == propertyTypeName)   return value.ToString();
         if (intFullName == propertyTypeName)   return int.Parse(value.ToString());
         if (floatFullName == propertyTypeName) return int.Parse(value.ToString());
       
         return value;
      }
   }

Execute the code.

class Program
   {
      static void Main(string[] args)
      {
         Dictionary consolidatedValues = new Dictionary();
       
         consolidatedValues.Add("Name", "Sohail");
         consolidatedValues.Add("TechnologyExpert", "C#");
         consolidatedValues.Add("Salary", "200");
         consolidatedValues.Add("Company", "ABC .Com");

// I noticed the less than greater than bracket is not visible, use simple the angle brackets.
         Emp emp = ConvertIntoStronglyTyped&ltEmp&gt.ConvertMe(consolidatedValues);

         Cust cust = ConvertIntoStronglyTyped.ConvertMe(consolidatedValues);
         Console.WriteLine(emp);
         Console.WriteLine(cust);
      }
   }

Please let me know incase you are facing issue and have more questions regarding the reflections or generics.