Does anyone know how to implement the DynamicEnumP

2019-06-04 07:35发布

I am trying to add a property to our custom build configuration for a C++ project. I want the property combo box to display a dynamic list of values that I can set in code. I think that this should be done using the DynamicEnumProperty type but I am unsure of its implementation. Has anyone worked with this property before that can point me in the right direction?

Thanks

2条回答
祖国的老花朵
2楼-- · 2019-06-04 08:22

I know it's a bit old question... but you might still enjoy the solution ;)

Beside referencing the assemblies and exporting desired type via MEF as Dmitry explained above, you also need to mark the VSPackage as MEF-enabled to make it scan though your contracts. Do it by editing source.extension.vsixmanifest:

for VS2010:

<Content>
  <VsPackage>|%CurrentProject%;PkgdefProjectOutputGroup|</VsPackage>
  <MefComponent>|%CurrentProject%|</MefComponent>
</Content>

for VS2012 / VS2013:

<Assets>
  <Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project"
         d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
</Assets>

This should let you hit a breakpoint set in an exported class.


Additionally, if you need to create an object at runtime 'manually', you can use VisualStudio's internal composition container. The simplest way to access it from anywhere is:

var container = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel)) as IComponentModel;
var service = container.GetService<SVsXYZ>();

I will shortly add a sample here: https://github.com/phofman/vs-plugin, so just putting the link for future reference.

查看更多
3楼-- · 2019-06-04 08:38

In your VSPackage (or any MEF-exposed DLL referenced by it) create a class implementing IDynamicEnumValuesProvider and add [Export(typeof(IDynamicEnumValuesProvider)), DynamicEnumCategory("MyCategory")] to that class's attributes. Then add EnumProvider="MyCategory" to the DynamicEnumProperty definition and your class will be used as the values provider.

Make sure your package references Microsoft.VisualStudio.ProjectSystem.Utilities.v12.0.dll and Microsoft.VisualStudio.ProjectSystem.V12Only.dll (for VS2013) or similar assemblies for earlier versions.

查看更多
登录 后发表回答