Visual Studio 可以从 NuGet 导航到 .NET 程序集的源文件,请参阅源链接 我想增强名为 Open on GitHub 的现有 Visual Studio 扩展。 我的目标是添加直接从 Visual Studio 导航到 Web 中的源链接文件的功能。 但是,我正在努力确定如何在扩展中检索必要的信息。
这是我的问题:
资源:
有没有一种方法可以以编程方式检索 Visual 的 URL Studio 下载了文件?
应该没有开放的API来实现这一点,但是元文件pdb应该能够实现一些信息:
using System;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
class Program
{
static void Main(string[] args)
{
string pdbPath = @"C:\Users\Administrator\Downloads\Newtonsoft.Json.pdb"; // Specify the path to your PDB file
string sourceLinkJson = GetSourceLinkJson(pdbPath);
if (sourceLinkJson != null)
{
Console.WriteLine("Source Link JSON:");
Console.WriteLine(sourceLinkJson);
}
else
{
Console.WriteLine("Source Link information not found.");
}
}
static string GetSourceLinkJson(string pdbPath)
{
// The well-known Guid for Source Link
Guid sourceLinkGuid = new Guid("CC110556-A091-4D38-9FEC-25AB9A351A6A");
using (FileStream stream = File.OpenRead(pdbPath))
using (MetadataReaderProvider metadataReaderProvider = MetadataReaderProvider.FromPortablePdbStream(stream))
{
MetadataReader metadataReader = metadataReaderProvider.GetMetadataReader();
foreach (var handle in metadataReader.CustomDebugInformation)
{
var cdi = metadataReader.GetCustomDebugInformation(handle);
var cdiKindGuid = metadataReader.GetGuid(cdi.Kind);
if (cdiKindGuid == sourceLinkGuid)
{
// If the kind matches the Source Link Guid, get the value
var value = metadataReader.GetBlobBytes(cdi.Value);
return System.Text.Encoding.UTF8.GetString(value);
}
}
}
return null;
}
}
是否可以确定源代码控制提供商的类型 (GitHub、GitLab、Bitbucket 等)基于下载文件的 内容或元数据?
您可以根据URL中包含的信息来判断。