Silverlight 的 Duck 类型库?
是否有适用于 Silverlight 的 鸭子类型 库?有一些适用于成熟的 .NET 框架,但我正在寻找更轻量的东西。
Is there any duck typing library for Silverlight? There are a few for the full-blown .NET framework, but I'm looking for something lighter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
通过询问“Silverlight ducktyping”,您是指接口的强类型自动实现吗?或者通过使用对象实例动态创建对象元数据?
如果是后者,安东尼的推荐就很正确。
在 CLR 上运行的强类型语言(C# 和 VB.NET)无法支持真正的动态鸭子类型。因此,您可以使用作为 Silverlight DLR 项目的一部分提供的 IronRuby 或 IronPython。
如果您正在寻找前者,那么您本质上是在寻找一个模拟库。
Silverlight 支持多种开源模拟框架。
起订量 - http://code.google.com/p/moq/ (下载:http://moq.googlecode .com/files/Moq.Silverlight.4.0.812.4-bin.zip)
RhinoMocks - http://ayende.com/projects/rhino-mocks.aspx(下载:http://ayende.com/20/section.aspx/download/230)
AutoFac - http://code.google.com/p/autofac/(下载:http://autofac.googlecode.com/files/Autofac-1.4.4.572-SL3.zip)
< strong>TypeMock / SilverUnit - http://cthru.codeplex.com(下载:http://cthru.codeplex.com/Release/ProjectReleases.aspx? ReleaseId=27748#DownloadId=69201)
By asking for "Silverlight duck typing" are you refering to the strongly-typed automatic implementation of interfaces? Or the dynamic creation of object metadata through using an object instance?
If it's the latter, Anthony's recommendation is spot on.
Strongly-typed languages that run on the CLR (C# and VB.NET) cannot support true dynamic duck typing. Thus, you can use IronRuby or IronPython that ship as part of the Silverlight DLR project.
If you're looking for the former, then you're essiantially looking for a mocking library.
Silverlight supports a myriad of open source mocking frameworks.
Moq - http://code.google.com/p/moq/ (download: http://moq.googlecode.com/files/Moq.Silverlight.4.0.812.4-bin.zip)
RhinoMocks - http://ayende.com/projects/rhino-mocks.aspx (download: http://ayende.com/20/section.aspx/download/230)
AutoFac - http://code.google.com/p/autofac/ (download: http://autofac.googlecode.com/files/Autofac-1.4.4.572-SL3.zip)
TypeMock / SilverUnit - http://cthru.codeplex.com (download: http://cthru.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27748#DownloadId=69201)
您可以考虑使用基于 DLR 的语言,例如 silverlight 中的 Python
You might consider using a DLR based language like Python in silverlight
根据您对鸭子类型的解释,我希望当它在 Silverlight 上可用时,它会出现在 C# 4.0 和
dynamic
中。然后,您可以选择哪些位是鸭子类型(动态
),哪些位是静态类型(其他所有)。全部在 C# 内。Depending on your interpretation of duck-typing, I would hope that this arrives in C# 4.0 and
dynamic
, when that becomes available on Silverlight. Then you can pick-and-choose which bits are duck-typed (dynamic
) vs static-typed (everything else). All within C#.我最近创建了一个名为 "DynamicWrapper" 的实用程序。它使用 Reflection.Emit 动态生成一个实现该接口的包装类——这是一种在 C# 中实现鸭子类型的方法。
不幸的是,它在 Silverlight 中不起作用。它在 .Net 3.5 中运行得非常好,并且我的测试在 SL 环境中通过了,但在 SL 运行时中,我遇到了安全异常。
我没有时间弄清楚,但代码非常简单。也许这段代码可以为您指明正确的方向?
I recently created a utility I call "DynamicWrapper". It uses Reflection.Emit to generate a wrapper class on the fly that implements the interface -- a way to achieve duck typing in C#.
Unfortunately, it doesn't work in Silverlight. It works really well in .Net 3.5 and my tests passed in my SL environment, but in the SL runtime, I get a security exception.
I haven't had any time to figure it out, but the code is pretty straight forward. Maybe this code can point you in the right direction?
ImpromptuInterface 支持 Silverlight 4。它不仅允许鸭子类型静态实现,还允许动态实现出色地。
ImpromptuInterface supports Silverlight 4. It not only allows for duck typing static implementations but dynamic ones as well.