简体   繁体   English

如何在 C#/Visual Studio 中调试谓词?

[英]How to debug Predicates in C#/Visual Studio?

In debugging mode, if I hover over a predicate, what I see is just some type names and some non-understandable symbols.在调试模式下,如果我 hover 超过一个谓词,我看到的只是一些类型名称和一些不可理解的符号。 This makes it very difficult to debug a code, for example to know what predicate some variable is holding.这使得调试代码变得非常困难,例如要知道某个变量所持有的谓词。 I usually assign this predicates values using lambda expression.我通常使用 lambda 表达式来分配这个谓词值。 Is there any way to have some idea of what the predicates contain?有什么方法可以了解谓词包含的内容吗?

For example, if I have a Predicate<object> myPred variable or a List<Predicate<object>> predList variables, how can I debug what value myPred has or what predList contains at runtime?例如,如果我有一个Predicate<object> myPred变量或一个List<Predicate<object>> predList变量,我如何调试 myPred 具有什么值或 predList 在运行时包含什么?

You probably want Expression<Predicate<T>> .您可能想要Expression<Predicate<T>> It can be converted to Predicate<T> in order to call it, but retains the information about the lambda structure.它可以转换为Predicate<T>以便调用它,但保留有关 lambda 结构的信息。

[I haven't checked the C# IDE experience, but actually the VS2010 VB.NET experience.] 【我没查过C# IDE的经验,其实是VS2010 VB.NET的经验。】

Either use Expression as @BenVoigt suggests, or don't use anonymous lambdas for your predicates: (VB.NET answer: Use Functions named by you and specify them with the AddressOf operator.)要么按照@BenVoigt 的建议使用Expression ,要么不要为您的谓词使用匿名 lambda:(VB.NET 回答:使用您命名的Functions并使用AddressOf运算符指定它们。)

C# answer is something like: declare explicit functions named by you and specify the function name when assigning the predicate. C# 答案类似于:声明由您命名的显式函数,并在分配谓词时指定 function 名称。

Here is my test VB.NET code that confirms at least one way of dynamically creating predicates can be named successfully.这是我的测试 VB.NET 代码,它确认至少可以成功命名一种动态创建谓词的方法。 In the VB.NET IDE these are easily seen by name.在 VB.NET IDE 中很容易看到这些名称。

Module Module1

Sub Main()
    For i = 1 To 2
        'Dim p As Predicate(Of Object) = Function(o) (o Is Nothing)
        'Dim p As Predicate(Of Object) = AddressOf NamedPredicate
        Dim p As Predicate(Of Object) = GeneratePredicate(i)
        Dim q As Expressions.Expression(Of Predicate(Of Object)) = Function(o) (o IsNot Nothing)
        If p(q) Then Console.WriteLine((q.Compile)(p))
    Next
End Sub
Private Function NamedPredicate(ByVal o As Object) As Boolean
    Return (o Is Nothing)
End Function
Private Function GeneratePredicate(ByVal i As Integer) As Predicate(Of Object)

    Dim gp = New Reflection.Emit.DynamicMethod("DynPred" & i, GetType(Boolean), {GetType(Object)})
    Dim mb = gp.GetILGenerator
    mb.Emit(Reflection.Emit.OpCodes.Ldarg, 0)
    mb.Emit(Reflection.Emit.OpCodes.Ldnull)
    mb.Emit(Reflection.Emit.OpCodes.Ceq)
    If i = 2 Then
        mb.Emit(Reflection.Emit.OpCodes.Ldc_I4_0)
        mb.Emit(Reflection.Emit.OpCodes.Ceq)
    End If
    mb.Emit(Reflection.Emit.OpCodes.Ret)
    GeneratePredicate = DirectCast(gp.CreateDelegate(GetType(Predicate(Of Object))), Predicate(Of Object))
End Function
End Module

if you mean that in such example如果你的意思是在这样的例子中

new List<int>()
.Select(i => i + 1);

you would like to debug i + 1 part then you can put your mouse cursor (caret) somewhere at i + 1 and press F9 that will add a breakpoint in that expression您想调试 i + 1 部分,然后您可以将鼠标 cursor(插入符号)放在 i + 1 的某个位置,然后按 F9 将在该表达式中添加一个断点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM