Microsoft.VisualStudio.Project.TokenProcessor.GetFileNamespace C# (CSharp) Method

GetFileNamespace() private method

private GetFileNamespace ( string fileFullPath, ProjectNode node ) : string
fileFullPath string
node ProjectNode
return string
        public virtual string GetFileNamespace(string fileFullPath, ProjectNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            // Get base namespace from the project
            string namespce = node.GetProjectProperty("RootNamespace", _PersistStorageType.PST_PROJECT_FILE);
            if(String.IsNullOrEmpty(namespce))
                namespce = Path.GetFileNameWithoutExtension(fileFullPath); ;

            // If the item is added to a subfolder, the name space should reflect this.
            // This is done so that class names from 2 files with the same name but different
            // directories don't conflict.
            string relativePath = Path.GetDirectoryName(fileFullPath);
            string projectPath = Path.GetDirectoryName(node.GetMKDocument());
            // Our project system only support adding files that are sibling of the project file or that are in subdirectories.
            if(String.Compare(projectPath, 0, relativePath, 0, projectPath.Length, true, CultureInfo.CurrentCulture) == 0)
            {
                relativePath = relativePath.Substring(projectPath.Length);
            }
            else
            {
                Debug.Fail("Adding an item to the project that is NOT under the project folder.");
                // We are going to use the full file path for generating the namespace
            }

            // Get the list of parts
            int index = 0;
            string[] pathParts;
            pathParts = relativePath.Split(Path.DirectorySeparatorChar);

            // Use a string builder with default size being the expected size
            StringBuilder result = new StringBuilder(namespce, namespce.Length + relativePath.Length + 1);
            // For each path part
            while(index < pathParts.Length)
            {
                string part = pathParts[index];
                ++index;

                // This could happen if the path had leading/trailing slash, we want to ignore empty pieces
                if(String.IsNullOrEmpty(part))
                    continue;

                // If we reach here, we will be adding something, so add a namespace separator '.'
                result.Append('.');

                // Make sure it starts with a letter
                if(!char.IsLetter(part, 0))
                    result.Append('N');

                // Filter invalid namespace characters
                foreach(char c in part)
                {
                    if(char.IsLetterOrDigit(c))
                        result.Append(c);
                }
            }
            return result.ToString();
        }

Usage Example

Exemplo n.º 1
0
        /// <summary>
        /// Gets the properties of the project node. 
        /// </summary>
        /// <param name="propId">The __VSHPROPID of the property.</param>
        /// <returns>A property dependent value. See: <see cref="__VSHPROPID"/> for details.</returns>
        public override object GetProperty(int propId)
        {
            switch ((__VSHPROPID)propId)
            {
                case __VSHPROPID.VSHPROPID_ConfigurationProvider:
                    return this.ConfigProvider;

                case __VSHPROPID.VSHPROPID_ProjectName:
                    return this.Caption;

                case __VSHPROPID.VSHPROPID_ProjectDir:
                    return this.ProjectFolder;

                case __VSHPROPID.VSHPROPID_TypeName:
                    return this.ProjectType;

                case __VSHPROPID.VSHPROPID_ShowProjInSolutionPage:
                    return this.ShowProjectInSolutionPage;

                case __VSHPROPID.VSHPROPID_ExpandByDefault:
                    return true;

                // Use the same icon as if the folder was closed
                case __VSHPROPID.VSHPROPID_OpenFolderIconIndex:
                    return GetProperty((int)__VSHPROPID.VSHPROPID_IconIndex);

                case __VSHPROPID.VSHPROPID_DefaultNamespace:
                    TokenProcessor processor = new TokenProcessor();
                    return processor.GetFileNamespace(this.Url, this);
            }

            switch ((__VSHPROPID2)propId)
            {
                case __VSHPROPID2.VSHPROPID_SupportsProjectDesigner:
                    return this.SupportsProjectDesigner;

                case __VSHPROPID2.VSHPROPID_PropertyPagesCLSIDList:
                    return Utilities.CreateSemicolonDelimitedListOfStringFromGuids(this.GetConfigurationIndependentPropertyPages());

                case __VSHPROPID2.VSHPROPID_CfgPropertyPagesCLSIDList:
                    return Utilities.CreateSemicolonDelimitedListOfStringFromGuids(this.GetConfigurationDependentPropertyPages());

                case __VSHPROPID2.VSHPROPID_PriorityPropertyPagesCLSIDList:
                    return Utilities.CreateSemicolonDelimitedListOfStringFromGuids(this.GetPriorityProjectDesignerPages());

                case __VSHPROPID2.VSHPROPID_Container:
                    return true;
                default:
                    break;
            }

            return base.GetProperty(propId);
        }
All Usage Examples Of Microsoft.VisualStudio.Project.TokenProcessor::GetFileNamespace