你還沒有發佈任何代碼,所以很難說,但這裏是一個絕對有效的示例。這是我今天正在閱讀的東西的修改版本。希望它有幫助
namespace DynamicStuff
{
public class Program
{
static void Main(string[] args)
{
dynamic brush = new Brush();
brush.Handle = "Wooden";
brush.Colour = "Red";
Console.WriteLine(brush.Handle + " " + brush.Colour);
Console.WriteLine(
"Number of dynamic properties:" + brush.Count);
}
}
public class Brush : Block
{
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
Console.Out.WriteLine("About To call TryGetMember base");
object res = null;
bool retVal = base.TryGetMember(binder, out res);
result = res;
return retVal;
}
public override bool TrySetMember(System.Dynamic.SetMemberBinder binder, object value)
{
Console.Out.WriteLine("About to call TrySetMember base");
return base.TrySetMember(binder, value);
}
}
public abstract class Block : DynamicObject
{
private Dictionary<string, object> d = new Dictionary<string, object>();
public int Count
{
get
{
return d.Count;
}
}
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
Console.Out.WriteLine("In TryGetMember in block");
string name = binder.Name.ToLower();
return d.TryGetValue(name, out result);
}
public override bool TrySetMember(
SetMemberBinder binder, object value)
{
Console.Out.WriteLine("In TrySetMember in block");
d[binder.Name.ToLower()] = value;
return true;
}
}
}
現在我完全困惑。 我已將Microsoft.CSharp.RuntimeBinder.RuntimeBinderException添加到調試程序已知的例外列表(CTRL + D,E;它之前未列入列表中),告訴調試程序不要停止此異常,和e'voila,現在它工作。 怪異... – 2010-03-12 22:39:39