【发布时间】:2013-01-04 10:46:51
【问题描述】:
我必须重写DynamicObject 的哪个方法才能根据使用实例的上下文获得不同的行为(在动态类中)?
这是我想要完成的一个示例:
class DynamicTest : DynamicObject
{
public DynamicTest(string xyz)
{
_xyz = xyz;
}
private string _xyz;
//TODO: what do I need to implement to get required behaviour?
}
class Program
{
static void Main(string[] args)
{
dynamic foo = new DynamicTest("test);
if (foo) // treat foo as boolean
{ // jump in here when _xyz of foo has a value
System.Console.WriteLine(foo); //treat foo as string
}
else
{ // jump in here when _xyz of foo is null
System.Console.WriteLine("No Value In Object");
}
}
}
【问题讨论】:
-
请问您为什么要这样做?为什么不想访问某个谓词中的字段?
-
在python中,一个有值的字段被认为是真,没有值是假的。我想要类似的行为。
标签: c# .net dynamic dynamicobject