Example #1
0
		///-------------------------------------------------------------------------------------------------------------
		/// <summary>
		///	 Locates the code model CodeClass 
		/// </summary>
		///-------------------------------------------------------------------------------------------------------------
		protected virtual CodeClass FindClass(VsHierarchyItem item, string className)
		{
			if (item != null)
			{
				try
				{
					ProjectItem projectItem = item.ProjectItem();
					if (projectItem != null)
					{
						FileCodeModel fileCodeModel = projectItem.FileCodeModel;
						if (fileCodeModel != null)
						{
							return FindClass(fileCodeModel.CodeElements, className);
						}
					}
				}
				catch
				{
				}
			}
			return null;
		}
Example #2
0
		///-------------------------------------------------------------------------------------------------------------
		/// <summary>
		///	 Gets a VshierarchyItem for the designer file if possible.
		///	 Will create new file if specified and not existing.
		/// </summary>
		///-------------------------------------------------------------------------------------------------------------
		protected virtual VsHierarchyItem GetDesignerItem(VsHierarchyItem itemCode, bool create)
		{
			VsHierarchyItem itemDesigner = null;

			if (itemCode != null)
			{
				// Calculate codebehind and designer file paths 
				string codeBehindFile = itemCode.FullPath();
				string designerFile = null;
				if (_isPartialClassDisabled)
				{
					designerFile = codeBehindFile;
				}
				else if (!string.IsNullOrEmpty(codeBehindFile))
				{
					designerFile = codeBehindFile.Insert(codeBehindFile.LastIndexOf("."), ".designer");
				}

				// Try to locate existing designer file
				if (!string.IsNullOrEmpty(designerFile))
				{
					itemDesigner = VsHierarchyItem.CreateFromMoniker(designerFile, _hierarchy);
					if (itemDesigner != null)
					{
						return itemDesigner;
					}
				}

				// Create empty designer file if requested
				if (create && !string.IsNullOrEmpty(designerFile))
				{
					ProjectItem projectItemCode = itemCode.ProjectItem();
					if (projectItemCode != null)
					{
						ProjectItems projectItems = projectItemCode.Collection;
						if (projectItems != null)
						{
							try
							{
								using (StreamWriter sw = File.CreateText(designerFile))
								{
									sw.WriteLine(" ");
								}

								projectItems.AddFromFileCopy(designerFile);
							}
							catch
							{
							}

							itemDesigner = VsHierarchyItem.CreateFromMoniker(designerFile, _hierarchy);
							if (itemDesigner != null)
							{
								return itemDesigner;
							}
						}
					}
				}
			}

			return itemDesigner;
		}