void WriteTranslatorBody <TFrom, TTo>(ILWriter writer) { var sourceProperties = typeof(TFrom).GetPublicPropertiesWithGetter(); var targetProperties = typeof(TTo).GetPublicPropertiesWithSetter().ToDictionary(p => p.Name); var targetedSourceProperties = sourceProperties.GetTranslatableProperties <TTo>(); if (targetProperties.Count == 0) { writer.ReturnNull(); return; } var localTargetInstance = writer.DeclareLocal <TTo>(); writer.New <TTo>(); writer.SetLocal(localTargetInstance); foreach (var sourceProperty in sourceProperties) { var explicitMapping = GetExplicitMappingIfPresent <TTo>(sourceProperty); var targetName = (explicitMapping == null) ? sourceProperty.Name : explicitMapping.PropertyName; if (!targetProperties.ContainsKey(targetName)) { if (this.shouldThrowExceptions) { throw new PropertyIsMissingException(); } continue; } var targetProperty = targetProperties[targetName]; var propertiesAreSameType = sourceProperty.PropertyType == targetProperty.PropertyType; if (!propertiesAreSameType && !sourceProperty.PropertyType.HasConversionTo(targetProperty.PropertyType)) { if (this.shouldThrowExceptions) { throw new PropertyTypeMismatchException(); } continue; } var localSourceValue = writer.DeclareLocal(sourceProperty.PropertyType); writer.LoadFirstParameter(); writer.GetPropertyValue(sourceProperty); writer.SetLocal(localSourceValue); writer.LoadLocal(localTargetInstance); writer.LoadLocal(localSourceValue); if (!propertiesAreSameType) { writer.Cast(sourceProperty.PropertyType, targetProperty.PropertyType); } writer.SetPropertyValue(targetProperty); } writer.LoadLocal(localTargetInstance); writer.Return(); writer.VerifyStack(); }