/// <summary> /// Gets the access attribute value for given variable depending on the options. /// Returns 'true' if new access value is different than the currently stored one. /// Otherwise no action may be taken as variable doesn't need to be changed. /// </summary> public static bool GetVariableAttributes(CodeVariable var, PropertyGeneratorOptions options, out vsCMAccess access) { switch (options & PropertyGeneratorOptions.ForceVarMask) { case PropertyGeneratorOptions.ForceVarDontChange: access = vsCMAccess.vsCMAccessDefault; return(false); case PropertyGeneratorOptions.ForceVarPublic: access = vsCMAccess.vsCMAccessPublic; break; case PropertyGeneratorOptions.ForceVarInternal: access = vsCMAccess.vsCMAccessAssemblyOrFamily; break; case PropertyGeneratorOptions.ForceVarProtected: access = vsCMAccess.vsCMAccessProtected; break; case PropertyGeneratorOptions.ForceVarProtectedInternal: access = vsCMAccess.vsCMAccessProjectOrProtected; break; case PropertyGeneratorOptions.ForceVarPrivate: access = vsCMAccess.vsCMAccessPrivate; break; default: // by default decrease to private: access = vsCMAccess.vsCMAccessPrivate; break; } // validate if real change has been made: return(var.Access != access); }
private static string GenerateSourceCodeOutput(string codeClassName, IEnumerable <CodeVariable> vars, IList <string> varNames, IList <string> propNames, CodeModelLanguages language, PropertyGeneratorOptions options, string regionName) { // generate output: if (vars != null) { CodeTypeMemberCollection code = new CodeTypeMemberCollection(); int index = 0; // serialize variables: foreach (CodeVariable var in vars) { code.Add(VariableHelper.GetProperty(codeClassName, propNames[index], var, varNames[index], options)); // replace the variable: var.Name = varNames[index]; try { vsCMAccess access; if (VariableHelper.GetVariableAttributes(var, options, out access)) { var.Access = access; } } catch { } index++; } if ((options & PropertyGeneratorOptions.SuppressRegion) != PropertyGeneratorOptions.SuppressRegion) { code[0].StartDirectives.Add(new CodeRegionDirective(CodeRegionMode.Start, regionName)); code[code.Count - 1].EndDirectives.Add(new CodeRegionDirective(CodeRegionMode.End, regionName)); } return(CodeHelper.GenerateFromMember(language, code)); } return(null); }
/// <summary> /// Generates the property description based on the variable. /// It will automatically update the variable inside the editor. /// </summary> public static CodeMemberProperty GetProperty(string className, string propertyName, CodeVariable var, string newVariableName, PropertyGeneratorOptions options) { CodeMemberProperty property = new CodeMemberProperty(); property.Type = new CodeTypeReference(); property.Type.BaseType = " " + var.Type.AsString; property.Name = propertyName; property.Attributes = GetPropertyAttributes(var, options); if (var.IsShared) { // generate getter: if ((options & PropertyGeneratorOptions.Getter) == PropertyGeneratorOptions.Getter) { property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(className), newVariableName))); } // generate setter: if ((options & PropertyGeneratorOptions.Setter) == PropertyGeneratorOptions.Setter) { property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(className), newVariableName), new CodePropertySetValueReferenceExpression())); } } else { // generate getter: if ((options & PropertyGeneratorOptions.Getter) == PropertyGeneratorOptions.Getter) { property.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), newVariableName))); } // generate setter: if ((options & PropertyGeneratorOptions.Setter) == PropertyGeneratorOptions.Setter) { property.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), newVariableName), new CodePropertySetValueReferenceExpression())); } } // set comments: if ((options & PropertyGeneratorOptions.SuppressComment) != PropertyGeneratorOptions.SuppressComment) { string commentFormat; switch (options & PropertyGeneratorOptions.GetterAndSetter) { default: commentFormat = "Gets or sets the value of {0}."; break; case PropertyGeneratorOptions.Getter: commentFormat = "Gets the value of {0}."; break; case PropertyGeneratorOptions.Setter: commentFormat = "Sets the value of {0}."; break; } property.Comments.Add(new CodeCommentStatement("<summary>", true)); property.Comments.Add(new CodeCommentStatement(string.Format(commentFormat, property.Name), true)); property.Comments.Add(new CodeCommentStatement("</summary>", true)); } return(property); }
/// <summary> /// Gets the access attributes of the proprety based on the variable and the given options. /// </summary> public static MemberAttributes GetPropertyAttributes(CodeVariable var, PropertyGeneratorOptions options) { MemberAttributes attributes = MemberAttributes.Final; if (var.IsShared) { attributes |= MemberAttributes.Static; } switch (options & PropertyGeneratorOptions.ForcePropertyMask) { case PropertyGeneratorOptions.ForcePropertyAsVar: { switch (var.Access) { case vsCMAccess.vsCMAccessPrivate: attributes |= MemberAttributes.Private; break; case vsCMAccess.vsCMAccessProtected: attributes |= MemberAttributes.Family; break; case vsCMAccess.vsCMAccessProject: case vsCMAccess.vsCMAccessProjectOrProtected: case vsCMAccess.vsCMAccessAssemblyOrFamily: attributes |= MemberAttributes.FamilyOrAssembly; break; default: attributes |= MemberAttributes.Public; break; } } break; case PropertyGeneratorOptions.ForcePropertyPublic: attributes |= MemberAttributes.Public; break; case PropertyGeneratorOptions.ForcePropertyInternal: attributes |= MemberAttributes.Assembly; break; case PropertyGeneratorOptions.ForcePropertyProtected: attributes |= MemberAttributes.Family; break; case PropertyGeneratorOptions.ForcePropertyProtectedInternal: attributes |= MemberAttributes.FamilyOrAssembly; break; case PropertyGeneratorOptions.ForcePropertyPrivate: attributes |= MemberAttributes.Private; break; default: { // by default generated increased one level: switch (var.Access) { case vsCMAccess.vsCMAccessPrivate: case vsCMAccess.vsCMAccessProtected: attributes |= MemberAttributes.Assembly; break; case vsCMAccess.vsCMAccessProject: case vsCMAccess.vsCMAccessProjectOrProtected: case vsCMAccess.vsCMAccessAssemblyOrFamily: attributes |= MemberAttributes.FamilyOrAssembly; break; default: attributes |= MemberAttributes.Public; break; } } break; } return(attributes); }