1
dynamic test = new ExpandoObject();
test.A = "ok";

try{
    Console.WriteLine(test.B);
}
catch (Microsoft.CSharp.RuntimeBinder.RuntimeBinderException ex){
    // how can i know that B was invoke?
}

As code above, test.B is not a member of 'test'. But how can i know 'B' is calling. The only way i found is looking into ex.Message but it's not a proper way.

Bonshington
  • 3,970
  • 2
  • 25
  • 20

2 Answers2

2

Try using the StackFrame:

catch (Exception e)
{
    StackTrace st = new StackTrace();
    StackTrace st1 = new StackTrace(new StackFrame(true));
    Console.WriteLine(" Stack trace for Method1: {0}",
       st1.ToString());
    Console.WriteLine(st.ToString());
    throw e;
}
KMån
  • 9,896
  • 2
  • 31
  • 41
2

It will be easier if you use DynamicObject instead of ExpandoObject.

class MyDynamicObject : DynamicObject
{
    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        Console.WriteLine(binder.Name);
         //simply prints the name, you can raise an event here or something else
        return base.TryGetMember(binder, out result);
    }
}
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174