chobo2
chobo2

Reputation: 85875

Generic ConvertTo?

I am trying to figure out how I could do some sort of ConvertTo while using reflection and generics.

I have 2 concrete files with properties (could be: simple types, nullable times, DateTime)

I want to do mapping between these files. One will be the source file and one will the destination file.

I have ran into a problem though of how do I convert? Say I have a string that looks like a GUID. How could I convert this?

Yet, I need something to handle at the same times strings to int, double, decmial or whatever.

I know there are mappers out there like automapper and injecter but since my name don't match it makes it hard to use these as there is no real naming convention. I actually have 3rd file that will have all the names and what they map to.

Example

Source Class

 public class Source
    {
        public string test1 { get; set; }
        public int test2 { get; set; }
        public int? test3 { get; set; }
        public double test4 { get; set; }
        public double? test5 { get; set; }
        public DateTime test6 { get; set; }
        public DateTime? test7 { get; set; }
        public int test8 { get; set; }
        public string test9 { get; set; }

    }

 Source source = new Source()
            {
                test1 = "hi",
                test2 = 1,
                test3 =  null,
                test4 = 50.50,
                test5 = null,
                test6 = DateTime.Now,
                test7 = null,
                test8 = 50,
                test9 = Guid.NewGuid().ToString()                    
            };

Destination

 public class Destination
    {
        public string Test1 { get; set; }
        public int Test2 { get; set; }
        public int? Test3 { get; set; }
        public double Test4 { get; set; }
        public double? Test5 { get; set; }
        public DateTime Test6 { get; set; }
        public DateTime Test7 { get; set; }

        public double Test8 { get; set; }

        public Guid Test9 { get; set; }
    }

Before you say that automapper can handle lowercase vs upper case not that is just an example my casing in my actual project is alot different. I am trying to to convert a double to an int and a string to guid.

here is my mapping file

{
  "test1": {
    "to": "Test1"
  },
  "test2": {
    "to": "Test2"
  },
  "test3": {
    "to": "Test3"
  },
  "test4": {
    "to": "Test4"
  },
  "test5": {
    "to": "Test5"
  },
  "test6": {
    "to": "Test6"
  },
  "test7": {
    "to": "Test7"
  },
  "test8": {
    "to": "Test8"
  },
  "test9": {
    "to": "Test9"
  }
}

Upvotes: 0

Views: 170

Answers (1)

Vir
Vir

Reputation: 652

I would start with that third file and build a Dictionary<String, Type> that would map names to C# types. You didn't provide examples of your files so just sample code here:

Dictionary<String, Type> map = new Dictionary<String, Type>();
foreach(KeyValuePair<String, String> nameToTypeName in entry_in_third_file)
{
    map.Add(nameToTypeName.Key, Type.GetType(nameToTypeName.Value));
}

Next actuall convertion:

foreach (KeyValuePair<String, String> nameAndValue in entry_in_properties)
{
    Type targetType = map[nameAndValue.Key];
    String value = nameAndValue.Value;
    Object convertedValue = System.Convert.ChangeType(value, targetType);
}

Of course that's just basic example, without peek into those files you have it's hard to tell more. You'll need to create that types dictionary depending on the direction of conversion.

Also this is just for simple string to object conversions. If you need something more sophisticated then take a look at TypeConverter which allows you to create own converting classes for each type you need.

Upvotes: 1

Related Questions