将匿名类型强制转换为接口?

发布于 2025-01-04 23:46:42 字数 552 浏览 5 评论 0原文

这似乎不可能吧?那么最好的解决方法是什么?扩展/动态?

public interface ICoOrd {
    int x { get; set; }
    int y { get; set; }
}      

...

ICoOrd a = new {x = 44, y = 55};

参考:

This doesn't seem to be possible? So what is the best work-around? Expando / dynamic?

public interface ICoOrd {
    int x { get; set; }
    int y { get; set; }
}      

...

ICoOrd a = new {x = 44, y = 55};

ref:

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

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

发布评论

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

评论(3

初见 2025-01-11 23:46:42

最好的“解决方法”是创建并使用实现该接口的普通“命名”类型。

但如果您坚持使用匿名类型,请考虑使用动态接口代理框架,例如 ImpromptuInterface

 var myInterface =  new { x = 44, y = 55 }.ActLike<ICoOrd>();

The best "workaround" is to create and use a normal, "named" type that implements the interface.

But if you insist that an anonymous type be used, consider using a dynamic interface proxy framework like ImpromptuInterface.

 var myInterface =  new { x = 44, y = 55 }.ActLike<ICoOrd>();
我也只是我 2025-01-11 23:46:42

不,匿名类型从不实现接口。 dynamic 也不会让您转换到接口,但会让您只访问这两个属性。请注意,匿名类型是内部,因此如果您想使用动态跨程序集使用它们,则需要使用InternalsVisibleTo

No, anonymous types never implement interfaces. dynamic wouldn't let you cast to the interface either, but would let you just access the two properties. Note that anonymous types are internal, so if you want to use them across assemblies using dynamic, you'll need to use InternalsVisibleTo.

弱骨蛰伏 2025-01-11 23:46:42

我知道这是一个古老的问题和答案,但我偶然发现了这一点,希望对某些单元测试做完全相同的事情。然后我想到我已经在使用 Moq (facepalm) 做类似的事情了。

我确实喜欢 ImpromptuInterface 的其他建议,但我已经在使用 Moq 了,感觉它拥有更多的追随者(这是观点而不是事实)会更稳定,支持时间更长。

所以对于这种情况,就像

public interface ICoOrd
{
    int X { get; set; }
    int Y { get; set; }
}

public class Sample
{

    public void Test()
    {
        var aCord = new Mock<ICoOrd>();
        aCord.SetupGet(c => c.X).Returns(44);
        aCord.SetupGet(c => c.Y).Returns(55);

        var a = aCord.Object;
    }
}

编辑:只是添加另一种方式来模拟电线,开始这样做,并且更喜欢它。

public void AnotherTest()
{
    var aCord = Mock.Of<ICoOrd>(c => c.X == 44 && c.Y == 55);
    //do stuff with aCord
}

I know this is an old question and answers but I stumbled on this on this looking to do exactly the same thing for some unit tests. I then occurred to me that I am already doing something like this using Moq (facepalm).

I do like the other suggestion for ImpromptuInterface but I am already using Moq and feel like it having a larger following (that is opinion and not fact) will be more stable and supported longer.

so for this case would be something like

public interface ICoOrd
{
    int X { get; set; }
    int Y { get; set; }
}

public class Sample
{

    public void Test()
    {
        var aCord = new Mock<ICoOrd>();
        aCord.SetupGet(c => c.X).Returns(44);
        aCord.SetupGet(c => c.Y).Returns(55);

        var a = aCord.Object;
    }
}

EDIT: just adding another way to mock the cord, started doing it this way and like it a little better.

public void AnotherTest()
{
    var aCord = Mock.Of<ICoOrd>(c => c.X == 44 && c.Y == 55);
    //do stuff with aCord
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文