C # الوظيفة الإضافية: كيف يمكنك الوصول إلى مثيلات وقت التشغيل من الكائنات أثناء تصحيح الأخطاء؟
-
14-11-2019 - |
سؤال
أنا أقدم إضافة في 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.