官方教程:Dynamic and ExpandoObject Mapping — AutoMapper documentation
代码如下:
[C#] 纯文本查看 复制代码 [Fact]
[Fact]
public static void Test_DynamicAndExpandoObjectMapping()
{
// AutoMapper可以在没有任何显式配置的情况下映射到动态对象或从动态对象映射:
dynamic foo = new MyDynamicObject();
foo.Bar = 5;
foo.Baz = 6;
var configuration = new MapperConfiguration(cfg => { });
var mapper = configuration.CreateMapper();
var result = mapper.Map<Foo>(foo);
Assert.Equal(5, ((Foo)result).Bar);
Assert.Equal(6, ((Foo)result).Baz);
}
/// <summary>
/// Foo.
/// </summary>
internal class Foo
{
/// <summary>
/// Gets or sets Bar.
/// </summary>
public int Bar { get; set; }
/// <summary>
/// Gets or sets Baz.
/// </summary>
public int Baz { get; set; }
/// <summary>
/// Gets or sets InnerFoo.
/// </summary>
public Foo InnerFoo { get; set; }
}
/// <summary>
/// MyDynamicObjec.
/// </summary>
internal class MyDynamicObject
{
/// <summary>
/// Gets or sets Bar.
/// </summary>
public int Bar { get; set; }
/// <summary>
/// Gets or sets Baz.
/// </summary>
public int Baz { get; set; }
}}
报错信息:
[C#] 纯文本查看 复制代码 Message: 
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : “Map”方法没有采用“1”个参数的重载
Stack Trace: 
CallSite.Target(Closure , CallSite , IMapper , Object )
UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
DynamicAndExpandoObjectMappingTest.Test_DynamicAndExpandoObjectMapping() line 28 |