public static object DtoAdapter(this object source, Type interfType) { if (source.GetType().GetInterfaces().Contains(interfType)) { return(source); } if (source is IDtoAdapter) { source = ((IDtoAdapter)source).GetSource(); } var sourceType = source.GetType(); var key = interfType.FullName + "#$%^%$#" + sourceType.FullName; var fact = s_factories.GetOrAdd(key, x => { IDtoAdapterFactory res = null; try { } finally { //ignore ThreadAbortException AssertInterface(interfType); AssertMapping(interfType, sourceType); if (sourceType.IsPublic) { res = new PublicDtoAdapterFactory(s_factory, sourceType, interfType); } if (sourceType.IsSealed && sourceType.Name.Contains("f__AnonymousType")) { res = new AnonymousDtoAdapterFactory(s_factory, sourceType, interfType); } else { res = new InternalDtoAdapterFactory(s_factory, sourceType, interfType); } } return(res); }); return(fact.Create(source)); }
public PublicDtoAdapterFactory(EmitTypeFactory factory, Type originalType, Type adapterType) { var unique = Guid.NewGuid(); var typeBldr = factory.CreateClass( $"generatedPublicAdapter_{originalType.Name}_{adapterType.Name}_{unique}", null, adapterType, typeof(IDtoAdapter)); var sourcePropertyName = $"Source_{unique}"; var sourceProperty = factory.AddProperty(typeBldr, sourcePropertyName, originalType); CreateProperties(factory, originalType, adapterType, typeBldr, sourceProperty); CreateConstructor(factory, originalType, typeBldr, sourceProperty); InternalDtoAdapterFactory.CreateIDtoAdapter(factory, typeBldr, sourceProperty); m_constructor = typeBldr.CreateTypeInfo().GetConstructor(new[] { originalType }); }