0

I have a Type TDestination with Name, FullName, etc. The method recieves a generic type TSource

I convert from one to another like this:

private static Type ConvertTypes<TSource>()
    {
        var mytype = typeof(TSource);
        var newName = string.Format("{0}.{1}", Assembly.GetAssembly(typeof(Plugin)).FullName.Split(',')[0].ToString(), mytype .Name);
        var newFullName = Assembly.GetAssembly(typeof(Plugin)).GetType(newName).FullName;
        var TDestination = Type.GetType(newFullName);
        return TDestination;
    }

I need to use it in a Generic Method like Method<T>();

I tried some coding but I was unable to convert the Type to it's generic version.

It all begun when I needed to create a method that recieves Func<TSource, bool> and this func needs to be mapped to Func<TDestination, bool>.

I recieve a Model class and must convert the func to TDestination that is an Entity Framework class.

Until now, no success.

Anyone?

Dima
  • 6,721
  • 4
  • 24
  • 43
programad
  • 1,291
  • 3
  • 19
  • 38
  • So your target type has the same class name but it a different namespace or assembly? – Maess Dec 09 '13 at 18:07
  • Yes, you are correct, sir. Same name, different namespace and assembly. – programad Dec 09 '13 at 18:09
  • I suggest you just use a config driven mapper to map between type names then, you could remove much of the expensive reflection. – Maess Dec 09 '13 at 18:13
  • Could you elaborate a little sample, Maess? – programad Dec 09 '13 at 18:14
  • Use app settings or a custom config to map one type name to another, then you can just get the target type name from the config and call Type.GetType(newFullName); – Maess Dec 09 '13 at 18:15
  • That's nice and will relieve some reflection code. Thanks. But I still can't see how to get the generic type from the type itself. – programad Dec 09 '13 at 18:19
  • I'm not sure what you mean by Generic type? Are you talking about an abstract class or some base class? – Maess Dec 09 '13 at 18:20
  • When I watch TSource, it shows the full name without quotes. When I watch the TDestination it shows a Type, a whole object, with lots of stuff, Name, Full Name, etc. I don't know how to reffer to this difference. the TSource came from the "generics" signature, and the TDestination is a type I created. – programad Dec 09 '13 at 18:23
  • 1
    Have you looked at invoking your `Method()` using reflection? http://stackoverflow.com/questions/232535/how-to-use-reflection-to-call-generic-method – Eugene S. Dec 09 '13 at 19:44

1 Answers1

0

I suggest you to use existed solution for mapping between types: AutoMapper or Emit Mapper. There's also ValueInjecter.

Dima
  • 6,721
  • 4
  • 24
  • 43
  • All the solutions I searched on SackOverflow talk about a Mapper.CreateMapExpression method that I simply can't access. Like this one: http://stackoverflow.com/questions/7424501/automapper-for-funcs-between-selector-types – programad Dec 09 '13 at 18:11