Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

You might consider using a DLR based language like Python in silverlight

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#.

dynamic duck = /* pretty much anything */
duck.Quack();

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 supports Silverlight 4. It not only allows for duck typing static implementations but dynamic ones as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top