Esempio n. 1
0
        private BindingContextCollection GetBindingContexts(GeneratorExecutionContext context, IEnumerable <ClassDeclarationSyntax> @classes)
        {
            var contexts = new BindingContextCollection();

            foreach (var bindingContext in @classes)
            {
                var model  = context.Compilation.GetSemanticModel(bindingContext.SyntaxTree);
                var symbol = model.GetDeclaredSymbol(bindingContext, context.CancellationToken) as INamedTypeSymbol;

                // Add binding contexts
                foreach (var attribute in symbol.GetAttributes().Where(attribute => attribute.IsAttribute(Constants.BindingContextFor)))
                {
                    var target = attribute.MakeGenericFromAttribute();
                    contexts.Add(target, symbol);
                }
            }
            return(contexts);
        }
Esempio n. 2
0
        private void CreateBehaviorFactories(GeneratorExecutionContext context,
                                             BindingContextCollection bindingContexts,
                                             Dictionary <INamedTypeSymbol, List <BehaviorData> > behaviorMap,
                                             IEnumerable <ClassDeclarationSyntax> @classes)
        {
            // Let's start by generating code for incomplete entities
            foreach (var entity in @classes)
            {
                var model  = context.Compilation.GetSemanticModel(entity.SyntaxTree);
                var symbol = model.GetDeclaredSymbol(entity, context.CancellationToken) as INamedTypeSymbol;

                // Get the set of behaviors for this entity
                var bindingContext = bindingContexts.GetBindingContext(symbol);
                var factory        = new BehaviorFactoryResolver(symbol, behaviorMap[symbol], bindingContext);
                var code           = factory.Create();
                context.AddSource(symbol.ToString() + ".Behaviors.cs", code);
            }
        }