问题描述
我有一个 .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 errorGenericParameterAttributes = '((System.RuntimeType)type).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
有什么办法可以让它发挥作用吗?
Is there any way I can get this to work?
更新删除了所有 Template10 的东西,因为它往往会掩盖真正的错误,并尝试了一个空白的 UWP 应用程序.得到这个异常
UPDATERemoved 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)
推荐答案
Form 官方文档,当您以应用或库中的框架为目标时,您指定了一组您希望应用或库可用的 API.您可以使用目标框架名称 (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 逻辑的预处理器符号有条件地针对这些程序集进行编译
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
在 UWP 中不支持.总之,apis必须对平台可用.
ProcessHandler
does not support in the UWP. In summary,the apis must be available to platform.
这篇关于从 UWP 应用调用 .NET Standard 库中的 Process.GetProcesses()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!