Esempio n. 1
0
        public static CodeCompileUnit ConvertToOrchestrationBindingCodeCompileUnit(this Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (!typeof(BTXService).IsAssignableFrom(type))
            {
                throw new ArgumentException(string.Format("{0} is not an orchestration type.", type.FullName), "type");
            }

            var ports        = (PortInfo[])Reflector.GetField(type, "_portInfo");
            var unboundPorts = ports
                               // filter out direct ports
                               .Where(p => p.FindAttribute(typeof(DirectBindingAttribute)) == null)
                               .ToArray();

            var @namespace = new CodeNamespace(type.Namespace);

            @namespace.ImportNamespace(typeof(Action <>));
            @namespace.ImportNamespace(typeof(GeneratedCodeAttribute));
            @namespace.ImportNamespace(typeof(OrchestrationBindingBase <>));
            @namespace.ImportNamespace(typeof(PortInfo));
            @namespace.ImportNamespace(type);

            if (unboundPorts.Any())
            {
                var @interface = @namespace.AddBindingInterface(type);
                unboundPorts.Each(port => @interface.AddPortPropertyMember(port));
                var @class = @namespace.AddBindingClass(type, @interface);
                ports.Each(port => @class.AddPortOperationMember(port));
                @class.AddDefaultConstructor();
                @class.AddBindingConfigurationConstructor(@interface);
                unboundPorts.Each(port => @class.AddPortPropertyMember(port, @interface));
            }
            else
            {
                var @class = @namespace.AddBindingClass(type);
                ports.Each(port => @class.AddPortOperationMember(port));
                @class.AddDefaultConstructor();
                @class.AddBindingConfigurationConstructor();
            }

            var compileUnit = new CodeCompileUnit();

            compileUnit.Namespaces.Add(@namespace);
            return(compileUnit);
        }