protected override void OnInitialize() { LastInvoke = InvokedMethod.Initialize; switch (CurrentMode) { case TestMode.MoryxException: throw new TestException(); case TestMode.SystemException: throw new Exception("I am done here!"); } }
public static void Invoke(InvokedMethod method) { if (instance == null) { throw new NotInitializedException(); } if (instance.InvokeRequired) { instance.Invoke(method); return; } method(); }
/// <summary> /// Gets the names and types of the arguments passed in the invocation. /// </summary> /// <param name="expression">An <see cref="InvocationExpressionSyntax"/> representing the invocation.</param> /// <param name="symbol">An <see cref="IMethodSymbol"/> representing the method being invoked.</param> /// <param name="model">A <see cref="SemanticModel"/> for the project used to get type information.</param> /// <param name="context">An <see cref="InvokedMethod"/> representing the context in which the provided <paramref name="symbol"/> is invoked.</param> /// <returns>Returns a range of tuples containing the names and types of the arguments passed in the invocation.</returns> public static IEnumerable <TypeInfo?> GetArgumentTypes(this InvocationExpressionSyntax expression, IMethodSymbol symbol, SemanticModel model, InvokedMethod context) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } if (expression.ArgumentList == null) { yield break; } for (int i = 0; i < expression.ArgumentList.Arguments.Count; i++) { TypeInfo?argumentType = null; var argument = expression.ArgumentList.Arguments[i].ChildNodes().FirstOrDefault(); if (argument != null) { SymbolInfo symbolInfo = model.GetSymbolInfo(argument); if (symbolInfo.Symbol?.Kind == SymbolKind.Parameter) { var parameter = context.Method.Parameters.FirstOrDefault(p => p.Name == symbolInfo.Symbol.Name); var parameterIndex = context.Method.Parameters.IndexOf(parameter); argumentType = context.ArgumentTypes.ElementAt(parameterIndex); } else { argumentType = model.GetTypeInfo(argument); } } yield return(argumentType); } }
/// <summary> /// Gets the type on which the method invocation of <paramref name="expression"/> is being performed. /// </summary> /// <param name="expression">An <see cref="InvocationExpressionSyntax"/> representing the invocation.</param> /// <param name="model">A <see cref="SemanticModel"/> for the project used to get type information.</param> /// <param name="context">An <see cref="InvokedMethod"/> representing the context in which another method is invoked.</param> /// <returns>Returns a <see cref="TypeInfo"/> instance, or <c>null</c> if no type information could be determined.</returns> public static TypeInfo?GetTargetType(this InvocationExpressionSyntax expression, SemanticModel model, InvokedMethod context) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } TypeInfo?result = null; var memberAccessExpressionSyntax = expression.Expression as MemberAccessExpressionSyntax; if (memberAccessExpressionSyntax != null) { if (memberAccessExpressionSyntax.Expression.Kind() == SyntaxKind.ThisExpression) { result = context.TargetType; } else { result = model.GetTypeInfo(memberAccessExpressionSyntax.Expression); } } return(result); }
/// <summary> /// Gets the target of the provided <paramref name="expression"/> using the provided <paramref name="symbol"/> and <paramref name="model"/>. /// </summary> /// <param name="expression">An <see cref="InvocationExpressionSyntax"/> instance describing the invocation of a particular method.</param> /// <param name="symbol">An <see cref="IMethodSymbol"/> describing the method being invoked by the <paramref name="expression"/>.</param> /// <param name="model">A <see cref="SemanticModel"/> that can be used to resolve type information.</param> /// <param name="context">An <see cref="InvokedMethod"/> that represents the context in which to determine the target of the invocation.</param> /// <returns>Returns a <see cref="TypeInfo"/> instance representing the type of the target being invoked, or <c>null</c> if the type could not be determined.</returns> public static TypeInfo?GetTargetOfInvocation(this InvocationExpressionSyntax expression, IMethodSymbol symbol, SemanticModel model, InvokedMethod context) { TypeInfo?targetTypeInfo = expression.Expression.Kind() == SyntaxKind.IdentifierName ? context.TargetType : expression.GetTargetType(model, context); if (targetTypeInfo.HasValue) { MemberAccessExpressionSyntax memberAccess = expression.Expression as MemberAccessExpressionSyntax; IdentifierNameSyntax identifier = memberAccess?.Expression as IdentifierNameSyntax ?? expression.Expression as IdentifierNameSyntax; if (identifier != null) { SymbolInfo identifierSymbol = model.GetSymbolInfo(identifier); switch (identifierSymbol.Symbol.Kind) { case SymbolKind.Local: VariableDeclaratorSyntax syntax = identifierSymbol.Symbol.GetComparableSyntax()?.GetSyntax() as VariableDeclaratorSyntax; if (syntax != null && syntax.ChildNodes().Any()) { targetTypeInfo = model.GetTypeInfo(syntax.ChildNodes().First().ChildNodes().First()); } break; case SymbolKind.Parameter: if (context.Method != null) { var parameter = context.Method.Parameters.FirstOrDefault(p => p.Name == identifierSymbol.Symbol.Name); var parameterIndex = context.Method.Parameters.IndexOf(parameter); targetTypeInfo = context.ArgumentTypes.ElementAt(parameterIndex); } break; case SymbolKind.Field: SyntaxNode node = expression.Parent; while (!(node is BlockSyntax)) { node = node.Parent; } var statementsInBlock = node.ChildNodes().ToList(); var currentStatementIndex = statementsInBlock.IndexOf(expression.Parent); var assignments = statementsInBlock.Take(currentStatementIndex) .OfType <ExpressionStatementSyntax>() .Where(s => s.Expression.IsKind(SyntaxKind.SimpleAssignmentExpression)) .Select(s => s.Expression as AssignmentExpressionSyntax) .Where(s => model.GetSymbolInfo(s.Left).Symbol == identifierSymbol.Symbol); AssignmentExpressionSyntax lastAssignment = assignments.LastOrDefault(); if (lastAssignment != null) { targetTypeInfo = model.GetTypeInfo(lastAssignment.Right); } break; default: break; } } } return(targetTypeInfo); }
protected void OnInvokedMethod(ServiceProxyInvokeEventArgs args) { InvokedMethod?.Invoke(this, args); }