Open GoogleCodeExporter opened 9 years ago
Here is source console app to show problem with dynamic objects:
using System;
using System.Collections.Generic;
using System.Dynamic;
namespace ConsoleApplication1
{
class Program
{
static string Script =
@"
e:Print(e.Firstname);
e.Age=20;
";
static void Main(string[] args)
{
dynamic e = new Entity();
e.Firstname = "Matt";
e.Age = 29;
LuaInterface.Lua Lua = new LuaInterface.Lua();
Lua["e"] = e;
Lua.DoString(Script, "");
e.Dispose();
Lua.Dispose();
Console.ReadLine();
}
}
class Entity : DynamicObject, IDisposable
{
Dictionary<string, object> Members = null;
public Entity()
{
Members = new Dictionary<string,object>();
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return Members.Keys;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (Members.ContainsKey(binder.Name))
{
result = Members[binder.Name];
return true;
}
else
{
result = null;
return false;
}
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
Members[binder.Name] = value;
return true;
}
public void Dispose()
{
foreach (KeyValuePair<string, object> Member in Members)
{
if (Member.Value.GetType() is IDisposable)
{
IDisposable DisposableMember = (IDisposable)Member.Value;
DisposableMember.Dispose();
}
}
Members.Clear();
Members = null;
}
public void Print(string Message)
{
Console.WriteLine(Message);
}
}
}
Original comment by coray...@gmail.com
on 14 Nov 2012 at 1:34
You would expect the Console to print 'Matt' from a call to TryGetMember in the
Entity Object.
Instead, the variable name is printed.
You then expect e.Age to be set to 29 by the Lua script. Instead an error is
thrown for an missing member.
Original comment by coray...@gmail.com
on 14 Nov 2012 at 1:36
Original issue reported on code.google.com by
coray...@gmail.com
on 14 Nov 2012 at 12:32