コード例 #1
0
        private string ChangeProperty(Property propertyClone)
        {
            if (propertyClone == null)
                return String.Empty;

            ElementBuilder eb = CodeRush.Language.GetActiveElementBuilder();

            // convert auto-implemented property...
            if (propertyClone.IsAutoImplemented)
            {
                // so it's no longer an auto-implemented property....
                propertyClone.IsAutoImplemented = false;
                // remove all nodes and create new ones...
                propertyClone.RemoveAllNodes();

                // create a field variable...
                string varName = CodeRush.Strings.Get("FormatFieldName", propertyClone.Name);
                Variable var = eb.BuildVariable(propertyClone.MemberTypeReference, varName);

                // add property getter...
                Get getter = GetGetter(eb, varName);
                propertyClone.AddNode(getter);

                // create property setter...
                Set setter = GetSetter(eb, propertyClone, varName);
                propertyClone.AddNode(setter);

                return CodeRush.Language.GenerateElement(var) + CodeRush.Language.GenerateElement(propertyClone);
            }
            else
            {
                Set setter = propertyClone.Setter;
                if (setter == null)
                    return String.Empty;

                Assignment assignment = setter.Nodes[0] as Assignment;
                if (assignment == null)
                    return String.Empty;

                string varName = assignment.LeftSide.Name;
                propertyClone.RemoveNode(setter);

                // recreate setter...
                Set newSetter = GetSetter(eb, propertyClone, varName);
                propertyClone.AddNode(newSetter);
            }
            return CodeRush.Language.GenerateElement(propertyClone);
        }
コード例 #2
0
    /// <summary>
    /// Tries to add member as a property.
    /// </summary>
    /// <param name="contractClass">The contract class.</param>
    /// <param name="member">The member.</param>
    /// <returns>
    ///   <c>true</c> if the member was a property and added; otherwise <c>false</c>
    /// </returns>
    private bool TryAddMemberAsProperty(Class contractClass, Member member)
    {
      Contract.Requires(contractClass != null, "contractClass is null.");
      Contract.Requires(member != null, "member is null.");

      var interfaceProperty = member as Property;
      if (interfaceProperty == null)
      {
        return false;
      }

      var contractProperty = new Property();
      this.SetupMemberDeclaration(contractProperty, interfaceProperty);
      contractProperty.AddNode(this.CreateGetter(interfaceProperty));
      contractProperty.AddNode(CreateSetter(interfaceProperty));

      contractClass.CloneDocComment(interfaceProperty);
      contractClass.AddNode(contractProperty);
      return true;
    }