gpt4 book ai didi

visual-studio-2013 - 如何让 VSIX 安装程序自动注册程序集

转载 作者:行者123 更新时间:2023-12-02 08:34:58 27 4
gpt4 key购买 nike

我已经开发了一个自定义代码生成器并通过 VSIX 部署它,问题是我应该在安装 VSIX 后通过 regasm.exe 注册程序集,但我看到一些项目,例如 DSLTool 具有自动注册的自定义代码生成,任何body 知道如何在我的 VSIX 项目中自动注册?

最佳答案

您应该能够执行以下操作:

0。删除旧的(不良做法)COM 代码

  1. 将您的项目build设置编辑为选中“Register for COM interop”。
  2. 编辑您的AssemblyInfo.cs 并设置ComVisiblefalse :

    [assembly: ComVisible(false)]
  3. 假设您的生成器名为 MyCodeGenerator , 打开 MyCodeGenerator 的定义并添加属性:

    [ComVisible(true)]

1。编辑您的 VSIX 项目以启用 pkgdef 文件的生成。

  1. 解决方案资源管理器中右键单击您的项目,然后选择卸载项目
  2. 右键单击卸载的项目并选择编辑MyProject.csproj,其中MyProject 是您的项目名称。
  3. 找到 XML 元素 <GeneratePkgDefFile> .

    1. 如果该元素存在,请确保将其值设置为 true .
    2. 否则,将以下内容添加到第一个 <PropertyGroup> 的末尾项目文件中没有的元素 Condition属性(这几乎总是文件中的第一个 PropertyGroup)。

      <GeneratePkgDefFile>true</GeneratePkgDefFile>
  4. 重复第 3 步以设置 <CopyBuildOutputToOutputDirectory>true .

  5. 保存并关闭 .csproj 文件。
  6. 右键单击解决方案资源管理器中卸载的项目,然后选择重新加载项目
  7. 打开您项目的source.extension.vsixmanifest 文件并找到<Content>元素。添加以下元素作为子元素:

    <VsPackage>|%CurrentProject%|</VsPackage>

    如果您的扩展不提供任何其他内容元素,则整个 <Content>元素现在是这样的:

    <Content>
    <VsPackage>|%CurrentProject%|</VsPackage>
    </Content>

2。定义所需的属性类型

此答案的末尾是 ProvideGeneratorAttribute.csProvideAssemblyObjectAttribute.cs 部分。将这些文件添加到您的项目中。

3。注册代码生成器类

  1. 打开项目的 AssemblyInfo.cs
  2. 假设您的自定义代码生成器类名为 MyCodeGenerator ,将以下属性添加到程序集信息文件。

    [assembly: ProvideAssemblyObject(typeof(MyCodeGenerator))]

4。将您的代码生成器与语言服务相关联

  1. 打开项目的 AssemblyInfo.cs
  2. 假设您的自定义代码生成器类名为 MyCodeGenerator , 并且你想使用 C# 语言服务注册代码生成器,将以下属性添加到程序集信息文件。

    [assembly: ProvideGenerator(
    typeof(MyCodeGenerator),
    VSConstants.UICONTEXT.CSharpProject_string,
    Description = "Description of the generator",
    GeneratesDesignTimeSource = true)]

附录 A:ProvideGeneratorAttribute.cs

免责声明:此代码完全未经测试。

using System;
using Microsoft.VisualStudio.Shell;

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public sealed class ProvideGeneratorAttribute : RegistrationAttribute
{
private readonly Type _generatorType;
private readonly Guid _languageServiceGuid;
private string _name;
private string _description;
private bool _generatesDesignTimeSource;

public ProvideGeneratorAttribute(Type generatorType, string languageServiceGuid)
{
if (generatorType == null)
throw new ArgumentNullException("generatorType");
if (languageServiceGuid == null)
throw new ArgumentNullException("languageServiceGuid");
if (string.IsNullOrEmpty(languageServiceGuid))
throw new ArgumentException("languageServiceGuid cannot be empty");

_generatorType = generatorType;
_languageServiceGuid = new Guid(languageServiceGuid);
_name = _generatorType.Name;
}

public Type GeneratorType
{
get
{
return _generatorType;
}
}

public Guid LanguageServiceGuid
{
get
{
return _languageServiceGuid;
}
}

public string Name
{
get
{
return _name;
}

set
{
if (value == null)
throw new ArgumentNullException("value");
if (string.IsNullOrEmpty(value))
throw new ArgumentException("value cannot be empty");

_name = value;
}
}

public string Description
{
get
{
return _description;
}

set
{
_description = value;
}
}

public bool GeneratesDesignTimeSource
{
get
{
return _generatesDesignTimeSource;
}

set
{
_generatesDesignTimeSource = value;
}
}

private string RegistrationKey
{
get
{
return string.Format(@"Generators\{0}\{1}", LanguageServiceGuid.ToString("B"), Name);
}
}

public override void Register(RegistrationContext context)
{
using (Key key = context.CreateKey(RegistrationKey))
{
if (!string.IsNullOrEmpty(Description))
key.SetValue(string.Empty, Description);
key.SetValue("CLSID", GeneratorType.GUID.ToString("B"));
key.SetValue("GeneratesDesignTimeSource", GeneratesDesignTimeSource ? 1 : 0);
}
}

public override void Unregister(RegistrationContext context)
{
context.RemoveKey(RegistrationKey);
}
}

附录 B:ProvideAssemblyObjectAttribute.cs

免责声明:此代码完全未经测试。

using System;
using Microsoft.VisualStudio.Shell;

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public sealed class ProvideAssemblyObjectAttribute : RegistrationAttribute
{
private readonly Type _objectType;
private RegistrationMethod _registrationMethod;

public ProvideAssemblyObjectAttribute(Type objectType)
{
if (objectType == null)
throw new ArgumentNullException("objectType");

_objectType = objectType;
}

public Type ObjectType
{
get
{
return _objectType;
}
}

public RegistrationMethod RegistrationMethod
{
get
{
return _registrationMethod;
}

set
{
_registrationMethod = value;
}
}

private string ClsidRegKey
{
get
{
return string.Format(@"CLSID\{0}", ObjectType.GUID.ToString("B"));
}
}

public override void Register(RegistrationContext context)
{
using (Key key = context.CreateKey(ClsidRegKey))
{
key.SetValue(string.Empty, ObjectType.FullName);
key.SetValue("InprocServer32", context.InprocServerPath);
key.SetValue("Class", ObjectType.FullName);
if (context.RegistrationMethod != RegistrationMethod.Default)
_registrationMethod = context.RegistrationMethod;

switch (RegistrationMethod)
{
case Microsoft.VisualStudio.Shell.RegistrationMethod.Default:
case Microsoft.VisualStudio.Shell.RegistrationMethod.Assembly:
key.SetValue("Assembly", ObjectType.Assembly.FullName);
break;

case Microsoft.VisualStudio.Shell.RegistrationMethod.CodeBase:
key.SetValue("CodeBase", context.CodeBase);
break;

default:
throw new InvalidOperationException();
}

key.SetValue("ThreadingModel", "Both");
}
}

public override void Unregister(RegistrationContext context)
{
context.RemoveKey(ClsidRegKey);
}
}

关于visual-studio-2013 - 如何让 VSIX 安装程序自动注册程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22634899/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com