///------------------------------------------------------------------------------------------------------------- /// <summary> /// /// </summary> ///------------------------------------------------------------------------------------------------------------- void IVsCodeBehindCodeGenerator.BeginGenerate(string document, string codeBehindFile, string className_Full, bool create) { DisposeGenerateState(); _itemCode = VsHierarchyItem.CreateFromMoniker(codeBehindFile, _hierarchy); _itemDesigner = GetDesignerItem(_itemCode, false); _create = create; _className_Full = className_Full; if (_itemCode != null) { // Get the CodeDomProvider for the language (MergedCodeDomProvider C#/VB) _codeDomProvider = CreateCodeDomProvider(_itemCode.VsItemID); if (_codeDomProvider != null) { // Get the field names so we can preserve location and access bool caseSensitive = IsCaseSensitive(_codeDomProvider); _codeFields = GetFieldNames(_itemCode, _className_Full, caseSensitive, false, 30); _designerFields = GetFieldNames(_itemDesigner, _className_Full, caseSensitive, false, 0); // Generate the class string designerContents = _itemDesigner.GetDocumentText(); TextReader reader = new StringReader(designerContents); _ccu = _codeDomProvider.Parse(reader); GenerateClass(); } } }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// /// </summary> ///------------------------------------------------------------------------------------------------------------- void IVsCodeBehindCodeGenerator.Generate() { DocData ddDesigner = null; DocDataTextWriter designerWriter = null; try { if (_itemCode != null && _codeDomProvider != null) { // Generate the code StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture); _codeDomProvider.GenerateCodeFromCompileUnit(_ccu, stringWriter, _codeGeneratorOptions); string generatedCode = stringWriter.ToString(); // Create designer file if requested if (_itemDesigner == null && _create) { _itemDesigner = GetDesignerItem(_itemCode, true); } // See if generated code changed string designerContents = _itemDesigner.GetDocumentText(); if (!BufferEquals(designerContents, generatedCode)) // Would be nice to just compare lengths but the buffer gets formatted after insertion { ddDesigner = new LockedDocData(_serviceProvider, _itemDesigner.FullPath()); // Try to check out designer file (this throws) ddDesigner.CheckoutFile(_serviceProvider); // Write out the new code designerWriter = new DocDataTextWriter(ddDesigner); designerWriter.Write(generatedCode); designerWriter.Flush(); designerWriter.Close(); } } } finally { if (designerWriter != null) { designerWriter.Dispose(); } if (ddDesigner != null) { ddDesigner.Dispose(); } } }