从UWP应用程序调用.NET标准库中的Process.GetProcesses() [英] Calling Process.GetProcesses() inside a .NET Standard library from a UWP app

查看:49
本文介绍了从UWP应用程序调用.NET标准库中的Process.GetProcesses()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET标准库,位于下面:

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();
        }
    }
}

然后,我有了一个Template10项目,所有.net核心内容都更新为最新版本(需要构建该版本),并引用了我的库.到目前为止,所有这些都有效.

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();

这也成功.

我的问题是,当我调用get处理方法时,它会呕吐并引发错误GenericParameterAttributes ='(((System.RuntimeType)type).GenericParameterAttributes'引发了类型'System.InvalidOperationException'的异常

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?

更新删除了所有Template10内容,因为它倾向于掩盖实际错误,并尝试了一个空白的UWP应用.得到这个例外

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.

如果您甚至无法使用它,将其包含在.net标准(应该包含一组可以使用的标准api)中的意义是什么

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

我还应该注意,我的目标是最新的更新(2018年春季)

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

推荐答案

表单官方文档,当您在应用程序或库中定位框架时,就是要指定要可用于该应用程序或库的API集.您可以使用Target Framework Monikers(TFM)在项目文件中指定目标框架.

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).

当您指定多个目标框架时,可以有条件地为每个目标框架引用程序集.在您的代码中,可以通过使用带有if-then-else逻辑的预处理器符号来有条件地针对这些程序集进行编译

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
    }
}

UWP不支持

ProcessHandler .总之,API必须可用于平台.

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

这篇关于从UWP应用程序调用.NET标准库中的Process.GetProcesses()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆