Possibility to have an hybrid dynamic type

Topics: C# Language Design
Sep 10, 2014 at 10:59 AM
Hello,

I often have the necessity in my objects that part of my object is known, but other properties are unknown and are added during runtime. Currently I help myself implementing a list where I add the new "properties" with an Add() method.

The problem is, that it is rather complicated to use bindings on these dynamic properties. Also when coding there is only a all or nothing solution. You have either late binding or dynamic binding, not both.

So my request would be to have following
dynamic<Type> myObject;
construct. In this case it is possible to access all known members with regular mechanisms, and all the others with the DynamicObject mechanisms.

Does this make sense?

Best
Martin
Sep 11, 2014 at 12:25 AM
I don't think a program language is the right place for that.

What you seem to be looking for is a special binding manager. And for that you must look at the framework/platform you're binding with.

As for your proposed syntax:
dynamic<Type> myObject;
I had already proposed (with no success) the same syntax for the opposite goal.

The meaning would not be "it's statically this and dynamically whatever else" like you propose but "it's statically typed like this but dynamically dispatched". Kind of a contract for dynamic objects.
Sep 11, 2014 at 2:27 AM
Could you use a non-dynamic type with a single dynamic property for any dynamic properties? Something like this:
    class PartiallyDynamicThing
    {
        public string FamilyName { get; set; }
        public dynamic DynamicData { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var partiallyDynamicThing = new PartiallyDynamicThing
            {
                FamilyName = "Griffin",
                DynamicData = new ExpandoObject()
            };

            partiallyDynamicThing.DynamicData.StreetAddress = "31 Spooner Street";

            Console.WriteLine("The {0} family lives at {1}.",
                partiallyDynamicThing.FamilyName,
                partiallyDynamicThing.DynamicData.StreetAddress);
        }
    }
Marked as answer by msedi on 9/11/2014 at 2:01 AM
Sep 11, 2014 at 8:42 AM
Right now, the easiest way is to just use a property of type dynamic with a bag object (the System.Dynamic.ExpandoObject is presumably efficiently implemented) to have the dynamic operations be dynamic and the rest be static. I would also like to have an easier way to partially dip into dynamic behavior. As I understand, this was the original plan for C# 4, especially the idea of keeping around as much static context and state as possible to inform the dynamism, but it was complicated to explain and also limited the dynamic behavior. Chris Burrows posted about the old design and the change and maybe those posts show the reason why it's such an all-or-nothing affair right now.
Sep 11, 2014 at 9:01 AM
csdahlberg
I guess you are right. This seems to be a good solution.
Thanks
Martin