//----------------------------------------------------------------------------------------------------------------------------------------------------- private void ImplementActivationInterface( TypeMember productType, TypeMember activationContract, MethodSignature constructorSignature, TypeMember artifactType) { var parameterCount = constructorSignature.Parameters.Count; var iConstructorGenericParameters = constructorSignature.Parameters.Select(p => p.Type) .Append(activationContract); TypeMember iConstructorInterfaceOpen = _s_iConstructorByParameterCount[parameterCount]; TypeMember iConstructorInterfaceClosed = iConstructorInterfaceOpen.MakeGenericType(iConstructorGenericParameters.ToArray()); artifactType.Interfaces.Add(iConstructorInterfaceClosed); var factoryMethod = new MethodMember( MemberVisibility.Public, MemberModifier.None, nameof(IConstructor <object> .NewInstance), new MethodSignature { ReturnValue = new MethodParameter { Type = activationContract } } ); factoryMethod.Signature.Parameters.AddRange(constructorSignature.Parameters); var newObjectExpression = new NewObjectExpression { Type = productType, ConstructorCall = new MethodCallExpression() }; newObjectExpression.ConstructorCall.Arguments.AddRange( constructorSignature.Parameters.Select(p => new Argument { Expression = new ParameterExpression { Parameter = p } })); factoryMethod.Body = new BlockStatement( new ReturnStatement { Expression = newObjectExpression } ); ImplementSingletonMethod(productType, activationContract, constructorSignature, artifactType); artifactType.Members.Add(factoryMethod); }
private void newObjectToString(NewObjectExpression newObject, StringBuilder sb) { sb.append("new ").append(newObject.Constructor.getDeclaringClass()).append("("); var first = true; foreach (var a in newObject.Arguments) { if (first) { first = false; } else { sb.append(", "); } expressionToString(a, sb, ""); } sb.append(")"); if (newObject.Initializers.any()) { sb.append(" { "); first = true; foreach (var i in newObject.Initializers) { if (first) { first = false; } else { sb.append(", "); } sb.append(i.Member.getDeclaringClass()).append(".").append(i.Member.getName()).append("("); var firstArg = true; foreach (var a in i.Arguments) { if (firstArg) { firstArg = false; } else { sb.append(", "); } expressionToString(a, sb, ""); } sb.append(")"); } sb.append(" }"); } }
//----------------------------------------------------------------------------------------------------------------------------------------------------- private static ExpressionSyntax EmitNewObjectSyntax(NewObjectExpression newObject) { var syntax = ObjectCreationExpression(newObject.Type.GetTypeNameSyntax()); if (newObject.ConstructorCall != null) { syntax = syntax.WithArgumentList(newObject.ConstructorCall.GetArgumentListSyntax()); } else { syntax = syntax.WithArgumentList(ArgumentList()); } return(syntax); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public virtual void VisitNewObjectExpression(NewObjectExpression expression) { VisitAbstractExpression(expression); }
public virtual NewObjectExpression RewriteNewObjectExpression(NewObjectExpression expression) { return(expression); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- private void ImplementSingletonMethod( TypeMember productType, TypeMember activationContract, MethodSignature constructorSignature, TypeMember artifactType) { var syncRootField = new FieldMember(artifactType, MemberVisibility.Private, MemberModifier.None, typeof(object), "_singletonInstanceSyncRoot") { IsReadOnly = true, Initializer = new NewObjectExpression { Type = typeof(object) } }; var singletonField = new FieldMember(artifactType, MemberVisibility.Private, MemberModifier.None, activationContract, "_singletonInstance"); var singletonMethod = new MethodMember( MemberVisibility.Public, MemberModifier.None, nameof(IConstructor <object> .GetOrCreateSingleton), new MethodSignature { ReturnValue = new MethodParameter { Type = activationContract } }); NewObjectExpression newObjectExpression; singletonMethod.Signature.Parameters.AddRange(constructorSignature.Parameters); singletonMethod.Body = new BlockStatement( new IfStatement { Condition = new BinaryExpression { Left = new MemberExpression { Target = new ThisExpression(), Member = singletonField }, Operator = BinaryOperator.Equal, Right = new ConstantExpression { Value = null } }, ThenBlock = new BlockStatement( new LockStatement { SyncRoot = new MemberExpression { Target = new ThisExpression(), Member = syncRootField }, Body = new BlockStatement( new IfStatement { Condition = new BinaryExpression { Left = new MemberExpression { Target = new ThisExpression(), Member = singletonField }, Operator = BinaryOperator.Equal, Right = new ConstantExpression { Value = null } }, ThenBlock = new BlockStatement( new ExpressionStatement { Expression = new AssignmentExpression { Left = new MemberExpression { Target = new ThisExpression(), Member = singletonField }, Right = newObjectExpression = new NewObjectExpression { Type = productType, ConstructorCall = new MethodCallExpression() } } } ) } ) } ) }, new ReturnStatement { Expression = new MemberExpression { Target = new ThisExpression(), Member = singletonField } } ); newObjectExpression.ConstructorCall.Arguments.AddRange( constructorSignature.Parameters.Select(p => new Argument { Expression = new ParameterExpression { Parameter = p } })); artifactType.Members.Add(singletonField); artifactType.Members.Add(syncRootField); artifactType.Members.Add(singletonMethod); }