Browse by Tags
All Tags »
Dynamic (
RSS)
First, I’ll start with a little disclaimer: this post is not about whether using dynamic is better/worse than static typing. Instead, it’s about making it more convenient to use dynamic if you choose to go that route . Clearly, some people dislike dynamic, as you can see in the comments in that post from Phil Haack , and for the most part, all the key arguments for/against have been made. So anyway, let’s proceed… Recently, a few people have experimented with extending their view pages from ViewPage<dynamic>. The idea is to then be able to access model data using the more convenient dynamic syntax. e.g. check out this thread on StackOverflow, as well as Phil’s post I mention above. One limitation that people are hitting is that you can...
Recently, I started playing around with C# dynamic, and blogged how it could be used to call static class members late bound . Today, I was talking to Phil Haack , who I think had talked to ScottGu , and he mentioned that it would be cool to use dynamic to simplify data access when you work directly with SQL query. So I thought I’d play around with that, and it didn’t take much code to make it work nicely. So the scenario is that you’re not using any fancy O/R mapper like LINQ to SQL or Entity Framework, but you’re directly using ADO.NET to execute raw SQL commands. It’s not something that I would personally do, but there are a lot of folks who prefer this over the higher level data access layers. So let’s look at an example...
By now, you’ve probably heard that C# 4.0 is adding support for the dynamic keyword, which introduces some aspects of dynamic languages to C#. I had not had a chance to really try it, but recently I was reading Bertrand Le Roy’s post on the topic, and was sort of looking for a good opportunity to use it. Today, I found a scenario which I thought it would work great for, but it turned out not to be supported out of the box! The scenario is to call static class members using dynamic. That probably sounds crazy, so let’s look at an example. Say you have these two classes: public class Foo1 { public static string TransformString(string s) { return s.ToLower(); } public static string MyConstant { get { return "Constant from...
More Posts