我正在开发一个用于C#的添加项,只能在调试期间使用。一旦实例化,我的添加需要查找特定类或接口的所有实例以显示关于找到的数据的图表。

如何在我的扩展名中找到或访问这些对象?我可以在我的扩展中访问DTE2应用程序对象,但我不确定如何搜索由VS调试的实际代码我在想我可能会以某种方式能够使用反射,但我不确定在哪里看。

谢谢。

有帮助吗?

解决方案

I've implemented a plugin that searches through dlls in a given directory and finds classes that implement a particular interface. Below is the class I used to do this:

public class PlugInFactory<T>
{
    public T CreatePlugin(string path)
    {
        foreach (string file in Directory.GetFiles(path, "*.dll"))
        {
            foreach (Type assemblyType in Assembly.LoadFrom(file).GetTypes())
            {
                Type interfaceType = assemblyType.GetInterface(typeof(T).FullName);

                if (interfaceType != null)
                {
                    return (T)Activator.CreateInstance(assemblyType);
                }
            }
        }

        return default(T);
    }
}

All you have to do is initialize this class with something like this:

PluginLoader loader = new PlugInFactory<InterfaceToSearchFor>();
InterfaceToSearchFor instanceOfInterface = loader.CreatePlugin(AppDomain.CurrentDomain.BaseDirectory);

其他提示

This type of operation isn't really possible from a Visual Studio plugin. The object alive when debugging live in the debugee process while your add-in is running in the Visual Studio process. It's not possible to access arbitrary objects across process boundaries in .Net.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top