首页 > 解决方案 > 是否可以从具有扩展名的项目中创建一个对象

问题描述

我需要的是使用 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();

           

            
        }}

所以我真正想知道我正在尝试的是可能还是不可能?是否有可能如何在此处获取该组件,或者是否有其他更好的方法可以做到这一点?

标签: c#.netreflectionvsixenvdte

解决方案


推荐阅读