/// <summary>
        /// Fills GetBehaviors method body by creating collection of registered <see cref="IBehavior<>"/>.
        /// </summary>
        /// <param name="method">GetBehaviors empty method.</param>
        private void GenerateBehaviorMethodBody(CodeMemberMethod method)
        {
            Type resultListType = typeof(List <>).MakeGenericType(typeof(IBehavior <>).MakeGenericType(handlerType));

            method.Statements.Add(new CodeVariableDeclarationStatement(
                                      resultListType,
                                      resultListName,
                                      new CodeObjectCreateExpression(resultListType)
                                      ));

            IEnumerable <Type>        behaviorTypes     = behaviorProvider.GetBehaviors(handlerType);
            ICodeDomContext           context           = new DefaultCodeDomContext(configuration, handlerType);
            ICodeDomBehaviorGenerator behaviorGenerator = GetBehaviorInstanceGenerator();

            foreach (Type behaviorType in behaviorTypes)
            {
                method.Statements.Add(new CodeMethodInvokeExpression(
                                          new CodeVariableReferenceExpression(resultListName),
                                          TypeHelper.MethodName <IList <object>, object>(l => l.Add),
                                          behaviorGenerator.TryGenerate(context, behaviorType)
                                          ));
            }

            method.Statements.Add(new CodeMethodReturnStatement(
                                      new CodeVariableReferenceExpression(resultListName)
                                      ));
        }
예제 #2
0
        /// <summary>
        /// Maps <paramref name="behaviorType"/> to be processed by <paramref name="generator" />
        /// </summary>
        public CodeDomBehaviorGeneratorCollection Add(Type behaviorType, ICodeDomBehaviorGenerator generator)
        {
            Ensure.NotNull(behaviorType, "behaviorType");
            Ensure.NotNull(generator, "generator");

            lock (storageLock)
                storage[behaviorType] = generator;

            return(this);
        }
        /// <summary>
        /// Returns behavior instance generator.
        /// </summary>
        /// <returns>Behavior instance generator.</returns>
        private ICodeDomBehaviorGenerator GetBehaviorInstanceGenerator()
        {
            ICodeDomBehaviorGenerator behaviorGenerator = configuration.GetBehaviorGenerator(null);

            if (behaviorGenerator == null)
            {
                behaviorGenerator = new DefaultCodeDomBehaviorGenerator();
            }
            else
            {
                behaviorGenerator = new DefaultCodeDomBehaviorGenerator(behaviorGenerator);
            }

            return(behaviorGenerator);
        }
예제 #4
0
 private static bool TryGetDefaultGenerator(Type behaviorType, out ICodeDomBehaviorGenerator generator)
 {
     generator = new DefaultCodeDomBehaviorGenerator();
     return(true);
 }
 /// <summary>
 /// Creates new instance that first checks <paramref name="generator"/> and if it returns <c>null</c>,
 /// uses default (parameter-less) constructor.
 /// </summary>
 /// <param name="generator">Inner generator used first.</param>
 public DefaultCodeDomBehaviorGenerator(ICodeDomBehaviorGenerator generator)
 {
     Ensure.NotNull(generator, "generator");
     this.generator = generator;
 }
예제 #6
0
 /// <summary>
 /// Sets behavior instance generator.
 /// </summary>
 /// <param name="configuration">Compiler configuration.</param>
 /// <param name="generator">Behavior instance generator.</param>
 /// <returns>Self (for fluency).</returns>
 public static ICompilerConfiguration AddBehaviorGenerator(this ICompilerConfiguration configuration, ICodeDomBehaviorGenerator generator)
 {
     Ensure.NotNull(configuration, "configuration");
     configuration.Add("BehaviorGenerator", generator);
     return(configuration);
 }
예제 #7
0
 /// <summary>
 /// Returns behavior instance generator.
 /// </summary>
 /// <param name="configuration">Compiler configuration.</param>
 /// <param name="defaultValue">Fallback value, when <paramref name="configuration"/> doesn't contain generator.</param>
 /// <returns>Behavior instance generator.</returns>
 public static ICodeDomBehaviorGenerator GetBehaviorGenerator(this ICompilerConfiguration configuration, ICodeDomBehaviorGenerator defaultValue)
 {
     Ensure.NotNull(configuration, "configuration");
     return(configuration.Get <ICodeDomBehaviorGenerator>("BehaviorGenerator", defaultValue));
 }