C#强制运营商?

时间:2010-10-08 14:14:39

标签: c# linq automapper linq-expressions

我得到了这个测试:

[Fact]
public void EverythingIsMappedJustFine(){
  new AutoMapperTask().Execute();
  Mapper.AssertConfigurationIsValid();
}

它引发了一个奇怪的例外:

  

测试'Unit.Web.Bootstrap.AutoMapperFacts.EverythingIsMappedJustFine'失败:
   System.InvalidOperationException:未定义强制运算符
   类型'System.Void'和'System.Object'之间    在System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType,Expression expression,Type convertToType)
  ...
  在AutoMapper.DelegateFactory.CreateGet(MethodInfo方法)

不幸的是 - 我无法在较小的范围内重现这一点,也无法弄清楚到底发生了什么。

什么是强制运营商?


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

1 个答案:

答案 0 :(得分: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 { }
}

事实证明非常简单 - source的方法就像目标属性一样命名。