[英]c# Visual Studio ...adding references programmatically
無論如何,可以以編程方式將引用添加到解決方案中嗎?
我有一個加載項按鈕,當用戶按下它時,我想要添加一個引用。 這可能嗎?
像這樣的東西我還沒有測試過
獲取環境
EnvDTE80.DTE2 pEnv = null;
Type myType = Type.GetTypeFromProgID("VisualStudio.DTE.8.0");
pEnv = (EnvDTE80.DTE2)Activator.CreateInstance(myType, true);
得到解決方案。
Solution2 pSolution = (Solution2)pEnv.VS.Solution;
得到你想要的項目
Project pProject = pSolution.Projects[0];
添加參考
pProject.References.Add(string referenceFilePath);
System.Assembly.load
允許您調用庫中不是用您的程序構建的函數。
我是用 vb 的宏做的,但我相信你能明白
DTE.Windows.Item(Constants.vsWindowKindSolutionExplorer).Activate() Dim objProject As EnvDTE.Project Dim i As Long i = DTE.Solution.Projects.Count For Each objProject In DTE.Solution.Projects If (objProject.Name() = "csCA") Then Dim vsproj As VSLangProj.VSProject vsproj = objProject.Object vsproj.References.Add("C:\\Users\\test.dll") End If Next
CodeProject上有一個示例。
該功能包含在單個類elRefManager
,調用的方法是CheckReferences
。 通過選擇左側的 elRefManager.cs 文件,可以在此處查看代碼。
正如文章中所見,您可以這樣做...
private void button1_Click(object sender, System.EventArgs e)
{
int ec;
ec=elRefManager.CheckReferences(null, new string[] {textBox1.Text});
if (ec<0)
MessageBox.Show("An error occurred adding this reference");
if (ec>0)
MessageBox.Show("Could not add " + textBox1.Text +
"\nCheck its spelling and try again");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.