C# 强制运算符?

发布于 2024-09-26 15:50:27 字数 855 浏览 4 评论 0原文

我得到了这个测试:

[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,表达式表达式,类型convertToType)
...
在 AutoMapper.DelegateFactory.CreateGet(MethodInfo 方法)

不幸的是 - 我无法以较小的规模重现这一点,并且无法弄清楚到底发生了什么。

什么是强制运算符?


可能有用。但我未能提取和简化必要的信息位。

I got this test:

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

It throws a bit strange exception:

Test 'Unit.Web.Bootstrap.AutoMapperFacts.EverythingIsMappedJustFine' failed:
System.InvalidOperationException : No coercion operator is defined
between types 'System.Void' and 'System.Object'.
at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
...
at AutoMapper.DelegateFactory.CreateGet(MethodInfo method)

Unfortunately - I couldn't reproduce this on smaller scale and can't figure out what exactly is going on.

What is coercion operator?


This might be useful. But I'm failing to extract and dumb down necessary information bits.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把人绕傻吧 2024-10-03 15:50:27

我仍然不知道强制运算符到底是什么,但至少 - 我解决了我的问题找到了原因。

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

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 { }
}

事实证明非常简单 - 源具有与目标属性一样命名的方法。

I still don't know what exactly is coercion operator, but at least - I solved my problem found reason.

After some automapper debugging was able to reproduce problem:

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 { }
}

Quite simple as it turns out - source has method which is named just like destination property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文