public void Constructor1() { CodeStatement cs1 = new CodeStatement(); CodeStatement cs2 = new CodeStatement(); CodeStatement[] statements = new CodeStatement[] { cs1, cs2 }; CodeStatementCollection coll = new CodeStatementCollection( statements); Assert.AreEqual(2, coll.Count, "#1"); Assert.AreEqual(0, coll.IndexOf(cs1), "#2"); Assert.AreEqual(1, coll.IndexOf(cs2), "#3"); }
public void Insert() { CodeStatement cs1 = new CodeStatement(); CodeStatement cs2 = new CodeStatement(); CodeStatementCollection coll = new CodeStatementCollection(); coll.Add(cs1); Assert.AreEqual(1, coll.Count, "#1"); Assert.AreEqual(0, coll.IndexOf(cs1), "#2"); coll.Insert(0, cs2); Assert.AreEqual(2, coll.Count, "#3"); Assert.AreEqual(1, coll.IndexOf(cs1), "#4"); Assert.AreEqual(0, coll.IndexOf(cs2), "#5"); }
public void Constructor2() { CodeStatement cs1 = new CodeStatement(); CodeStatement cs2 = new CodeStatement(); CodeStatementCollection c = new CodeStatementCollection(); c.Add(cs1); c.Add(cs2); CodeStatementCollection coll = new CodeStatementCollection(c); Assert.AreEqual(2, coll.Count, "#1"); Assert.AreEqual(0, coll.IndexOf(cs1), "#2"); Assert.AreEqual(1, coll.IndexOf(cs2), "#3"); }
private static void ReplaceControlCreateStatement(Type ctrlType, CodeAssignStatement objAssignStatement, CodeStatementCollection statements) { /* Generate code like below * * IServiceProvider __activator = HttpRuntime.WebObjectActivator; * * if (activator != null) { * _ctrl = (ctrlType)activator.GetService(ctrlType); * } * * // if default c-tor exists * else { * _ctrl = new .... * } * // if no default c-tor * else { * throw new InvalidOperationException(SR.GetString(SR.Could_not_create_type_instance, ctrlType)) * } * */ var webObjectActivatorExpr = new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("System.Web.HttpRuntime"), "WebObjectActivator"); var activatorRefExpr = new CodeVariableReferenceExpression("__activator"); var getServiceExpr = new CodeMethodInvokeExpression(webObjectActivatorExpr, "GetService", new CodeTypeOfExpression(ctrlType)); var castExpr = new CodeCastExpression(new CodeTypeReference(ctrlType), getServiceExpr); var createObjectStatement = new CodeConditionStatement() { Condition = new CodeBinaryOperatorExpression(activatorRefExpr, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)) }; createObjectStatement.TrueStatements.Add(new CodeAssignStatement(objAssignStatement.Left, castExpr)); // If default c-tor exists if (DoesTypeHaveDefaultCtor(ctrlType)) { createObjectStatement.FalseStatements.Add(objAssignStatement); } else { var throwExceptionStatement = new CodeThrowExceptionStatement(new CodeObjectCreateExpression( new CodeTypeReference(typeof(System.InvalidOperationException)), new CodeExpression[] { new CodePrimitiveExpression(SR.GetString(SR.Could_not_create_type_instance, ctrlType)) })); createObjectStatement.FalseStatements.Add(throwExceptionStatement); } // replace the old assign statement var indexOfStatement = statements.IndexOf(objAssignStatement); statements.Insert(indexOfStatement, createObjectStatement); statements.Insert(indexOfStatement, new CodeAssignStatement(activatorRefExpr, webObjectActivatorExpr)); statements.Insert(indexOfStatement, new CodeVariableDeclarationStatement(typeof(IServiceProvider), "__activator")); statements.Remove(objAssignStatement); }
public void AddRange() { CodeStatement cs1 = new CodeStatement(); CodeStatement cs2 = new CodeStatement(); CodeStatement cs3 = new CodeStatement(); CodeStatementCollection coll1 = new CodeStatementCollection(); coll1.Add(cs1); coll1.Add(cs2); CodeStatementCollection coll2 = new CodeStatementCollection(); coll2.Add(cs3); coll2.AddRange(coll1); Assert.AreEqual(3, coll2.Count, "#1"); Assert.AreEqual(1, coll2.IndexOf(cs1), "#2"); Assert.AreEqual(2, coll2.IndexOf(cs2), "#3"); Assert.AreEqual(0, coll2.IndexOf(cs3), "#4"); CodeStatementCollection coll3 = new CodeStatementCollection(); coll3.Add(cs3); coll3.AddRange(new CodeStatement[] { cs1, cs2 }); Assert.AreEqual(3, coll2.Count, "#5"); Assert.AreEqual(1, coll2.IndexOf(cs1), "#6"); Assert.AreEqual(2, coll2.IndexOf(cs2), "#7"); Assert.AreEqual(0, coll2.IndexOf(cs3), "#8"); }
public void Constructor1_Deny_Unrestricted() { CodeStatementCollection coll = new CodeStatementCollection(array); coll.CopyTo(array, 0); Assert.AreEqual(1, coll.Add(cs), "Add"); Assert.AreSame(cs, coll[0], "this[int]"); coll.AddRange(array); coll.AddRange(coll); Assert.IsTrue(coll.Contains(cs), "Contains"); Assert.AreEqual(0, coll.IndexOf(cs), "IndexOf"); coll.Insert(0, cs); coll.Remove(cs); }
// CodeStatementCollection public void CodeStatementCollectionSample() { //<Snippet1> //<Snippet2> // Creates an empty CodeStatementCollection. CodeStatementCollection collection = new CodeStatementCollection(); //</Snippet2> //<Snippet3> // Adds a CodeStatement to the collection. collection.Add(new CodeCommentStatement("Test comment statement")); //</Snippet3> //<Snippet4> // Adds an array of CodeStatement objects to the collection. CodeStatement[] statements = { new CodeCommentStatement("Test comment statement"), new CodeCommentStatement("Test comment statement") }; collection.AddRange(statements); // Adds a collection of CodeStatement objects to the collection. CodeStatement testStatement = new CodeCommentStatement("Test comment statement"); CodeStatementCollection statementsCollection = new CodeStatementCollection(); statementsCollection.Add(new CodeCommentStatement("Test comment statement")); statementsCollection.Add(new CodeCommentStatement("Test comment statement")); statementsCollection.Add(testStatement); collection.AddRange(statementsCollection); //</Snippet4> //<Snippet5> // Tests for the presence of a CodeStatement in the // collection, and retrieves its index if it is found. int itemIndex = -1; if (collection.Contains(testStatement)) { itemIndex = collection.IndexOf(testStatement); } //</Snippet5> //<Snippet6> // Copies the contents of the collection beginning at index 0 to the specified CodeStatement array. // 'statements' is a CodeStatement array. CodeStatement[] statementArray = new CodeStatement[collection.Count]; collection.CopyTo(statementArray, 0); //</Snippet6> //<Snippet7> // Retrieves the count of the items in the collection. int collectionCount = collection.Count; //</Snippet7> //<Snippet8> // Inserts a CodeStatement at index 0 of the collection. collection.Insert(0, new CodeCommentStatement("Test comment statement")); //</Snippet8> //<Snippet9> // Removes the specified CodeStatement from the collection. collection.Remove(testStatement); //</Snippet9> //<Snippet10> // Removes the CodeStatement at index 0. collection.RemoveAt(0); //</Snippet10> //</Snippet1> }