2014-09-08 2 views
1

Я пытался отобразить от типизированного объекта до dynamic, но это кажется невозможным.Карта от типизированного объекта до динамического

Например:

public class Customer 
{ 
    public string CustomerName {get; set;} 
    public Category Category {get; set;} 
} 

public class Category 
{ 
    public string CategoryName {get; set;} 
    public int IgnoreProp {get; set;} 
} 

Тогда я хотел бы мой результат, как показано ниже:

var customer = new Customer 
     { 
      CustomerName = "Ibrahim", 
      Category = new Category 
       { 
        CategoryName = "Human", 
        IgnoreProp = 10 
       } 
     }; 

dynamic dynamicCustomer = Mapper.Map<Customer, dynamic>(customer); 

Могу ли я настроить AutoMapper, чтобы как-то справиться с этим?

ответ

1

Похоже, возможно, следующий тест успешно:

public class SourceObject 
{ 
    public int IntProperty { get; set; } 
    public string StringProperty { get; set; } 
    public SourceObject SourceProperty { get; set; } 
} 

internal class Program 
{ 
    private static void Main(string[] args) 
    { 
     var result = AutoMapper.Mapper.Map<dynamic>(new SourceObject() {IntProperty = 123, StringProperty = "abc", SourceProperty = new SourceObject()}); 
     Console.WriteLine("Int " + result.IntProperty); 
     Console.WriteLine("String " + result.StringProperty); 
     Console.WriteLine("Object is " + (result.SourceProperty == null ? "null" : "not null").ToString()); 
     Console.ReadLine(); 
    } 
} 

Это выводит динамический объект с отображаемые свойства из SourceObject

0

Вам не нужно использовать AutoMapper для сопоставления динамического объекта:

dynamic dynamicCustomer = customer; 
Console.WriteLine(dynamicCustomer.CustomerName); // "Ibrahim" 
Console.WriteLine(dynamicCustomer.Category.CategoryName); // "Human" 
Смежные вопросы