concept dynamic code in category .net

appears as: dynamic code, dynamic code
C# in Depth

This is an excerpt from Manning's book C# in Depth.

The two neatest workarounds for this problem to date require later versions of .NET: one designed by Marc Gravell (http://mng.bz/9m8i) uses expression trees (which you’ll meet in chapter 9) to build dynamic methods; the other uses the dynamic features of C# 4. You’ll see an example of the latter in chapter 14. But, as you can tell by the descriptions, both of these are dynamic—you have to wait until execution time to see whether your code will work with a particular type. There are a few workarounds that still use static typing, but they have other disadvantages (surprisingly enough, they can sometimes be slower than the dynamic code).

One word of warning that I’ll repeat throughout the chapter—it’s worth being careful with dynamic typing. It’s fun to explore, and it’s been well implemented, but I still recommend that you think carefully before using it heavily. Just like any other new feature, weigh the pros and cons rather than rushing into it just because it’s neat (which it undoubtedly is). The framework does a fine job of optimizing dynamic code, but it’ll be slower than static code in most cases. More important, you lose a lot of compile-time safety. Whereas unit testing will help you find a lot of the mistakes that can crop up when the compiler isn’t able to help you much, I still prefer the immediate feedback of the compiler telling me if I’m trying to use a method that doesn’t exist or can’t be called with a given set of arguments.

Listing 14.17. The results of compiling dynamic code
[CompilerGenerated] private static class <Main>o__SiteContainer0 { #1   public static CallSite<Func<CallSite, object, string>> <>p__Site1;   public static CallSite<Func<CallSite, string, object, object>>        <>p__Site2; } private static void Main() {   string text = "text to cut";   object startIndex = 2;   if (<Main>o__SiteContainer0.<>p__Site1 == null) {  #2     <Main>o__SiteContainer0.<>p__Site1 =        CallSite<Func<CallSite, object, string>>.Create(         new CSharpConvertBinder(typeof(string),           CSharpConversionKind.ImplicitConversion, false));   }   if (<Main>o__SiteContainer0.<>p__Site2 == null) { #3     <Main>o__SiteContainer0.<>p__Site2 =        CallSite<Func<CallSite, string, object, object>>.Create(         new CSharpInvokeMemberBinder(CSharpCallFlags.None,           "Substring", typeof(Snippet), null,             new CSharpArgumentInfo[] {               new CSharpArgumentInfo(                CSharpArgumentInfoFlags.UseCompileTimeType, null), #4              new CSharpArgumentInfo(                CSharpArgumentInfoFlags.None, null) }));   }   string substring = #5     <Main>o__SiteContainer0.<>p__Site1.Target.Invoke(       <Main>o__SiteContainer0.<>p__Site1,        <Main>o__SiteContainer0.<>p__Site2.Target.Invoke(         <Main>o__SiteContainer0.<>p__Site2, text, startIndex)); } #1 – 1 Call-sites storage #2 – 2 Creates conversion call site #3 – 3 Creates substring call site #4 – 4 Preserves text type #5 – 5 Invocation of both calls !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!