private CodeSnippetTypeMember RewriteMemberAutoProperty(CodeMemberAutoImplementedProperty autoProperty) { using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture)) { this.OutputCustomAttributes(sw, autoProperty.CustomAttributes); this.WriteIndentation(sw); this.OutputMemberAccessModifier(sw, autoProperty.Attributes); this.OutputVTableModifier(sw, autoProperty.Attributes); this.OutputMemberScopeModifier(sw, autoProperty.Attributes); this.generator.GenerateCodeFromExpression(new CodeTypeReferenceExpression(autoProperty.Type), sw, null); sw.Write(" "); sw.Write(autoProperty.Name); sw.Write(" { "); if ((autoProperty.GetAttributes & MemberAttributes.AccessMask) != MemberAttributes.Public) { this.OutputMemberAccessModifier(sw, autoProperty.GetAttributes); } sw.Write("get; "); if ((autoProperty.SetAttributes & MemberAttributes.AccessMask) != MemberAttributes.Public) { this.OutputMemberAccessModifier(sw, autoProperty.SetAttributes); } sw.Write("set; }"); return(new CodeSnippetTypeMember(sw.ToString())); } }
/// <summary> /// Rewrites CodeTypeMember /// </summary> /// <param name="e">A CodeTypeMember to be rewritten.</param> /// <param name="didRewrite">A value which will be set to true if the rewriting returned a new object.</param> /// <returns>Rewritten CodeTypeMember.</returns> protected override CodeTypeMember Rewrite(CodeTypeMember e, ref bool didRewrite) { CodeMemberAutoImplementedProperty cmap = e as CodeMemberAutoImplementedProperty; if (cmap != null) { didRewrite = true; return(this.RewriteMemberAutoProperty(cmap)); } return(base.Rewrite(e, ref didRewrite)); }
private static void ApplyPropertyAccessModifier(CodeMemberAutoImplementedProperty codeProp, PropertyAccessModifierAnnotation propertyAccessModifierAnnotation) { if (propertyAccessModifierAnnotation != null) { var maxVisibility = (AccessModifier)Math.Max( (int)propertyAccessModifierAnnotation.GetterAccessModifier, (int)propertyAccessModifierAnnotation.SetterAccessModifier); codeProp.Attributes &= ~MemberAttributes.AccessMask; codeProp.Attributes |= ConvertAccessModifierToMemberAttributes(maxVisibility); if (maxVisibility != propertyAccessModifierAnnotation.GetterAccessModifier) { codeProp.GetAttributes = ConvertAccessModifierToMemberAttributes(propertyAccessModifierAnnotation.GetterAccessModifier); } if (maxVisibility != propertyAccessModifierAnnotation.SetterAccessModifier) { codeProp.SetAttributes = ConvertAccessModifierToMemberAttributes(propertyAccessModifierAnnotation.SetterAccessModifier); } } }
private CodeSnippetTypeMember RewriteMemberAutoProperty(CodeMemberAutoImplementedProperty autoProperty) { using (StringWriter sw = new StringWriter(CultureInfo.InvariantCulture)) { if (autoProperty.GetAttributes != autoProperty.SetAttributes) { // VB doesn't support auto-implemented properties with different access modifiers, so we have // to expand it into a field and a property that gets and sets that field. // Private _PropertyName As Type var fieldName = "_" + autoProperty.Name; this.WriteIndentation(sw); sw.Write("Private " + fieldName + " As "); this.generator.GenerateCodeFromExpression(new CodeTypeReferenceExpression(autoProperty.Type), sw, null); sw.WriteLine(); // Public Virtual Property PropetyName As Type this.OutputCustomAttributes(sw, autoProperty.CustomAttributes); this.WriteIndentation(sw); this.OutputMemberAccessModifier(sw, autoProperty.Attributes); this.OutputVTableModifier(sw, autoProperty.Attributes); this.OutputMemberScopeModifier(sw, autoProperty.Attributes); sw.Write("Property "); sw.Write(autoProperty.Name); sw.Write(" As "); this.generator.GenerateCodeFromExpression(new CodeTypeReferenceExpression(autoProperty.Type), sw, null); sw.WriteLine(this.GetArrayPostfix(autoProperty.Type)); // Protected Get this.nestingLevel++; this.WriteIndentation(sw); if ((autoProperty.GetAttributes & MemberAttributes.AccessMask) != MemberAttributes.Public) { this.OutputMemberAccessModifier(sw, autoProperty.GetAttributes); } sw.WriteLine("Get"); // Return _PropertyName this.nestingLevel++; this.WriteIndentation(sw); sw.Write("Return "); sw.WriteLine(fieldName); // End Get this.nestingLevel--; this.WriteIndentation(sw); sw.WriteLine("End Get"); // Private Set(ByVal value As Type) this.WriteIndentation(sw); if ((autoProperty.SetAttributes & MemberAttributes.AccessMask) != MemberAttributes.Public) { this.OutputMemberAccessModifier(sw, autoProperty.SetAttributes); } sw.Write("Set(ByVal value As "); this.generator.GenerateCodeFromExpression(new CodeTypeReferenceExpression(autoProperty.Type), sw, null); sw.Write(this.GetArrayPostfix(autoProperty.Type)); sw.WriteLine(")"); // Me._PropertyName = value this.nestingLevel++; this.WriteIndentation(sw); sw.Write("Me."); sw.Write(fieldName); sw.WriteLine(" = value"); // End Set this.nestingLevel--; this.WriteIndentation(sw); sw.WriteLine("End Set"); // End Property this.nestingLevel--; this.WriteIndentation(sw); sw.Write("End Property"); } else { this.OutputCustomAttributes(sw, autoProperty.CustomAttributes); this.WriteIndentation(sw); this.OutputMemberAccessModifier(sw, autoProperty.Attributes); this.OutputVTableModifier(sw, autoProperty.Attributes); this.OutputMemberScopeModifier(sw, autoProperty.Attributes); sw.Write("Property "); sw.Write(autoProperty.Name); sw.Write(" As "); this.generator.GenerateCodeFromExpression(new CodeTypeReferenceExpression(autoProperty.Type), sw, null); sw.Write(this.GetArrayPostfix(autoProperty.Type)); } return(new CodeSnippetTypeMember(sw.ToString())); } }