Reference3-Schnittstelle
Aktualisiert: November 2007
Erweitert die Reference2-Schnittstelle des VSLangProj2-Namespaces.
Assembly: VSLangProj80 (in VSLangProj80.dll)
Im folgenden Beispiel werden einem geöffneten Visual Basic-, Visual C#- oder Visual J#-Projekt zwei Verweise hinzugefügt. Anschließend wird die Funktion GetRefTypeName aufgerufen, um den Verweistyp anzuzeigen, und dann die Funktion ReportReferences, um zusätzliche Verweiseigenschaften anzuzeigen. Um dieses Beispiel als Add-In auszuführen, informieren Sie sich unter Gewusst wie: Kompilieren und Ausführen der Codebeispiele für das Automatisierungsobjektmodell.
Die Standardpfade für die hinzugefügten Verweise lauten: <Stammpfad der Installation>\Programme\Microsoft.NET\Primary Interop Assemblies für adodb.dll und <Stammpfad der Installation>\Programme\Gemeinsame Dateien\SpeechEngines\Microsoft für spcommon.dll. Ersetzen Sie <Dateipfad> im Beispiel durch diese oder andere geeignete Dateipfade.
using System.Windows.Forms; using VSLangProj; using VSLangProj2; using VSLangProj80; public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom) { applicationObject = (DTE2)application; addInInstance = (AddIn)addInInst; AddNewReference(((DTE2)applicationObject)); } public void AddNewReference(DTE2 dte) { Project aProject = null; VSProject2 aVSProject = null; aProject = applicationObject.Solution.Projects.Item(1); aVSProject = ((VSProject2)(applicationObject.Solution.Projects.Item(1).Object)); // Add an Assembly reference and display its type and a report. Reference3 newRef = null; // Replace <file path> with an actual file path. newRef = ((Reference3)(aVSProject.References.Add(@" <file path>\adodb.dll"))); MessageBox.Show("The " + newRef.Name + " added, is of type:" + "\n" + GetRefTypeName(newRef)); MessageBox.Show("A report on " + newRef.Name + ":" + "\n" + ReportReferences(newRef)); // Add a COM reference and display its type and a report. // Replace <file path> with an actual file path. newRef = ((Reference3)(aVSProject.References.Add(@" <file path>\spcommon.dll"))); MessageBox.Show("The " + newRef.Name + " added, is of type:" + "\n" + GetRefTypeName(newRef)); MessageBox.Show("A report on " + newRef.Name + ":" + "\n" + ReportReferences(newRef)); } private string GetRefTypeName(Reference3 refIdent) { string type = null; switch (refIdent.Type) { case prjReferenceType.prjReferenceTypeActiveX: type = "COM"; break; case prjReferenceType.prjReferenceTypeAssembly: type = "Assembly"; break; } return type; } public string ReportReferences(Reference3 aRef) { string report = ""; string type = null; // Each entry in the ArrayList contains a label and a value. System.Collections.ArrayList ht = new System.Collections.ArrayList(); VSLangProj.Reference temp = aRef; ht.Add(new string[] { "Name", temp.Name }); ht.Add(new string[] { "Description", temp.Description }); ht.Add(new string[] { "Version", string.Format("{0}.{1}.{2}.{3}" , temp.MajorVersion, temp.MinorVersion, temp.BuildNumber, temp.RevisionNumber) }); ht.Add(new string[] { "Location", temp.ContainingProject.FullName }); switch (temp.Type) { case prjReferenceType.prjReferenceTypeActiveX: type = "COM"; break; case prjReferenceType.prjReferenceTypeAssembly: type = "Assembly"; break; } ht.Add(new string[] { "Type", type }); ht.Add(new string[] { "Culture", temp.Culture }); string[] datas = null; foreach (string[] temp1 in ht) { datas = temp1; report += datas[0] + "\t" + datas[1] + "\n"; } return report; }