Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best way to detect target framework(s) in a CPS project #430

Closed
ErikEJ opened this issue May 11, 2023 · 4 comments
Closed

Best way to detect target framework(s) in a CPS project #430

ErikEJ opened this issue May 11, 2023 · 4 comments

Comments

@ErikEJ
Copy link

ErikEJ commented May 11, 2023

I tried:

await project.GetAttributeAsync("TargetFrameworkMoniker");

But it breaks with:

<TargetFrameworks>netstandard2.1;net6.0</TargetFrameworks>

(Only the first one is returned)

"TargetFrameworkMonikers" retrurns an empty string

@ErikEJ ErikEJ changed the title Best way to detect target framework Best way to detect target framework in a CPS project May 11, 2023
@ErikEJ ErikEJ changed the title Best way to detect target framework in a CPS project Best way to detect target framework(s) in a CPS project May 11, 2023
@MagicAndre1981
Copy link

GetProjectFrameworksAsync from public interface IActiveDebugFrameworkServices, but getting the nugets is difficult you need to use a custom nuget.config with sources for vs-impl source because the interface is not included in the latest version on nuget.org.

But for what ever reasons, getting the nugets doesn't work for all users all time.

@ErikEJ
Copy link
Author

ErikEJ commented May 18, 2023

But for what ever reasons, getting the nugets doesn't work for all users all time.

They look too internal/wip to me. Hoping a more general solution from @reduckted perhaps? 😄

@MagicAndre1981
Copy link

Microsoft has some plans to maybe publish them on nuget when a new VS 2022 update is released. As you can see this is a long open issue and it can still take some time to get it solved

@ErikEJ
Copy link
Author

ErikEJ commented Jun 11, 2023

I ended up doing this:

        private static async Task<string> GetTargetFrameworkMonikersAsync(Project project)
        {
            string result = null;

            project.GetItemInfo(out IVsHierarchy hierarchy, out uint itemId, out _);

            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            try
            {
                var vsProject = (IVsProject)hierarchy;

                if (vsProject != null)
                {
                    var props = vsProject.ToProject().Properties;
                    result = props.Item("TargetFrameworkMonikers")?.Value?.ToString();
                }
            }
            catch
            {
                // Ignore
            }

            if (string.IsNullOrEmpty(result))
            {
                result = await project.GetAttributeAsync("TargetFrameworkMoniker");
            }

            return result;
        }

        private static EnvDTE.Project ToProject(this IVsProject vsProject)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            var vsHierarchy = (IVsHierarchy)vsProject;
            var hr = vsHierarchy.GetProperty(
                (uint)VSConstants.VSITEMID.Root,
                (int)__VSHPROPID.VSHPROPID_ExtObject,
                out var project);
            Marshal.ThrowExceptionForHR(hr);

            return (EnvDTE.Project)project;
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants