Calling Process.GetProcesses() inside a .NET Standard library from a UWP app

那年仲夏 提交于 2019-12-13 04:09:07

问题


I have a .NET standard library which is below:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace process_library
{
    public class ProcessHandler
    {
        public ProcessHandler()
        {

        }

        [MTAThread]
        public Process[] GetProcesses()
        {
            return Process.GetProcesses();
        }
    }
}

I then have a Template10 project with all the .net core stuff updated to the latest version (required to get it to build) and referenced my library. This all works so far.

Inside my main view-model I instantiate my library class

ProcessHandler p = new ProcessHandler();

This is also successfull.

My problem is when I call my get process method it pukes and throws an error GenericParameterAttributes = '((System.RuntimeType)type).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'

Is there any way I can get this to work?

UPDATE Removed all the Template10 stuff as it tends to mask the real errors and tried a blank UWP app. Get this exception

System.PlatformNotSupportedException
  HResult=0x80131539
  Message=Retrieving information about local processes is not supported on this platform.

What is the point of including it in .net standard (Which is supposed to contain a standard set of apis you can use) if you cant even use it

I should also note I'm targeting the very latest update (Spring 2018)


回答1:


Form official document, when you target a framework in an app or library, you're specifying the set of APIs that you'd like to make available to the app or library. You specify the target framework in your project file using Target Framework Monikers (TFMs).

When you specify multiple target frameworks, you may conditionally reference assemblies for each target framework. In your code, you can conditionally compile against those assemblies by using preprocessor symbols with if-then-else logic

public class MyClass
{
    static void Main()
    {
#if NET40
        Console.WriteLine("Target framework: .NET Framework 4.0");
#elif NET45  
        Console.WriteLine("Target framework: .NET Framework 4.5");
#else
        Console.WriteLine("Target framework: .NET Standard 1.4");
#endif
    }
}

ProcessHandler does not support in the UWP. In summary,the apis must be available to platform.



来源:https://stackoverflow.com/questions/50161323/calling-process-getprocesses-inside-a-net-standard-library-from-a-uwp-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!