Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态性能评价_C#_C# 4.0_Razor_Dynamic Language Runtime - Fatal编程技术网

C# 动态性能评价

C# 动态性能评价,c#,c#-4.0,razor,dynamic-language-runtime,C#,C# 4.0,Razor,Dynamic Language Runtime,对于DLR,我想做如下工作: class MyClass { int MyProperty { get; set; } } 在《剃刀》中,我会这样做。(InstanceOfMyClass是查看MyClass实例的动态对象) 这将输出MyProperty的字符串表示形式 现在如果我这样做 @InstanceOfMyClass.MyMissingProperty 我希望它输出“Missing:MyMissingProperty”。我很想捕捉整个表情,就像这样 @InstanceOfMyCl

对于DLR,我想做如下工作:

class MyClass {
   int MyProperty { get; set; }
}
在《剃刀》中,我会这样做。(
InstanceOfMyClass
是查看
MyClass
实例的动态对象)

这将输出
MyProperty
的字符串表示形式

现在如果我这样做

@InstanceOfMyClass.MyMissingProperty
我希望它输出“Missing:MyMissingProperty”。我很想捕捉整个表情,就像这样

@InstanceOfMyClass.MyMissingProperty.MoreMissing
可能会输出“Missing:MyMissingProperty.MoreMissing”,但这可能会对DLR提出很多要求


ExpandoObject
是否允许我执行此操作?如果没有,我要怎么做才能实现它?

我不确定Expando,当您想要设置属性然后获取它时,它会被使用。然而,从您编写的内容来看,您似乎希望能够读取以前未设置的任何值


为此,可以使用
DynamicObject
。您只需覆盖
TryGetMember

,您可以通过创建自己的DynamicObject版本,然后覆盖TryGetMember来实现这一点


以这种方式扩展DynamicObject.TryGetMember:

如果成员存在,则返回值。如果成员不存在,则返回一个类的新实例,该类将处理缺少的属性的字符串表示形式和链。像这样的

public class MissingPropertyChain : DynamicObject
{
    private string property;

    public MissingPropertyChain(string property)
    {
        this.property = property;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result) 
    {
        if(binder.Name == "ToString")
            result = "Missing property: " + property;
        else
            result = new MissingPropertyChain( property + "." + binder.Name;

        return true;
    }
}
我没有试过,但我想它会让你知道如何解决这个问题

希望能有帮助

public class MissingPropertyChain : DynamicObject
{
    private string property;

    public MissingPropertyChain(string property)
    {
        this.property = property;
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result) 
    {
        if(binder.Name == "ToString")
            result = "Missing property: " + property;
        else
            result = new MissingPropertyChain( property + "." + binder.Name;

        return true;
    }
}