private string GetSource(string text)
 {
     var methodName = CreateMethodName(text);
     ElementBuilder eb = new ElementBuilder();
     var method = eb.AddMethod(CodeRush.Source.ActiveClass, "void", methodName);
     method.Visibility = MemberVisibility.Public;
     var testSection = eb.AddAttributeSection(method);
     eb.AddAttribute(testSection, "Test");
     method.AddSupportElement(testSection);
     eb.AddMethodCall(method, "Assert.Fail", new[] { "\"Not Implemented\"" });
     string source = CodeRush.Language.GenerateElement(testSection) + CodeRush.Language.GenerateElement(method);
     return source;
 }
        private void AddVirtualDisposeMethod(ElementBuilder elementBuilder)
        {
            TypeDeclaration activeType = _ActiveClass;
              Method virtualDisposeMethod = elementBuilder.AddMethod(null, null, STR_Dispose);

              if (activeType.ElementType == LanguageElementType.Struct)
              {
            virtualDisposeMethod.Visibility = MemberVisibility.Public;
              }
              else
              {
              virtualDisposeMethod.Visibility = MemberVisibility.Protected;
              virtualDisposeMethod.IsVirtual = true;
              }

              string typeName = CodeRush.Language.GetSimpleTypeName("System.Boolean");
              virtualDisposeMethod.AddParameter(new Param(typeName, "disposing"));
              If ifDisposingBlock = new If();
              ifDisposingBlock.Expression = new ElementReferenceExpression("disposing");
              virtualDisposeMethod.AddNode(ifDisposingBlock);
              // Dispose fields individually...
              foreach (BaseVariable field in _ActiveClass.AllFields)
              {
            if (!field.IsStatic && field.Is("System.IDisposable"))
              AddFieldDisposeBlock(elementBuilder, ifDisposingBlock, field);
            }
        }
 private static void AddDisposeImplementer(ElementBuilder elementBuilder)
 {
     Method disposeMethod = elementBuilder.AddMethod(null, null, STR_Dispose);
       // If implicit interface implementation is supported by the language?
       disposeMethod.Visibility = MemberVisibility.Public;
       Expression newCollection = new ElementReferenceExpression("IDisposable.Dispose");
       disposeMethod.AddImplementsExpression(newCollection);
       PrimitiveExpression booleanTrue = GetBooleanLiteral(true);
       ExpressionCollection argumentsCollection = new ExpressionCollection();
       argumentsCollection.Add(booleanTrue);
       elementBuilder.AddMethodCall(disposeMethod, STR_Dispose, argumentsCollection, null);
       string thisReference = CodeRush.Language.GenerateElement(new ThisReferenceExpression());
       elementBuilder.AddMethodCall(disposeMethod, "GC.SuppressFinalize", new string[]
       { thisReference
       });
 }
        private void AddDestructorMember(ElementBuilder elementBuilder)
        {
            Method destructor = elementBuilder.AddMethod(null, null, _ActiveClass.Name);
              destructor.MethodType = MethodTypeEnum.Destructor;

              ExpressionCollection arguments = new ExpressionCollection();
              arguments.Add(GetBooleanLiteral(false));
              elementBuilder.AddMethodCall(destructor, STR_Dispose, arguments, null);
        }
        private void WrapInTryFunction_Apply(Object sender, ApplyContentEventArgs ea)
        {
            // INITIALIZE
            Class activeClass = CodeRush.Source.ActiveClass;
            Method activeMethod = CodeRush.Source.ActiveMethod;
            var builder = new ElementBuilder();

            Logger.Log("WITF:Builder Built");

            var method = builder.AddMethod(activeClass, "bool", "Try" + activeMethod.Name);
            method.IsStatic = activeMethod.IsStatic;
            Logger.Log("WITF:Method Created");

            // PARAMS
            foreach (Param param in activeMethod.Parameters)
            {
                method.Parameters.Add(new Param(param.ParamType, param.Name));
            }
            Param resultParam = builder.BuildParameter(activeMethod.GetTypeName(), "result", ArgumentDirection.Out);
            method.Parameters.Add(resultParam);
            Logger.Log("WITF:Params Added");

            // METHOD CALL
            var arguments = new List<string>();
            foreach (IParameterElement SourceParam in activeMethod.Parameters)
            {
                arguments.Add(SourceParam.Name);
            }
            Logger.Log("WITF:Arguments Built");

            var methodCall = builder.BuildMethodCall(activeMethod.Name, arguments.ToArray());
            Logger.Log("WITF:Methodcall Added");

            var Try = builder.AddTry(method);
            Try.AddNode(builder.BuildAssignment("result", methodCall));
            Try.AddNode(builder.BuildReturn("true"));
            Logger.Log("WITF:Try Added");

            var ExCatch = builder.AddCatch(method);
            ExCatch.AddNode(builder.BuildAssignment("result", CodeRush.Language.GetNullReferenceExpression()));
            ExCatch.AddNode(builder.BuildReturn("false"));
            Logger.Log("WITF:Catch Added");

            // RENDER METHOD
            activeClass.AddNode(method);
            var Code = CodeRush.CodeMod.GenerateCode(method, false);
            Logger.Log("WITF:Code Generated");

            int LastLine = activeMethod.Range.End.Line;
            Logger.Log(String.Format("WITF:Last Line calculated = {0}", LastLine));

            SourcePoint InsertionPoint = new SourcePoint(LastLine + 1, 1);
            Logger.Log(String.Format("WITF:InsertionPoint Calculated=(Line:{0},Offset:{1})",
                                     InsertionPoint.Line, InsertionPoint.Offset));

            var newMethodRange = CodeRush.Documents.ActiveTextDocument.InsertText(InsertionPoint, Code);
            Logger.Log("WITF:Code Inserted");

            CodeRush.Documents.Format(newMethodRange);
            Logger.Log("WITF:Code Formatted");
        }