投匿名类型的接口? [英] cast anonymous type to an interface?

查看:133
本文介绍了投匿名类型的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎并不可能吗?那么,什么是最好的解决方法?为Expando /动态?

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:

  • http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.internalsvisibletoattribute.aspx (Jon's suggestion)

推荐答案

最好的解决方法,是创建和使用正常的,命名实现该接口类型。

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

但是,如果你坚持认为,一个匿名类型使用,请考虑使用动态接口代理框架如 ImpromptuInterface

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>();

这篇关于投匿名类型的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆