Пример #1
0
        public static Int32 Add(
            this CodeAttributeDeclarationCollection attributeCollection,
            Type attributeType,
            params Tuple <String, Object>[] parameters
            )
        {
            if (attributeType == null)
            {
                throw Logger.Fatal.ArgumentNull(nameof(attributeType));
            }

            return(attributeCollection.Add(
                       new CodeAttributeDeclaration(
                           new CodeTypeReference(attributeType),
                           parameters.Select(
                               tuple => new CodeAttributeArgument(tuple.Item1, CommandCodeGenerator.CreateLiteralExpression(tuple.Item2))
                               ).ToArray()
                           )
                       ));
        }
Пример #2
0
        public static void GenerateBackingField(
            this CodeMemberProperty property,
            CodeTypeDeclaration containingType,
            Object defaultValue
            )
        {
            if (containingType == null)
            {
                throw Logger.Fatal.ArgumentNull(nameof(containingType));
            }

            if (String.IsNullOrEmpty(property.Name))
            {
                throw Logger.Fatal.Argument(
                          nameof(property),
                          SR.CodeDomExtensions_PropertyNameNotSet
                          );
            }

            if (property.GetStatements.Any())
            {
                throw Logger.Fatal.Argument(
                          nameof(property),
                          SR.CodeDomExtensions_PropertyGetterNotEmpty
                          );
            }

            if (property.SetStatements.Any())
            {
                throw Logger.Fatal.Argument(
                          nameof(property),
                          SR.CodeDomExtensions_PropertySetterNotEmpty
                          );
            }

            var fieldName = "_" + ToCamelCase(property.Name);

            if (containingType.Members
                .Cast <CodeTypeMember>()
                .Any(member => member.Name == fieldName)
                )
            {
                throw Logger.Fatal.InvalidOperation(SR.CodeDomExtensions_FieldExists);
            }

            var isStatic = (property.Attributes & MemberAttributes.Static) == MemberAttributes.Static;

            var codeField = new CodeMemberField()
            {
                Name       = fieldName,
                Type       = property.Type,
                Attributes = MemberAttributes.Private | (isStatic ? MemberAttributes.Static : 0),
            };

            if (defaultValue != null)
            {
                codeField.InitExpression = CommandCodeGenerator.CreateLiteralExpression(defaultValue);
            }

            CreatePropertyGetterAndSetter(property, codeField.Name, CreateThisExpression(containingType, isStatic));

            containingType.Members.Add(codeField);
        }