示例#1
0
        /// <summary>
        /// Creates a backing field for the given property
        /// </summary>
        /// <param name="property">The code property</param>
        /// <param name="type">The type of the property</param>
        /// <param name="initialValue">The initial value expression for the field</param>
        /// <returns>A reference to the generated backing field</returns>
        public static CodeFieldReferenceExpression CreateBackingField(this CodeMemberProperty property, CodeTypeReference type, CodeExpression initialValue)
        {
            var reference = CodeDomHelper.GetOrCreateUserItem <CodeFieldReferenceExpression>(property, CodeDomHelper.BackingFieldKey);

            if (reference == null)
            {
                var field = new CodeMemberField()
                {
                    Attributes = MemberAttributes.Private,
                    Name       = "_" + property.Name.ToCamelCase(),
                    Type       = type ?? new CodeTypeReference(typeof(object))
                };

                field.WriteDocumentation(string.Format("The backing field for the {0} property", property.Name));

                property.DependentMembers(true).Add(field);

                if (initialValue != null)
                {
                    field.InitExpression = initialValue;
                }

                reference = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), field.Name);
                CodeDomHelper.SetUserItem(property, CodeDomHelper.BackingFieldRefKey, reference);
                CodeDomHelper.SetUserItem(property, CodeDomHelper.BackingFieldKey, field);
            }
            return(reference);
        }
        /// <summary>
        /// Generates an OnChanging-pattern for the given property
        /// </summary>
        /// <param name="property">The code property</param>
        /// <returns></returns>
        public static CodeStatement CreateOnChangingEventPattern(this CodeMemberProperty property)
        {
            var changingEvent = new CodeMemberEvent()
            {
                Name       = property.Name + "Changing",
                Type       = new CodeTypeReference(typeof(EventHandler).Name),
                Attributes = MemberAttributes.Public | MemberAttributes.Final
            };

            changingEvent.WriteDocumentation(string.Format("Gets fired before the {0} property changes its value", property.Name));

            var eventType = new CodeTypeReference(typeof(EventArgs).Name);
            var eventData = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(eventType), "Empty");

            var onChanging = CreateOnChangedMethod(changingEvent, eventType);

            var dependent = property.DependentMembers(true);

            dependent.Add(onChanging);
            dependent.Add(changingEvent);

            var onChangedRef = new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), onChanging.Name);

            return(new CodeExpressionStatement(new CodeMethodInvokeExpression(onChangedRef, eventData)));
        }
示例#3
0
        /// <summary>
        /// Generates an OnChanged-pattern for the given property
        /// </summary>
        /// <param name="property">The code property</param>
        /// <returns>The statement to call the OnChanged method for the given property</returns>
        public static CodeStatement CreateOnChangedEventPattern(this CodeMemberProperty property, CodeTypeReference eventType, CodeExpression eventData)
        {
            CodeTypeReference handlerType;

            if (eventType == null)
            {
                handlerType = typeof(EventHandler).ToTypeReference();
            }
            else
            {
                handlerType = typeof(EventHandler <>).ToTypeReference(eventType);
            }
            var changedEvent = new CodeMemberEvent()
            {
                Name       = property.Name + "Changed",
                Type       = handlerType,
                Attributes = MemberAttributes.Public | MemberAttributes.Final
            };

            changedEvent.WriteDocumentation(string.Format("Gets fired when the {0} property changed its value", property.Name));

            if (eventType == null)
            {
                eventType = new CodeTypeReference(typeof(EventArgs).Name);
                eventData = new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(eventType), "Empty");
            }
            var onChanged = CodeDomHelper.CreateRaiseMethod(changedEvent, eventType);

            var dependent = property.DependentMembers(true);

            dependent.Add(onChanged);
            dependent.Add(changedEvent);

            var onChangedRef = new CodeMethodReferenceExpression(new CodeThisReferenceExpression(), onChanged.Name);

            return(new CodeExpressionStatement(new CodeMethodInvokeExpression(onChangedRef, eventData)));
        }