我试图将我的AutoMapper代码转换为更流畅的API,例如 现有代码:将对象转换为泛型类型 - automapper静态扩展
Model.Foo target = Mapper.Map<Contract.Foo, Model.Foo>(source);
我想什么的代码看起来是这样的
Model.Foo target = source.ConvertTo<Model.Foo>();
我开始写我的扩展方法,但我似乎无法得到这个工作。
public static class AutoMapperConverterExtension
{
public static T ConvertTo<T>(this string source) where T : new()
{
Type sourceType = Type.GetType(source);
if (IsMapExists<sourceType, T>()) // complains here! cannot resolve 'sourceType'. If I use inline, won't compile.
{
return Mapper.Map<T>(source);
}
throw new NotImplementedException("type not supported for conversion");
}
public static bool IsMapExists<TSource, TDestination>()
{
return (AutoMapper.Mapper.FindTypeMapFor<TSource, TDestination>() != null);
}
}
,并且您实施有什么问题? – Servy 2014-09-22 18:57:26
*抱怨这里*关于什么? – 2014-09-22 18:59:17
更新后 - 基本上不会编译。 – Raymond 2014-09-22 19:19:51