本文整理汇总了C#中UnaryOperatorExpression类的典型用法代码示例。如果您正苦于以下问题:C# UnaryOperatorExpression类的具体用法?C# UnaryOperatorExpression怎么用?C# UnaryOperatorExpression使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UnaryOperatorExpression类属于命名空间,在下文中一共展示了UnaryOperatorExpression类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: VisitUnaryOperatorExpression
public override void VisitUnaryOperatorExpression (UnaryOperatorExpression unaryOperatorExpression)
{
base.VisitUnaryOperatorExpression (unaryOperatorExpression);
if (unaryOperatorExpression.Operator != UnaryOperatorType.Not)
return;
var expr = unaryOperatorExpression.Expression;
while (expr != null && expr is ParenthesizedExpression)
expr = ((ParenthesizedExpression)expr).Expression;
var binaryOperatorExpr = expr as BinaryOperatorExpression;
if (binaryOperatorExpr == null)
return;
var negatedOp = CSharpUtil.NegateRelationalOperator(binaryOperatorExpr.Operator);
if (negatedOp == BinaryOperatorType.Any)
return;
if (IsFloatingPoint (binaryOperatorExpr.Left) || IsFloatingPoint (binaryOperatorExpr.Right)) {
if (negatedOp != BinaryOperatorType.Equality && negatedOp != BinaryOperatorType.InEquality)
return;
}
AddIssue (unaryOperatorExpression, ctx.TranslateString ("Simplify negative relational expression"),
script => script.Replace (unaryOperatorExpression,
new BinaryOperatorExpression (binaryOperatorExpr.Left.Clone (), negatedOp,
binaryOperatorExpr.Right.Clone ())));
}
示例2: VisitUnaryOperatorExpression
public override object VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression, object data)
{
if (unaryOperatorExpression.Op == UnaryOperatorType.Not) {
return unaryOperatorExpression.Expression.AcceptVisitor(this, data) == SymbolDefined ? null : SymbolDefined;
} else {
return null;
}
}
示例3: TrickyCast3
public void TrickyCast3()
{
Expression expr = new UnaryOperatorExpression(
UnaryOperatorType.Not, new IdentifierExpression("a")
).CastTo(new SimpleType("MyType"));
Assert.AreEqual("(MyType)!a", InsertRequired(expr));
Assert.AreEqual("(MyType)(!a)", InsertReadable(expr));
}
示例4: TrickyCast1
public void TrickyCast1()
{
Expression expr = new UnaryOperatorExpression(
UnaryOperatorType.Minus, new IdentifierExpression("a")
).CastTo(new PrimitiveType("int"));
Assert.AreEqual("(int)-a", InsertRequired(expr));
Assert.AreEqual("(int)(-a)", InsertReadable(expr));
}
示例5: DoubleNegation
public void DoubleNegation()
{
Expression expr = new UnaryOperatorExpression(
UnaryOperatorType.Minus,
new UnaryOperatorExpression(UnaryOperatorType.Minus, new IdentifierExpression("a"))
);
Assert.AreEqual("- -a", InsertRequired(expr));
Assert.AreEqual("-(-a)", InsertReadable(expr));
}
示例6: VisitUnaryOperatorExpression
public override void VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression)
{
if (unaryOperatorExpression.Operator == UnaryOperatorType.Await) {
_errorReporter.Message(7998, unaryOperatorExpression.GetRegion(), "await");
_result = false;
}
else {
base.VisitUnaryOperatorExpression(unaryOperatorExpression);
}
}
示例7: GetControlVariable
static VariableInitializer GetControlVariable(VariableDeclarationStatement variableDecl,
UnaryOperatorExpression condition)
{
var controlVariables = variableDecl.Variables.Where (
v =>
{
var identifier = new IdentifierExpression (v.Name);
return condition.Expression.Match (identifier).Success;
}).ToList ();
return controlVariables.Count == 1 ? controlVariables [0] : null;
}
示例8: VisitUnaryOperatorExpression
public override void VisitUnaryOperatorExpression(UnaryOperatorExpression unaryOperatorExpression)
{
if (unaryOperatorExpression.Operator == UnaryOperatorType.Await)
{
var tryBlock = unaryOperatorExpression.GetParent<TryCatchStatement>();
if (tryBlock != null)
{
this.Found = true;
}
}
base.VisitUnaryOperatorExpression(unaryOperatorExpression);
}
示例9: GetValueFromUnaryExpression
public static dynamic GetValueFromUnaryExpression(UnaryOperatorExpression expr)
{
var value = GetValueFromExpression(expr.Expression);
if (value == null)
return null;
switch (expr.Operator)
{
case UnaryOperatorType.Not:
return !value;
case UnaryOperatorType.Minus:
return -value;
case UnaryOperatorType.Plus:
return +value;
default:
return null;
}
}
示例10: VisitUnaryOperatorExpression
public override void VisitUnaryOperatorExpression (UnaryOperatorExpression unaryOperatorExpression)
{
base.VisitUnaryOperatorExpression (unaryOperatorExpression);
if (unaryOperatorExpression.Operator != UnaryOperatorType.Not)
return;
var innerUnaryOperatorExpr =
RemoveParentheses (unaryOperatorExpression.Expression) as UnaryOperatorExpression;
if (innerUnaryOperatorExpr == null || innerUnaryOperatorExpr.Operator != UnaryOperatorType.Not)
return;
var expression = RemoveParentheses (innerUnaryOperatorExpr.Expression);
if (expression.IsNull)
return;
AddIssue (unaryOperatorExpression, ctx.TranslateString ("Remove double negation"),
script => script.Replace (unaryOperatorExpression, expression.Clone ()));
}
示例11: GetActions
public override IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var node = context.GetNode<BinaryOperatorExpression>();
if (node == null ||
(node.Operator != BinaryOperatorType.Equality && node.Operator != BinaryOperatorType.InEquality) ||
!node.OperatorToken.Contains(context.Location))
yield break;
yield return new CodeAction(
context.TranslateString("Use 'Equals'"),
script => {
Expression expr = new InvocationExpression(GenerateTarget(context, node), node.Left.Clone(), node.Right.Clone());
if (node.Operator == BinaryOperatorType.InEquality)
expr = new UnaryOperatorExpression(UnaryOperatorType.Not, expr);
script.Replace(node, expr);
},
node.OperatorToken
);
}
示例12: VisitBinaryOperatorExpression
public override void VisitBinaryOperatorExpression (BinaryOperatorExpression binaryOperatorExpression)
{
base.VisitBinaryOperatorExpression (binaryOperatorExpression);
var match = pattern.Match (binaryOperatorExpression);
if (!match.Success)
return;
AddIssue (binaryOperatorExpression, ctx.TranslateString ("Simplify boolean comparison"), scrpit => {
var expr = match.Get<Expression> ("expr").First ().Clone ();
var boolConstant = (bool)match.Get<PrimitiveExpression> ("const").First ().Value;
if ((binaryOperatorExpression.Operator == BinaryOperatorType.InEquality && boolConstant) ||
(binaryOperatorExpression.Operator == BinaryOperatorType.Equality && !boolConstant)) {
expr = new UnaryOperatorExpression (UnaryOperatorType.Not, expr);
expr.AcceptVisitor (insertParenthesesVisitor);
}
scrpit.Replace (binaryOperatorExpression, expr);
});
}
示例13: ResolveOperator
protected bool ResolveOperator(UnaryOperatorExpression unaryOperatorExpression, OperatorResolveResult orr)
{
if (orr != null && orr.UserDefinedOperatorMethod != null)
{
var method = orr.UserDefinedOperatorMethod;
var inline = this.Emitter.GetInline(method);
if (!string.IsNullOrWhiteSpace(inline))
{
new InlineArgumentsBlock(this.Emitter, new ArgumentsInfo(this.Emitter, unaryOperatorExpression, orr, method), inline).Emit();
return true;
}
else
{
if (orr.IsLiftedOperator)
{
this.Write(Bridge.Translator.Emitter.ROOT + ".Nullable.lift(");
}
this.Write(BridgeTypes.ToJsName(method.DeclaringType, this.Emitter));
this.WriteDot();
this.Write(OverloadsCollection.Create(this.Emitter, method).GetOverloadName());
if (orr.IsLiftedOperator)
{
this.WriteComma();
}
else
{
this.WriteOpenParentheses();
}
new ExpressionListBlock(this.Emitter, new Expression[] { unaryOperatorExpression.Expression }, null).Emit();
this.WriteCloseParentheses();
return true;
}
}
return false;
}
示例14: GetActions
public override System.Collections.Generic.IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
Expression expr = null;
AstNode token;
if (!NegateRelationalExpressionAction.GetLogicalExpression (context, out expr, out token))
yield break;
var uOp = expr as UnaryOperatorExpression;
if (uOp != null) {
yield return new CodeAction(
string.Format(context.TranslateString("Invert '{0}'"), expr),
script => {
script.Replace(uOp, CSharpUtil.InvertCondition(CSharpUtil.GetInnerMostExpression(uOp.Expression)));
}, token
);
yield break;
}
var negativeExpression = CSharpUtil.InvertCondition(expr);
if (expr.Parent is ParenthesizedExpression && expr.Parent.Parent is UnaryOperatorExpression) {
var unaryOperatorExpression = expr.Parent.Parent as UnaryOperatorExpression;
if (unaryOperatorExpression.Operator == UnaryOperatorType.Not) {
yield return new CodeAction(
string.Format(context.TranslateString("Invert '{0}'"), unaryOperatorExpression),
script => {
script.Replace(unaryOperatorExpression, negativeExpression);
}, token
);
yield break;
}
}
var newExpression = new UnaryOperatorExpression(UnaryOperatorType.Not, new ParenthesizedExpression(negativeExpression));
yield return new CodeAction(
string.Format(context.TranslateString("Invert '{0}'"), expr),
script => {
script.Replace(expr, newExpression);
}, token
);
}
示例15: VisitBinaryOperatorExpression
public override void VisitBinaryOperatorExpression (BinaryOperatorExpression binaryOperatorExpression)
{
base.VisitBinaryOperatorExpression (binaryOperatorExpression);
var match = pattern.Match (binaryOperatorExpression);
if (!match.Success)
return;
var expr = match.First<Expression>("expr");
// check if expr is of boolean type
var exprType = ctx.Resolve (expr).Type.GetDefinition ();
if (exprType == null || exprType.KnownTypeCode != KnownTypeCode.Boolean)
return;
AddIssue (binaryOperatorExpression, ctx.TranslateString ("Simplify boolean comparison"), scrpit => {
var boolConstant = (bool)match.First<PrimitiveExpression>("const").Value;
if ((binaryOperatorExpression.Operator == BinaryOperatorType.InEquality && boolConstant) ||
(binaryOperatorExpression.Operator == BinaryOperatorType.Equality && !boolConstant)) {
expr = new UnaryOperatorExpression (UnaryOperatorType.Not, expr.Clone());
expr.AcceptVisitor (insertParenthesesVisitor);
}
scrpit.Replace (binaryOperatorExpression, expr);
});
}