MichaelvdV@Atos

Having fun with AF and Dynamic Programming

Blog Post created by MichaelvdV@Atos on Nov 2, 2010

Having fun with AF and dynamic programming

I haven't been really active on vCampus lately due to time constraints. We are working on some pretty exciting projects here at Atos, but that takes it's toll on my vCampus activity. I thought it would be time to make use of my newly earned Blogging priviliges, and talk some geek

 

Today, let's talk about dynamic programming with c# and .NET 4.0. For those of you who aren't using Visual Studio 2010, and if you have the chance, you should really switch. Besided having the awesome benefits of .NET 4.0, the UI has changed a lot, and has some really cool features you should check out.

 

Allright, dynamic programming in C#. What's that about?

 

 
 public class DAFElement : DynamicObject
    {
        public AFElement EmbeddedElement { get; set; }

 

        public DAFElement(AFElement embeddedElement)
        {
            EmbeddedElement = embeddedElement;
        }

 

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = null;
            var name = binder.Name.Replace("_", " ");
            if (EmbeddedElement.Attributes.Contains(name))
            {
                result = new DAFAttribute(EmbeddedElement.Attributes[name]);
                return true;
            }
            if (EmbeddedElement.Elements.Contains(name))
            {
                result = new DAFElement(EmbeddedElement.Elements[name]);
                return true;
            }
         
            var prop = EmbeddedElement.GetType().GetProperty(name);
            if (prop != null)
            {
                result = prop.GetValue(EmbeddedElement, null);
                return true;
            }
           
            return false;
        } 
    }

Outcomes