// Used to remove the ctor from the statement colletion in order for the ctor statement to be moved. // private CodeStatement ExtractCtorStatement(IDesignerSerializationManager manager, CodeStatementCollection statements, object component) { CodeStatement result = null; CodeAssignStatement assignment = null; CodeObjectCreateExpression ctor = null; int toRemove = -1; for (int i = 0; i < statements.Count; i++) { assignment = statements[i] as CodeAssignStatement; if (assignment != null) { ctor = assignment.Right as CodeObjectCreateExpression; if (ctor != null && manager.GetType(ctor.CreateType.BaseType) == component.GetType()) { result = assignment; toRemove = i; } } } if (toRemove != -1) { statements.RemoveAt(toRemove); } return(result); }
public override object Serialize(IDesignerSerializationManager manager, object value) { CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager. GetSerializer(typeof(MyComponent).BaseType, typeof(CodeDomSerializer)); object codeObject = baseClassSerializer.Serialize(manager, value); if (codeObject is CodeStatementCollection) { CodeStatementCollection statements = (CodeStatementCollection)codeObject; // Initial MyList // Generate "myComponent1.MyList = new System.Collections.Generic.List<string>();" CodeObjectCreateExpression objectCreate1; objectCreate1 = new CodeObjectCreateExpression("System.Collections.Generic.List<string>", new CodeExpression[] { }); CodeAssignStatement as2 = new CodeAssignStatement(new CodeVariableReferenceExpression(manager.GetName(value) + ".MyList"), objectCreate1); statements.Insert(0, as2); // Add my generated code comment string commentText = "MyList generation code"; CodeCommentStatement comment = new CodeCommentStatement(commentText); statements.Insert(1, comment); // Add items to MyList // Generate the following code // this.myComponent1.MyList.Add("string5"); // this.myComponent1.MyList.Add("string4"); // this.myComponent1.MyList.Add("string3"); // this.myComponent1.MyList.Add("string2"); // this.myComponent1.MyList.Add("string1"); MyComponent myCom = (MyComponent)manager.GetInstance(manager.GetName(value)); for (int i = 0; i < myCom.MyList.Count; i++) { CodeMethodInvokeExpression methodInvoke = new CodeMethodInvokeExpression( // targetObject that contains the method to invoke. new CodeThisReferenceExpression(), // methodName indicates the method to invoke. manager.GetName(value) + ".MyList.Add", // parameters array contains the parameters for the method. new CodeExpression[] { new CodePrimitiveExpression(myCom.MyList[i]) }); CodeExpressionStatement expressionStatement; expressionStatement = new CodeExpressionStatement(methodInvoke); statements.Insert(2, expressionStatement); } // Remove system generated code statements.RemoveAt(statements.Count - 1); } return(codeObject); }
public override object Serialize(IDesignerSerializationManager manager, object value) { /* Associate the component with the serializer in the same manner as with * Deserialize */ CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager. GetSerializer(typeof(RoundGauge).BaseType, typeof(CodeDomSerializer)); object codeObject = baseClassSerializer.Serialize(manager, value); CodeStatementCollection newCode = new CodeStatementCollection(); if (codeObject is CodeStatementCollection) { CodeMethodInvokeExpression invokeExpr; CodeStatementCollection statements = (CodeStatementCollection)codeObject; // The code statement collection is valid, so add our Begin/EndUpdate calls. CodeThisReferenceExpression thisRef = new CodeThisReferenceExpression(); CodeFieldReferenceExpression gaugeRef = new CodeFieldReferenceExpression(thisRef, manager.GetName(value)); // move the comments and the "new" call for (int i = 0; i < 4; i++) { newCode.Add(statements[0]); statements.RemoveAt(0); } // add BeginInvoke invokeExpr = new CodeMethodInvokeExpression(gaugeRef, "BeginUpdate"); newCode.Add(invokeExpr); // add the designer-generated code newCode.AddRange(statements); // add EndUpdate invokeExpr = new CodeMethodInvokeExpression(gaugeRef, "EndUpdate"); newCode.Add(invokeExpr); } return(newCode); }
public override object Serialize(IDesignerSerializationManager manager, object value) { var baseSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(SharedImageCollection), typeof(CodeDomSerializer)); object codeObject = baseSerializer.Serialize(manager, value); // remove all generated code except for the member initialization CodeStatementCollection coll = codeObject as CodeStatementCollection; if (coll != null) { for (int i = coll.Count - 1; i >= 0; i--) { CodeStatement ex = coll[i]; var ass = ex as CodeAssignStatement; if (ass == null || !(ass.Left is CodeFieldReferenceExpression)) { coll.RemoveAt(i); } } } return(codeObject); }
private CodeStatement ExtractCtorStatement(CodeStatementCollection statements) { CodeStatement result = null; CodeAssignStatement assignment = null; int toRemove = -1; for (int i = 0; i < statements.Count; i++) { assignment = statements[i] as CodeAssignStatement; if (assignment != null && assignment.Right is CodeObjectCreateExpression) { result = assignment; toRemove = i; } } if (toRemove != -1) { statements.RemoveAt(toRemove); } return(result); }
// 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> }