mercredi 28 mars 2018

How to change data types of properties in c#

Im trying to change data type of objects dynamically , here is my scenario I have data in a data table , and i need to map it to objects in c#

Here are steps i have followed

  1. Loop through data table and fetch each data row
  2. get columns inside that loop
  3. Pass column name , new object to assign values to properties , data table cell value to new method.

here is sample code

            m1()
            {
                foreach (DataRow row in inputTable.Rows)
                {
                    foreach (DataColumn col in inputTable.Columns)
                    {
                        m2(col.caption,row[col.caption].toString(),new product())
                    }
                }
            }

            m1(string columnName,string cellValue,product mappingEntity){
                foreach (var prop in entityToMap.GetType().GetProperties())
                    {
                        if (prop.Name.ToLower() == column.ToLower())
                        {
                            prop.SetValue(entityToMap, GetConverter(t)(cellValue));
                            break;
                        }
                        else if (prop.Name == "Site")
                        {
                            entityToMap.Site = MapSite(column, cellValue, new Domain.Entities.Site());
                            break;
                        }
                        else if (prop.Name == "Product_categories")
                        {
                            entityToMap.Product_categories.Add(MapProductToCategory(column, cellValue, new Domain.Entities.ProductToCategory()));
                        }
                        else if (prop.Name == "Category_searchgroups")
                        {
                            entityToMap.Category_searchgroups.Add(MapCategoryToSearchGroup(column, cellValue, new Domain.Entities.CategoryToSearchGroup()));
                        }
            }

Now i need to dynamically change data types of assigning values.

 if (prop.Name.ToLower() == column.ToLower())
    {
      Type t = prop.PropertyType;
      prop.SetValue(entityToMap, GetConverter(t)(cellValue));
      break;
    }

so i have found type inference question here Change data type dynamically in c#

 static Func<string, T> GetConverter<T>(T value)
        {
            return (x) => Convert<T>(x);
        }
    static T Convert<T>(string val)
    {
        Type destiny =typeof(T);

        // See if we can cast           
        try
        {
            return (T)(object)val;
        }
        catch { }

        // See if we can parse
        try
        {
            return (T)destiny.InvokeMember("Parse", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public, null, null, new object[] { val });
        }
        catch { }

        // See if we can convert
        try
        {
            Type convertType = typeof(Convert);
            return (T)convertType.InvokeMember("To" + destiny.Name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public, null, null, new object[] { val });
        }
        catch { }

        // Give up
        return default(T);
    }

the issue is i have reflection object , i cant pass reflection object because it's not valid at that context , Can anyone help me to resolve this ?? thanks





Aucun commentaire:

Enregistrer un commentaire