C#强制运营商?

Arn*_*psa 4 c# linq automapper linq-expressions

我得到了这个测试:

[Fact]
public void EverythingIsMappedJustFine(){
  new AutoMapperTask().Execute();
  Mapper.AssertConfigurationIsValid();
}
Run Code Online (Sandbox Code Playgroud)

它引发了一个奇怪的异常:

测试"Unit.Web.Bootstrap.AutoMapperFacts.EverythingIsMappedJustFine"失败:
System.InvalidOperationException:没有强制经营者被定义
类型"System.Void"和"System.Object的"之间.
在System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType,表达式表达,类型convertToType)
...
在AutoMapper.DelegateFactory.CreateGet(MethodInfo的方法)

不幸的是 - 我无法在较小规模上重现这一点,也无法弄清楚到底发生了什么.

什么是强制运营商?


可能很有用.但我没有提取和愚弄必要的信息位.

Arn*_*psa 5

我仍然不知道究竟是什么强制算子,但至少 - 我解决了我的问题找到了原因.

经过一些自动化调试后,能够​​重现问题:

namespace mappertest
{
    using AutoMapper;
    using NUnit.Framework;

    [TestFixture]
    public class FooFacts
    {
        [Test]
        public void MapToFizz()
        {
            Mapper.Initialize(c => c.AddProfile(new FooProfile()));

            var foo = new Foo { Bar = "BarValue" };
            var fooModel = Mapper.Map<Foo, FooModel>(foo);

            Assert.AreEqual("BarValue", fooModel.Bar);
        }
    }

    public class FooProfile : Profile
    {
        protected override void Configure()
        {
            CreateMap<Foo, FooModel>();
        }
    }

    public class Foo
    {
        public string Bar { get; set; }
        public void Fizz() { }
    }

    public class FooModel
    {
        public string Bar { get; set; }
        public FizzModel Fizz { get; set; }
    }

    public class FizzModel { }
}
Run Code Online (Sandbox Code Playgroud)

事实证明非常简单 - source有一个方法,就像目标属性一样命名.