C# AutoMapper 映射到动态对象或从动态对象映射?报错??
官方教程:Dynamic and ExpandoObject Mapping — AutoMapper documentation代码如下:
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; }
}}
报错信息:
Message:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : “Map”方法没有采用“1”个参数的重载
Stack Trace:
CallSite.Target(Closure , CallSite , IMapper , Object )
UpdateDelegates.UpdateAndExecute2(CallSite site, T0 arg0, T1 arg1)
DynamicAndExpandoObjectMappingTest.Test_DynamicAndExpandoObjectMapping() line 28 本帖最后由 凉游浅笔深画眉 于 2024-11-21 17:53 编辑
MyDynamicObject 应该继承 DynamicObject 这种不应该到CSDN或者cnblogs去问么 这是什么啊?看不懂啊
凉游浅笔深画眉 发表于 2024-11-21 17:50
MyDynamicObject 应该继承 DynamicObject
感谢回复,继承了还是报这个错误。我就纳闷,官方文档会不会错了。 killjd 发表于 2024-11-21 19:50
这种不应该到CSDN或者cnblogs去问么
去不了一点。 chen714714 发表于 2024-11-21 23:20
这是什么啊?看不懂啊
我也不懂啊。{:1_908:} 凉游浅笔深画眉 发表于 2024-11-21 17:50
MyDynamicObject 应该继承 DynamicObject
我自己测试是可以的,你应该是AutoMapper版本问题,文档用的可能是旧版本。
我用的 AutoMapper:13.0.1
```
using AutoMapper;
using ConsoleApp14;
using System.Dynamic;
namespace ConsoleApp14
{
/// <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; }
}
}
internal class Program
{
public static void Test_DynamicAndExpandoObjectMapping()
{
dynamic foo = new ExpandoObject();
foo.Bar = 5;
foo.Baz = 6;
var configuration = new MapperConfiguration(cfg =>
{
});
var mapper = configuration.CreateMapper();
var result = mapper.Map<Foo>(foo);
}
static void Main(string[] args)
{
Test_DynamicAndExpandoObjectMapping();
}
}
``` 不是展示了,没有带一个参数的重载方法 本帖最后由 Pojie1999.0909 于 2024-11-22 20:49 编辑
凉游浅笔深画眉 发表于 2024-11-22 10:08
我自己测试是可以的,你应该是AutoMapper版本问题,文档用的可能是旧版本。
我用的 AutoMapper:13.0. ...
我用的11版本,周一再试试。{:1_893:}{:1_893:}
页:
[1]
2