///------------------------------------------------------------------------------------------------------------- /// <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> /// Cleans up Generate member state. /// </summary> ///------------------------------------------------------------------------------------------------------------- protected virtual void DisposeGenerateState() { try { _itemCode = null; _itemDesigner = null; _ccu = null; _ctd = null; _create = false; _codeFields = null; _designerFields = null; _className_Full = null; _className_Namespace = null; _className_Name = null; if (_codeDomProvider != null) { _codeDomProvider.Dispose(); _codeDomProvider = null; } } catch { } }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// Returns field names in the specified class using code model. /// If publicOnly is true only public fields are returned. /// </summary> ///------------------------------------------------------------------------------------------------------------- protected FieldDataDictionary GetFieldNames(VsHierarchyItem itemCode, string className, bool caseSensitive, bool onlyBaseClasses, int maxDepth) { FieldDataDictionary fields = null; if (itemCode != null) { CodeClass codeClass = FindClass(itemCode, className); if (codeClass != null) { GetFieldNames(codeClass, caseSensitive, onlyBaseClasses, 0, maxDepth, ref fields); } } return fields; }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// Returns public field names in dictionary /// </summary> ///------------------------------------------------------------------------------------------------------------- protected FieldDataDictionary GetFieldNames(string[] publicFields, bool caseSensitive) { FieldDataDictionary fields = null; if (publicFields != null) { foreach (string name in publicFields) { if (!string.IsNullOrEmpty(name)) { FieldData field = new FieldData(name); if (field != null) { if (fields == null) { fields = new FieldDataDictionary(caseSensitive); } try { if (!fields.ContainsKey(field.Name)) { fields.Add(field.Name, field); } } catch { } } } } } return fields; }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// Returns field names in the specified class using code model. /// If publicOnly is true only public fields are returned. /// </summary> ///------------------------------------------------------------------------------------------------------------- protected void GetFieldNames(CodeClass codeClass, bool caseSensitive, bool onlyBaseClasses, int depth, int maxDepth, ref FieldDataDictionary fields) { if (codeClass != null && depth <= maxDepth) { if (!(onlyBaseClasses && depth == 0)) { foreach (CodeElement codeElement in codeClass.Members) { //vsCMElement kind = codeElement.Kind; //vsCMElementVariable CodeVariable codeVariable = codeElement as CodeVariable; if (codeVariable != null) { FieldData field = new FieldData(codeClass, codeVariable, depth); if (field != null && !string.IsNullOrEmpty(field.Name)) { if (fields == null) { fields = new FieldDataDictionary(caseSensitive); } try { if (!fields.ContainsKey(field.Name)) { fields.Add(field.Name, field); } } catch { } } } } } foreach (CodeElement baseCodeElement in codeClass.Bases) { CodeClass baseCodeClass = baseCodeElement as CodeClass; if (baseCodeClass != null) { CodeElements partCodeElements = null; CodeClass2 baseCodeClass2 = baseCodeClass as CodeClass2; if (baseCodeClass2 != null) { vsCMClassKind classKind = baseCodeClass2.ClassKind; if ((classKind | vsCMClassKind.vsCMClassKindPartialClass) == vsCMClassKind.vsCMClassKindPartialClass) { try { partCodeElements = baseCodeClass2.Parts; } catch { } } } if (partCodeElements != null && partCodeElements.Count > 1) { foreach (CodeElement partCodeElement in partCodeElements) { CodeClass partCodeClass = partCodeElement as CodeClass; if (partCodeClass != null) { GetFieldNames(partCodeClass, caseSensitive, onlyBaseClasses, depth + 1, maxDepth, ref fields); } } } else { GetFieldNames(baseCodeClass, caseSensitive, onlyBaseClasses, depth + 1, maxDepth, ref fields); } } } } }