示例#1
0
文件: Engine.cs 项目: xul8tr/wilinq
        /// <summary>
        ///     Generates the name of the property.
        /// </summary>
        /// <param name="fieldDefinition">The field definition.</param>
        /// <returns></returns>
        private string GeneratePropertyName(ModelFieldDefinition fieldDefinition)
        {
            if (!string.IsNullOrEmpty(fieldDefinition.PropertyName))
            {
                return(fieldDefinition.PropertyName);
            }

            string replacement;

            switch (FieldSeparator)
            {
            case FieldSeparator.NoSpace:
                replacement = string.Empty;
                break;

            case FieldSeparator.Underscore:
                replacement = "_";
                break;

            default:
                throw new GenerationException("InvalidSeparatorToken");
            }

            var propName = Regex.Replace(fieldDefinition.Name, @"\W+", replacement);

            if (_definedPropertyList.Contains(propName))
            {
                throw new GenerationException("PropertyNameConflict", fieldDefinition.ReferenceName);
            }

            return(propName);
        }
示例#2
0
文件: Engine.cs 项目: xul8tr/wilinq
        /// <summary>
        ///     Generates a field.
        /// </summary>
        /// <param name="wiTypeClass">The wi type class.</param>
        /// <param name="fieldDefinition">The field definition.</param>
        private void GenerateField(CodeTypeDeclaration wiTypeClass, ModelFieldDefinition fieldDefinition)
        {
            var property = new CodeMemberProperty {
                HasGet = true
            };

            var propName = GeneratePropertyName(fieldDefinition);

            property.Name = propName;

            var propertyType = fieldDefinition.Type;

            if (!propertyType.IsClass)
            {
                var nullableType = typeof(Nullable <>);
                propertyType = nullableType.MakeGenericType(propertyType);
            }
            property.Type       = new CodeTypeReference(propertyType);
            property.Attributes = MemberAttributes.Public;

            property.CustomAttributes.Add(new CodeAttributeDeclaration(
                                              new CodeTypeReference(typeof(FieldAttribute)),
                                              new CodeAttributeArgument(new CodePrimitiveExpression(fieldDefinition.ReferenceName))));


            if (!string.IsNullOrEmpty(fieldDefinition.Description))
            {
                var comment = new CodeComment($"<summary>{fieldDefinition.Description}</summary>", true);
                property.Comments.Add(new CodeCommentStatement(comment));
            }

            var fieldMethod = new CodeMethodReferenceExpression(new CodeThisReferenceExpression(),
                                                                propertyType.IsClass ? "GetRefField" : "GetStructField", new CodeTypeReference(fieldDefinition.Type));
            var fieldMethodInvoke = new CodeMethodInvokeExpression(fieldMethod,
                                                                   new CodePrimitiveExpression(fieldDefinition.ReferenceName));

            property.GetStatements.Add(new CodeMethodReturnStatement(fieldMethodInvoke));


            if (!fieldDefinition.IsReadOnly)
            {
                var setFieldMethod = new CodeMethodReferenceExpression(new CodeThisReferenceExpression(),
                                                                       propertyType.IsClass ? "SetRefField" : "SetStructField",
                                                                       new CodeTypeReference(fieldDefinition.Type));
                var setFielMethodInvoke = new CodeMethodInvokeExpression(setFieldMethod,
                                                                         new CodePrimitiveExpression(fieldDefinition.ReferenceName),
                                                                         new CodePropertySetValueReferenceExpression());
                property.SetStatements.Add(setFielMethodInvoke);
            }
            wiTypeClass.Members.Add(property);
            _definedPropertyList.Add(property.Name);
        }
 /// <summary>
 /// Initializes a new instance of the PropertyDefinitionViewModel class.
 /// </summary>
 /// <param name="field"></param>
 public PropertyDefinitionViewModel(ModelFieldDefinition field)
 {
     _field = field;
 }