Geeks With Blogs
Path Notes of a Kodefu Master blog

We've all been told to think of a dynamic object as a regular object but with dynamic behavior. Kevin Hazzard peeked behind the curtain to show that the compiler is actually doing a lot with that keyword; it isn't just an object. It does change the type to an object, but then it builds CallSites everywhere that object is referenced. These CallSites handle the calls to the dynamic language runtime.

That's great, but we have an object. How does a consumer of your class library know that a property you marked as dynamic is dynamic when it is compiled to an object? To figure this out, I compiled a simple class then reflected it.

    public class TestDynamic
    {
        
public dynamic Foo { get; set; }

        
public void Bar()
        {
            
Console.WriteLine(Foo);
        }
    }

I refactored the reflected class to make it easier to read.

    public class TestManual
    {
        [
Dynamic]
        
private object k__BackingField;

        
public void Bar()
        {
            
if (SiteContainer.Site1 == null)
            {
                
SiteContainer.Site1 = CallSite<Action<CallSite, Type, object>>.Create(new CSharpInvokeMemberBinder(CSharpCallFlags.None, "WriteLine", typeof(TestDynamic), null, new CSharpArgumentInfo[] { new CSharpArgumentInfo(CSharpArgumentInfoFlags.IsStaticType | CSharpArgumentInfoFlags.UseCompileTimeType, null), new CSharpArgumentInfo(CSharpArgumentInfoFlags.None, null) }));
            }
            
SiteContainer.Site1.Target(SiteContainer.Site1, typeof(Console), this.Foo);
        }

        [
Dynamic]
        
public object Foo { [return: Dynamic] get; [param: Dynamic] set; }

        
private static class SiteContainer
        {
            
public static CallSite<Action<CallSite, Type, object>> Site1;
        }
    }

The magic is all in the new DynamicAttribute class. This attribute is unnecessary for the assembly you compiled, but it is important for anyone referencing that assembly. The compiler will use that attribute to know that it has to treat the property, field, or method as though the dynamic keyword were used.

When I saw that, I imagined the possibilities. What if I make a class have static behavior in my assembly but dynamic behavior in a calling assembly? I set out to make this happen, but then I ran into a big roadblock: 

Do not use 'System.Runtime.CompilerServices.DynamicAttribute'. Use the 'dynamic' keyword instead.

That's unfortunate, and I couldn't find a way around it. I see nothing being reflected back from the DynamicAttribute class, so the error must be baked into the compiler.

So, it appears that you can't use this class yourself. However, it is useful to know when reflecting a class that you can check for that attribute if you need to know if it is a dynamic type. Though, I do suspect an IsDynamic property will appear on the Type class sooner or later.

Posted on Thursday, June 4, 2009 5:18 PM Kodefu | Back to top


Comments on this post: DynamicAttribute

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Chris Eargle | Powered by: GeeksWithBlogs.net