< RealProxy'是' SomeInterface> operator show' true'在调试器视图中但运行时它是“假”'

时间:2018-06-10 12:16:15

标签: c# .net-4.7.1

标题中描述的这个相互矛盾的结果令人难以置信(就像任何直到解释的那样),但是让我用VS调试会话中的快照向你证明。在(1)您可以看到运行时结果' false'在(2)调试器视图结果' true'同样的表达和...他们不平等!这怎么可能?

debug session

.NET运行时:v4.7.1

public class ShallowProxy : RealProxy
{
    private readonly object _id;

    [PermissionSet(SecurityAction.LinkDemand)]
    public ShallowProxy(Type myType, object id) : base(myType)
    {
        _id = id;
    }

    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
    public override IMessage Invoke(IMessage myIMessage)
    {
        if (myIMessage is IMethodCallMessage methodCall)
        {
            if (methodCall.MethodName.Equals("GetType"))
                return new ReturnMessage(GetProxiedType(), null, 0, methodCall.LogicalCallContext, methodCall);
            if (methodCall.MethodName.Equals("get_Id", StringComparison.OrdinalIgnoreCase))
                return new ReturnMessage(_id, null, 0, methodCall.LogicalCallContext, methodCall);
            if (methodCall.MethodName.Equals("ToString"))
                return new ReturnMessage(ToString(), null, 0, methodCall.LogicalCallContext, methodCall);
            if (methodCall.MethodName.Equals("Equals"))
                return new ReturnMessage(Equals(methodCall.InArgs[0]), null, 0, methodCall.LogicalCallContext, methodCall);
            if (methodCall.MethodName.Equals("GetHashCode"))
                return new ReturnMessage(0, null, 0, methodCall.LogicalCallContext, methodCall);
        }

        throw new InvalidOperationException("Cannot call shallow object");
    }

    public override string ToString()
    {
        return $"{{Id: {_id}}}";
    }

    public override bool Equals(object o)
    {
        return o == this;
    }
}

0 个答案:

没有答案