public CodeExpression ListInitializer(IEnumerable<CodeExpression> exprs) { var list = new CodeObjectCreateExpression { Type = new CodeTypeReference("List", new CodeTypeReference("object")) }; EnsureImport("System.Collections.Generic"); list.Initializers.AddRange(exprs); return list; }
protected abstract void GenerateObjectCreateExpression(CodeObjectCreateExpression e);
private void DefineWrapperClassFieldsAndMethods(Smoke.Class* smokeClass, CodeTypeDeclaration type) { // define the dummy constructor if (smokeClass->size > 0) { CodeConstructor dummyCtor = new CodeConstructor(); dummyCtor.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(typeof(Type)), "dummy")); if (data.Smoke->inheritanceList[smokeClass->parents] > 0) { dummyCtor.BaseConstructorArgs.Add(new CodeSnippetExpression("(System.Type) null")); } dummyCtor.Attributes = MemberAttributes.Family; if (SupportingMethodsHooks != null) { SupportingMethodsHooks(data.Smoke, (Smoke.Method*) 0, dummyCtor, type); } type.Members.Add(dummyCtor); } CodeMemberField staticInterceptor = new CodeMemberField("SmokeInvocation", "staticInterceptor"); staticInterceptor.Attributes = MemberAttributes.Static; CodeObjectCreateExpression initExpression = new CodeObjectCreateExpression("SmokeInvocation"); initExpression.Parameters.Add(new CodeTypeOfExpression(type.Name)); initExpression.Parameters.Add(new CodePrimitiveExpression(null)); staticInterceptor.InitExpression = initExpression; type.Members.Add(staticInterceptor); if (smokeClass->size == 0) return; // we only need this for real classes CodeMemberMethod createProxy = new CodeMemberMethod(); createProxy.Name = "CreateProxy"; createProxy.Attributes = MemberAttributes.Public; if (data.Smoke->inheritanceList[smokeClass->parents] != 0) { createProxy.Attributes |= MemberAttributes.Override; } createProxy.Statements.Add(new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "interceptor"), // left hand side new CodeObjectCreateExpression("SmokeInvocation", new CodeTypeOfExpression(type.Name), new CodeThisReferenceExpression()) // right hand side )); type.Members.Add(createProxy); if (data.Smoke->inheritanceList[smokeClass->parents] != 0) return; // The following fields are only necessary for classes without superclasses. CodeMemberField interceptor = new CodeMemberField("SmokeInvocation", "interceptor"); interceptor.Attributes = MemberAttributes.Family; type.Members.Add(interceptor); CodeMemberField smokeObject = new CodeMemberField(typeof(IntPtr), "smokeObject"); type.Members.Add(smokeObject); type.BaseTypes.Add(new CodeTypeReference("ISmokeObject")); CodeMemberProperty propertySmokeObject = new CodeMemberProperty(); propertySmokeObject.Name = "SmokeObject"; propertySmokeObject.Type = new CodeTypeReference(typeof(IntPtr)); propertySmokeObject.Attributes = MemberAttributes.Public; CodeFieldReferenceExpression smokeObjectReference = new CodeFieldReferenceExpression( new CodeThisReferenceExpression(), smokeObject.Name); propertySmokeObject.GetStatements.Add(new CodeMethodReturnStatement(smokeObjectReference)); propertySmokeObject.SetStatements.Add(new CodeAssignStatement(smokeObjectReference, new CodePropertySetValueReferenceExpression())); type.Members.Add(propertySmokeObject); }
protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression e) { Output.Write("new "); OutputType(e.CreateType); Output.Write("("); OutputExpressionList(e.Parameters); Output.Write(")"); }
static void BuildRegion (Region r, CodeMemberMethod method) { CodeObjectCreateExpression region = new CodeObjectCreateExpression (regionType, new CodeExpression[] { new CodePrimitiveExpression (r.Name), new CodePrimitiveExpression (r.Countries.Count) }); string methodName = String.Format ("Region_{0}", regionCounter++); CodeMemberMethod regionMethod = new CodeMemberMethod (); regionMethod.Name = methodName; MainClass.Members.Add (regionMethod); CodeMethodInvokeExpression methodCall = new CodeMethodInvokeExpression ( thisref, methodName ); method.Statements.Add (methodCall); CodeAssignStatement regionAssign = new CodeAssignStatement (); regionAssign.Left = curRegion; regionAssign.Right = region; regionMethod.Statements.Add (regionAssign); methodCall = new CodeMethodInvokeExpression ( new CodeFieldReferenceExpression (thisref, "regions"), "Add", new CodeExpression[] {curRegion} ); regionMethod.Statements.Add (methodCall); methodCall = new CodeMethodInvokeExpression ( curRegion, "Add", new CodeExpression[] {curCountry} ); foreach (Country c in r.Countries) { BuildCountry (c, regionMethod); regionMethod.Statements.Add (methodCall); } }
static void GenerateBuildData (CodeMemberMethod method) { CodeTypeReference regionListType = new CodeTypeReference ("List", new CodeTypeReference[] { new CodeTypeReference ("Region") }); CodeObjectCreateExpression newObject = new CodeObjectCreateExpression (regionListType, new CodeExpression[] {new CodePrimitiveExpression (regions.Count)}); CodeAssignStatement regionsAssign = new CodeAssignStatement (); regionsAssign.Left = new CodeFieldReferenceExpression (thisref, "regions"); regionsAssign.Right = newObject; method.Statements.Add (regionsAssign); CodeMemberField field = new CodeMemberField ("Region", "curRegion"); MainClass.Members.Add (field); field = new CodeMemberField ("Country", "curCountry"); MainClass.Members.Add (field); field = new CodeMemberField ("Element", "curCountryLocation"); MainClass.Members.Add (field); field = new CodeMemberField ("City", "curCity"); MainClass.Members.Add (field); field = new CodeMemberField ("State", "curState"); MainClass.Members.Add (field); field = new CodeMemberField ("Element", "curStateLocation"); MainClass.Members.Add (field); foreach (Region r in regions) BuildRegion (r, method); }
static CodeObjectCreateExpression BuildCountryLocation (Element e, CodeMemberMethod method) { CodeObjectCreateExpression location; if (e is Location) location = new CodeObjectCreateExpression (locationType, new CodeExpression[] { new CodePrimitiveExpression (e.Name), new CodePrimitiveExpression (((Location)e).Code), new CodePrimitiveExpression (((Location)e).Coordinates) }); else if (e is City) location = new CodeObjectCreateExpression (cityType, new CodeExpression[] { new CodePrimitiveExpression (e.Name), new CodePrimitiveExpression (((City)e).Locations.Count) }); else if (e is State) location = new CodeObjectCreateExpression (stateType, new CodeExpression[] { new CodePrimitiveExpression (e.Name), new CodePrimitiveExpression (((State)e).Locations.Count) }); else throw new ApplicationException (String.Format ("Unexpected <country> child type: {0}", e)); if (e is Location) return location; CodeAssignStatement locationAssign = new CodeAssignStatement (); locationAssign.Left = curCountryLocation; locationAssign.Right = location; method.Statements.Add (locationAssign); if (e is City) BuildCity (e as City, method, curCountry, curCountryLocation); else if (e is State) BuildState (e as State, method); return null; }
static void BuildCountry (Country c, CodeMemberMethod method) { CodeObjectCreateExpression country = new CodeObjectCreateExpression (countryType, new CodeExpression[] { new CodePrimitiveExpression (c.Name), new CodePrimitiveExpression (c.Locations.Count) }); string methodName = String.Format ("Country_{0}", countryCounter++); CodeMemberMethod countryMethod = new CodeMemberMethod (); countryMethod.Name = methodName; MainClass.Members.Add (countryMethod); CodeAssignStatement countryAssign = new CodeAssignStatement (); countryAssign.Left = curCountry; countryAssign.Right = country; countryMethod.Statements.Add (countryAssign); CodeMethodInvokeExpression methodCall = new CodeMethodInvokeExpression ( thisref, methodName ); method.Statements.Add (methodCall); methodCall = new CodeMethodInvokeExpression ( curCountry, "Add", new CodeExpression[] {curCountryLocation} ); CodeMethodInvokeExpression locationAddDirect; CodeObjectCreateExpression expr; foreach (Element e in c.Locations) { expr = BuildCountryLocation (e, countryMethod); if (expr == null) countryMethod.Statements.Add (methodCall); else { locationAddDirect = new CodeMethodInvokeExpression ( curCountry, "Add", new CodeExpression[] {expr} ); countryMethod.Statements.Add (locationAddDirect); } } }
private static CodeMemberMethod CreateOperationCompletedMethod(ServiceContractGenerationContext context, CodeTypeDeclaration clientType, string syncMethodName, CodeTypeDeclaration operationCompletedEventArgsType, CodeMemberEvent operationCompletedEvent) { CodeObjectCreateExpression expression; CodeMemberMethod method = new CodeMemberMethod(); method.Attributes = MemberAttributes.Private; method.Name = NamingHelper.GetUniqueName(GetOperationCompletedMethodName(syncMethodName), new NamingHelper.DoesNameExist(ClientClassGenerator.DoesMethodNameExist), context.Operations); method.Parameters.Add(new CodeParameterDeclarationExpression(new CodeTypeReference(objectType), "state")); method.ReturnType = new CodeTypeReference(voidType); CodeVariableDeclarationStatement statement = new CodeVariableDeclarationStatement(invokeAsyncCompletedEventArgsTypeName, "e"); statement.InitExpression = new CodeCastExpression(invokeAsyncCompletedEventArgsTypeName, new CodeArgumentReferenceExpression(method.Parameters[0].Name)); CodeVariableReferenceExpression targetObject = new CodeVariableReferenceExpression(statement.Name); if (operationCompletedEventArgsType != null) { expression = new CodeObjectCreateExpression(operationCompletedEventArgsType.Name, new CodeExpression[] { new CodePropertyReferenceExpression(targetObject, EventArgsPropertyNames[0]), new CodePropertyReferenceExpression(targetObject, EventArgsPropertyNames[1]), new CodePropertyReferenceExpression(targetObject, EventArgsPropertyNames[2]), new CodePropertyReferenceExpression(targetObject, EventArgsPropertyNames[3]) }); } else { expression = new CodeObjectCreateExpression(asyncCompletedEventArgsType, new CodeExpression[] { new CodePropertyReferenceExpression(targetObject, EventArgsPropertyNames[1]), new CodePropertyReferenceExpression(targetObject, EventArgsPropertyNames[2]), new CodePropertyReferenceExpression(targetObject, EventArgsPropertyNames[3]) }); } CodeEventReferenceExpression expression3 = new CodeEventReferenceExpression(new CodeThisReferenceExpression(), operationCompletedEvent.Name); CodeDelegateInvokeExpression expression4 = new CodeDelegateInvokeExpression(expression3, new CodeExpression[] { new CodeThisReferenceExpression(), expression }); CodeConditionStatement statement2 = new CodeConditionStatement(new CodeBinaryOperatorExpression(expression3, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression(null)), new CodeStatement[] { statement, new CodeExpressionStatement(expression4) }); method.Statements.Add(statement2); clientType.Members.Add(method); return method; }
public void Visit(CodeObjectCreateExpression o) { g.GenerateObjectCreateExpression(o); }
protected override void GenerateObjectCreateExpression(CodeObjectCreateExpression expression) { base.Output.Write("new "); this.OutputType(expression.CreateType); base.Output.Write('('); this.OutputExpressionList(expression.Parameters); base.Output.Write(')'); }
public void VisitObjectCreation(CodeObjectCreateExpression c) { writer.Write("new"); writer.Write(" "); VisitTypeReference(c.Type); if (c.Arguments.Count > 0 || c.Initializers.Count == 0 && c.Initializer == null) { writer.Write("("); var sep = ""; foreach (var e in c.Arguments) { writer.Write(sep); sep = ", "; e.Accept(this); } writer.Write(")"); } if (c.Initializers.Count > 0) { writer.Write(" {"); writer.WriteLine(); ++writer.IndentLevel; string sep = null; foreach (var e in c.Initializers) { if (sep != null) { writer.Write(sep); writer.WriteLine(); } sep = ","; e.Accept(this); } --writer.IndentLevel; writer.WriteLine(); writer.Write("}"); } if (c.Initializer != null) { writer.Write(" "); c.Initializer.Accept(this); } }
private static void ImplementINotifyPropertyChanged(CodeTypeDeclaration decl) { decl.BaseTypes.Add(typeof(INotifyPropertyChanged)); decl.Members.Add(new CodeMemberEvent() { Name = PropertyChangedEventName , Attributes = MemberAttributes.Public , Type = new CodeTypeReference(typeof(PropertyChangedEventHandler)) }); var notify = new CodeMemberMethod() { Name = PropertyChangedFunctionName , Attributes = MemberAttributes.Family }; decl.Members.Add(notify); notify.Parameters.Add(new CodeParameterDeclarationExpression() { Name = PropertyNameParameterName, Type = new CodeTypeReference(typeof(string)) }); notify.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference(typeof(PropertyChangedEventHandler)), EventHandlerName)); notify.Statements.Add(new CodeAssignStatement() { Left = new CodeVariableReferenceExpression(EventHandlerName), Right = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), PropertyChangedEventName) }); var condition = new CodeConditionStatement() { Condition = new CodeBinaryOperatorExpression() { Left = new CodePrimitiveExpression(null) , Right = new CodeVariableReferenceExpression(EventHandlerName) , Operator = CodeBinaryOperatorType.IdentityInequality } }; var eventArgs = new CodeObjectCreateExpression() { CreateType = new CodeTypeReference(typeof(PropertyChangedEventArgs)) }; eventArgs.Parameters.Add(new CodeVariableReferenceExpression(PropertyNameParameterName)); var invoke = new CodeMethodInvokeExpression(null,EventHandlerName); invoke.Parameters.Add(new CodeThisReferenceExpression()); invoke.Parameters.Add(eventArgs); condition.TrueStatements.Add( invoke ); notify.Statements.Add(condition); }
private void NewObject(Instruction il) { var ctor = (MethodReference)il.Operand; this.ExternalMethods.Add(ctor); CodeObjectCreateExpression expr = new CodeObjectCreateExpression { Constructor = ctor }; PopParametersInto(ctor.Parameters, expr.Parameters); Push(expr); }
private void ValidateObjectCreateExpression(CodeObjectCreateExpression e) { ValidateTypeReference(e.CreateType); ValidateExpressionList(e.Parameters); }