public class Animal : DynamicObject
{
Dictionary<string, object> dictionary = new Dictionary<string, object>();
public override bool TryGetMember(
GetMemberBinder binder, out object result)
{
string name = binder.Name.ToLower();
return dictionary.TryGetValue(name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
dictionary[binder.Name.ToLower()] = value;
return true;
}
}
public class Bird : Animal
{
}
然后将其称为您的示例:
dynamic obj = new Bird();
obj.Fly = new Action(() => Console.Write("Yes I can"));
obj.Fly();
有关更多信息,请查看DynamicObject