c# - 是否可以从具有扩展名的项目中创建一个对象
问题描述
我需要的是使用 VSIX 扩展在主项目中创建 C# 类的实例。这可能吗?
示例:- 一个安装了我的扩展的项目。当扩展调用它应该创建该打开文件的实例。在 Vsix 项目内部,以便将这些方法用于其他目的。
我在这里尝试过什么;
public Void ExecuteCommand(SaveCommandArgs args, CommandExecutionContext executionContext)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (args.SubjectBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument textDoc))
{
var filePath = textDoc.FilePath;
// Here I get the Open File Locatiob
var dte = Package.GetGlobalService(typeof(DTE)) as DTE;
ProjectItem item = dte.Solution?.FindProjectItem(filePath);
var name = Path.GetFileNameWithoutExtension(filePath);
// test is the project I used for Extension. namespace Test in the project that used the Extension.
// Here When I tried to create a object from that class In give me assembly error. Activator point to the Current VSIX assembly not the Project assembly we want
object obj = ((ObjectHandle)Activator.CreateInstance(null, "Test."+name)).Unwrap();
Type type = obj.GetType();
}}
所以我真正想知道我正在尝试的是可能还是不可能?是否有可能如何在此处获取该组件,或者是否有其他更好的方法可以做到这一点?
解决方案
推荐阅读
- sql - 创建位图索引-ora-00921
- javascript - 自动从 https/ws 服务器获取服务器证书
- html - .m3u8 视频源仅在 macOS 上运行
- vue.js - 使用 Vuejs 时如何优化我的 app.js 文件
- arrays - Bash 数组未正确写入变量
- javascript - 有 2 个名称的 JavaScript 函数?
- amazon-web-services - Amazon Redshift 是否有自己的存储后端
- python - 使用 if 根据值添加列值
- bash - Bash 终端:仅将特定行写入日志文件
- docker - 如何使用 docker run 命令传递环境变量?