我需要在可能的项目引用中替换相当大的解决方案中的所有文件引用。 这项繁琐的任务可以自动执行吗?
答案 0 :(得分:1)
创建适合我的Visual Studio加载项非常简单。
这里执行脏工作的加载项代码:
public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
{
handled = false;
LogMessage(string.Format("Start investigation at {0} {1}", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));
if(executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
{
if(commandName == "ReferenceDoctor.Connect.ReferenceDoctor")
{
foreach (Project project in _applicationObject.Solution.Projects)
{
VSProject vsProject = project.Object as VSProject;
if (vsProject != null)
{
foreach (Reference reference in vsProject.References)
{
// if it not project reference
if (reference.SourceProject == null)
{
string refPath = reference.Path;
string refAssemblyName = Path.GetFileNameWithoutExtension(refPath);
Project refProject = FindProjectWithinSolution(refAssemblyName);
// if within solution found project with suitable name
if (refProject != null)
{
try
{
//replace reference with project reference
reference.Remove();
vsProject.References.AddProject(refProject);
LogMessage(string.Format(" [{0}] Reference to {1} replaced. ({2} -> {3})", vsProject.Project.Name, refAssemblyName, refPath, refProject.FullName));
}
catch (Exception exc)
{
LogMessage(string.Format(" [{0}] ERROR during replacing reference to {1} ({2} -> {3}): {4}", vsProject.Project.Name, refAssemblyName, refPath, refProject.FullName, exc.Message));
}
}
}
}
}
}
LogMessage(string.Format("Completed at {0} {1}\r\n", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString()));
handled = true;
return;
}
}
}
private Project FindProjectWithinSolution(string projectName)
{
foreach (Project project in _applicationObject.Solution.Projects)
{
VSProject vsProject = project.Object as VSProject;
if (vsProject != null)
{
if (string.Compare(vsProject.Project.Name, projectName, ignoreCase: true) == 0)
{
return project;
}
}
}
return null;
}
我还发现有用的扩展,在添加某个项目时从文件引用切换到项目引用,并在删除时反向切换: http://markkemper1.blogspot.ru/2011/09/project-to-file-reference-switcher-for.html
我使用的完整加载项代码:here