Home > Code Samples > How to programmatically verify a context

How to programmatically verify a context

September 25th, 2010

Here’s a sample code of how to programmatically verify a particular context inside of your DXCore plug-in. You may achieve this using the Context service like this:

CSharp code:

ContextResult result = CodeRush.Context.Satisfied(@"Focus\Documents\Source\Code Editor");
bool editorHasFocus = result == ContextResult.Satisfied;

A sequence of context checks must be wrapped inside BeginCheck and EndCheck methods like this:

bool AllContextsAreSatisfied(string[] contextsToCheck)
{
  CodeRush.Context.BeginCheck();
  try
  {
    foreach (string contextName in contextsToCheck)
    {
      ContextResult result = CodeRush.Context.Satisfied(contextName);
      if (result != ContextResult.Satisfied)
        return false;
    }
  }
  finally
  {
    CodeRush.Context.EndCheck();
  }
  return true;
}

Visual Basic code:

Dim result As ContextResult = CodeRush.Context.Satisfied("Focus\Documents\Source\Code Editor")
Dim editorHasFocus As Boolean = result = ContextResult.Satisfied

A sequence of context checks:

Function AllContextsAreSatisfied(ByVal contextsToCheck As String()) As Boolean
  CodeRush.Context.BeginCheck()
  Try
    For Each contextName As String In contextsToCheck
      Dim result As ContextResult = CodeRush.Context.Satisfied(contextName)
      If (result <> ContextResult.Satisfied) Then
        Return False
      End If
    Next
  Finally
    CodeRush.Context.EndCheck()
  End Try
  Return True
End Function
—–
Products: DXCore
Versions: 9.1 and up
VS IDEs: any
Updated: Nov/01/2010
ID: D013

Similar Posts: