Ejemplo n.º 1
0
 public bool TryAccessorAssignment(SyntaxKind type, ISymbol leftSymbol, ISymbol rightSymbol, JsExpression left, JsExpression right, out JsExpression result)
 {
     // Special handling for property and event assignments, since those have to be translated to a set/add/remove_PropertyName invocation.
     if ((!(leftSymbol is IPropertySymbol) || !IsMinimizedAutoProperty((IPropertySymbol)leftSymbol)) && (type == SyntaxKind.SimpleAssignmentExpression || type == SyntaxKind.AddAssignmentExpression || type == SyntaxKind.SubtractAssignmentExpression))
     {
         if (leftSymbol is IPropertySymbol || leftSymbol is IEventSymbol)
         {
             var isExported = leftSymbol.IsExported();
             var nameOverride = leftSymbol.GetAttributeValue<string>(Context.Instance.JsAttributeType, "Name");
             var target = left.GetLogicalTarget();
             var arguments = new List<JsExpression>();
             var property = leftSymbol as IPropertySymbol;
             var isCall = false;
             if (left is JsInvocationExpression)
             {
                 var invocationExpression = (JsInvocationExpression)left;
                 if (invocationExpression.Target is JsMemberReferenceExpression)
                 {
                     var innerMemberReference = (JsMemberReferenceExpression)invocationExpression.Target;
                     if (innerMemberReference.Name == "call" && target is JsMemberReferenceExpression && ((JsMemberReferenceExpression)target).Name.StartsWith("get_"))
                     {
                         isCall = true;
                         target = target.GetLogicalTarget();
                     }
                 }
                 
                 arguments.AddRange(invocationExpression.Arguments);
             }
             arguments.Add(right);
             if (leftSymbol is IPropertySymbol)
             {
                 var methodSymbol = ((IPropertySymbol)leftSymbol).SetMethod;
                 JsExpression inline;
                 if (TryInline(methodSymbol, target, arguments.ToArray(), out inline))
                 {
                     result = inline;
                     return true;
                 }
             }
             if (isExported)
             {
                 IMethodSymbol methodSymbol;
                 if (leftSymbol is IPropertySymbol)
                     methodSymbol = ((IPropertySymbol)leftSymbol).SetMethod;
                 else
                 {
                     var @event = (IEventSymbol)leftSymbol;
                     switch (type)
                     {
                         case SyntaxKind.AddAssignmentExpression:
                             methodSymbol = @event.AddMethod;
                             break;
                         default:
                             methodSymbol = @event.RemoveMethod;
                             break;
                     }
                 }
                 target = target.Member(methodSymbol.Name);
                 if (isCall)
                     target = target.Member("call");
                 result = target.Invoke(arguments.ToArray());
                 return true;
             }
             else if (nameOverride != null && property != null && property.SetMethod.Parameters.Count() > 1)
             {
                 result = target.Member(nameOverride).Invoke(arguments.ToArray());
                 return true;
             }
         }
     }
     result = null;
     return false;
 }