Пример #1
0
        private static void GenerateFilterPropertyMembers(CodeTypeDeclaration genClass, Type entityType)
        {
            foreach (
                PropertyInfo pi in
                entityType.GetInstanceProperties().Where(pi => FilterPropertyTypes.Contains(pi.PropertyType)))
            {
                string fieldName    = string.Format("_Filter{0}", pi.Name);
                string propertyName = string.Format("Filter{0}", pi.Name);
                bool   isValueType  = pi.PropertyType.IsValueType;

                bool isNullable = (pi.PropertyType.IsGenericType &&
                                   pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>));

                string fieldType = isValueType && !isNullable
                                       ? string.Format("System.Nullable<{0}>", pi.PropertyType)
                                       : pi.PropertyType.FullName;

                #region field

                //generate field
                var clsMember = new CodeMemberField
                {
                    Name       = string.Format(fieldName),
                    Attributes = MemberAttributes.Private,
                    Type       = new CodeTypeReference(fieldType)
                };

                genClass.Members.Add(clsMember);

                #endregion

                #region Property

                //generate property getter and setter
                var property = new CodeMemberProperty
                {
                    Name       = string.Format(propertyName),
                    Type       = new CodeTypeReference(fieldType),
                    Attributes = MemberAttributes.Public
                };

                //Add [Display] Attribute
                property.DefineDisplayAttribute(null, true);

                var codeFieldReferenceExpression = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(),
                                                                                    fieldName);
                var codePropertySetValueReferenceExpression = new CodePropertySetValueReferenceExpression();

                #region getter

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

                #endregion

                #region setter

                //if (_PropValue == value) return;
                property.SetStatements.Add(
                    new CodeConditionStatement(new CodeBinaryOperatorExpression(codeFieldReferenceExpression,
                                                                                CodeBinaryOperatorType.ValueEquality,
                                                                                codePropertySetValueReferenceExpression),
                                               new CodeMethodReturnStatement()));


                var beforeVariableDeclaration = new CodeVariableDeclarationStatement(typeof(object), "before");
                var beforeVariableReference   = new CodeVariableReferenceExpression("before");

                //object before;
                property.SetStatements.Add(beforeVariableDeclaration);

                //before = _PropValue;
                property.SetStatements.Add(new CodeAssignStatement(beforeVariableReference,
                                                                   codeFieldReferenceExpression));

                //_PropValue = value;
                property.SetStatements.Add(new CodeAssignStatement(codeFieldReferenceExpression,
                                                                   codePropertySetValueReferenceExpression));


                var onPropertyChangedMethodReference =
                    new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "OnPropertyChanged");

                //OnPropertyChanged("PropertyName",before,value)
                property.SetStatements.Add(new CodeMethodInvokeExpression(onPropertyChangedMethodReference,
                                                                          new CodePrimitiveExpression(propertyName),
                                                                          beforeVariableReference,
                                                                          codePropertySetValueReferenceExpression));

                #endregion

                genClass.Members.Add(property);

                #endregion
            }
        }
Пример #2
0
        public static CodePropertyReferenceExpression DefineProperty(this Type fieldType,
                                                                     CodeTypeDeclaration codeTypeDeclaration,
                                                                     CodeFieldReferenceExpression
                                                                     codeFieldReferenceExpression,
                                                                     string propertyName,
                                                                     bool?autoGenerateField  = null,
                                                                     bool?autoGenerateFilter = null,
                                                                     Action <CodeMemberProperty> customization = null)
        {
            //var propertyName = string.Format("{0}{1}", propertyNamePrefix, propertyInfo.Name);
            //var fieldType = propertyInfo.PropertyType.FullName;

            //generate property getter and setter
            var property = new CodeMemberProperty
            {
                Name       = string.Format(propertyName),
                Type       = new CodeTypeReference(fieldType),
                Attributes = MemberAttributes.Public
            };

            //Add [Display] Attribute
            property.DefineDisplayAttribute(autoGenerateField, autoGenerateFilter);

            var codePropertySetValueReferenceExpression = new CodePropertySetValueReferenceExpression();

            #region getter

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

            #endregion

            #region setter

            //if (_PropValue == value) return;
            property.SetStatements.Add(
                new CodeConditionStatement(new CodeBinaryOperatorExpression(codeFieldReferenceExpression,
                                                                            CodeBinaryOperatorType.ValueEquality,
                                                                            codePropertySetValueReferenceExpression),
                                           new CodeMethodReturnStatement()));


            var beforeVariableDeclaration = new CodeVariableDeclarationStatement(typeof(object), "before");
            var beforeVariableReference   = new CodeVariableReferenceExpression("before");

            //object before;
            property.SetStatements.Add(beforeVariableDeclaration);

            //before = _PropValue;
            property.SetStatements.Add(new CodeAssignStatement(beforeVariableReference,
                                                               codeFieldReferenceExpression));

            //_PropValue = value;
            property.SetStatements.Add(new CodeAssignStatement(codeFieldReferenceExpression,
                                                               codePropertySetValueReferenceExpression));


            var onPropertyChangedMethodReference =
                new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), "OnPropertyChanged");

            //OnPropertyChanged("PropertyName",before,value)
            property.SetStatements.Add(new CodeMethodInvokeExpression(onPropertyChangedMethodReference,
                                                                      new CodePrimitiveExpression(propertyName),
                                                                      beforeVariableReference,
                                                                      codePropertySetValueReferenceExpression));

            #endregion

            //customize definition...
            if (customization != null)
            {
                customization(property);
            }


            var codePropertyReferenceExpression = new CodePropertyReferenceExpression(
                new CodeThisReferenceExpression(), propertyName);

            return(codePropertyReferenceExpression);
        }