Beispiel #1
0
        private CodeAttributeDeclaration ReadAnnotation(Annotation annotation, CodeNamespace nameSpace)
        {
            var declaration = WellKnownAnnotations[annotation.Name];

            if (declaration != null)
            {
                return(declaration);
            }

            var memberName = annotation.Name;

            var fqt           = new FullyQualifiedType(memberName);
            var attributeName = fqt.TypeName + "Attribute";

            var attributeNamespace =
                compileUnit.Namespaces()
                .FirstOrDefault(n => n.Name == fqt.Namespace.Name);

            if (attributeNamespace == null)
            {
                attributeNamespace = fqt.Namespace ?? nameSpace;
                AddNamespace(attributeNamespace);
            }
            ImportNamespace(attributeNamespace.Name, nameSpace);

            // Declare the Attribute only if it doesn't exist already.
            var exists =
                attributeNamespace.Types()
                .Any(a => a.Name == attributeName);

            if (!exists)
            {
                var typeDeclaration = CodeTypeContainer.CreateAttribute(attributeName);

                var valueProperty  = Declare.Property("Value", CodeTypeContainer.Create(typeof(string)).Reference);
                var valueParameter = valueProperty.AsParameter();

                var constructor = new CodeConstructor();
                constructor.Attributes = MemberAttributes.Public;
                constructor.Parameters.Add(valueParameter);
                constructor.Statements.Add(new CodeAssignStatement(valueProperty.Reference(), valueParameter.Reference()));

                typeDeclaration.Members.Add(constructor);
                typeDeclaration.Members.Add(valueProperty);

                attributeNamespace.Types.Add(typeDeclaration);
                ImportNamespaces(typeDeclaration.RequiredImports);
            }

            return(Declare.Attribute(fqt.TypeName, annotation.Value));
        }
Beispiel #2
0
        private void ReadInterface(Interface @interface)
        {
            var interfaceName = @interface.Name;
            var fqt           = new FullyQualifiedType(interfaceName);
            var declaration   = CodeTypeContainer.CreateInterface(fqt.TypeName);

            ImportNamespaces(declaration.RequiredImports);

            var @namespace = fqt.Namespace;

            @namespace.Types.Add(declaration);
            AddNamespace(@namespace);

            foreach (var item in @interface.Children)
            {
                switch (item.DBusItemType)
                {
                case DBusItemType.Signal:
                    var eventHandler = ReadSignal((Signal)item, @namespace);
                    declaration.AddEvent(item.Name, eventHandler.Reference);
                    break;

                case DBusItemType.Method:
                    var method = ReadMethod((Method)item, @namespace);
                    declaration.Members.Add(method);
                    break;

                case DBusItemType.Property:
                    var property = ReadProperty((Property)item, @namespace);
                    declaration.Members.Add(property);
                    break;

                case DBusItemType.Annotation:
                    var attribute = ReadAnnotation((Annotation)item, @namespace);
                    declaration.CustomAttributes.Add(attribute);
                    break;

                default:
                    throw CreateInvalidDataException(@interface.DBusItemType, DBusItemType.Signal, DBusItemType.Method, DBusItemType.Property, DBusItemType.Annotation);
                }
            }
        }