0

I've met a strange issue from a type extends DynamicObject. I even tried the sample from MSDN:

// The class derived from DynamicObject. 
public class DynamicDictionary : DynamicObject
{
    // The inner dictionary.
    Dictionary<string, object> dictionary
        = new Dictionary<string, object>();

    // This property returns the number of elements 
    // in the inner dictionary. 
    public int Count
    {
        get
        {
            return dictionary.Count;
        }
    }

    // If you try to get a value of a property  
    // not defined in the class, this method is called. 
    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        // Converting the property name to lowercase 
        // so that property names become case-insensitive. 
        string name = binder.Name.ToLower();

        // If the property name is found in a dictionary, 
        // set the result parameter to the property value and return true. 
        // Otherwise, return false. 
        return dictionary.TryGetValue(name, out result);
    }

    // If you try to set a value of a property that is 
    // not defined in the class, this method is called. 
    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        // Converting the property name to lowercase 
        // so that property names become case-insensitive.
        dictionary[binder.Name.ToLower()] = value;

        // You can always add a value to a dictionary, 
        // so this method always returns true. 
        return true;
    }
}

Usage:

dynamic d = new DynamicDictionary();
d.FirstName = "Jeff"; // stack overflow

The code works find with a new simple console, but it just throws StackOverflowException from a huge WPF application. In the WPF we have other dynamic code using ExpandoObject, but it's failed for DynamicObject:

Stack Overflow

Both the WPF project and a console are .NET 4.0 (Full Profile). Can someone share some idea about that?

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52
  • How are you using it in the WPF app? Are you using it in reflection-based binding? Are you able to use it at *all* in the WPF app? Can you provide a sample WPF app where it fails, making that as short as possible? – Jon Skeet Sep 03 '15 at 21:16
  • Just using it in a normal Command binds to a menu item. I don't know what's reflection-based binding, I just use it as simple as the code shown above. I've also tried in a new WPF project, drop a button, write code on the click handler, and it works just fine. The tricky thing is that WPF app is HUGE so I cannot simply get a short one to reproduce it. – Jeffrey Zhao Sep 03 '15 at 21:31
  • What exactly do you mean by "using it in a normal Command binds to a menu item"? Are you using it in binding? Just because your current app is huge doesn't mean you can't cut it down. Take a copy, and then start butchering it - remove as much as you can so that it *just* shows the problem. – Jon Skeet Sep 03 '15 at 21:34
  • Yes, it's an ICommand object and bind to an menu item. The dynamic code is in the Execute method. I'll try to cut it down, but it would be a lot of work so I'm trying to see if some one has idea. Another thing is, since the application is modulized, so the actually code loaded is not very big, but still, it seems the environment is compromised so I cannot run this. I'm not using any fancy technologies and everything is managed code so it's really strange. Also, I'm trying to trace into the very beginning of the stacks and into DLR to see what causes stack overflow. – Jeffrey Zhao Sep 03 '15 at 21:40

0 Answers0