在 .NET Core 中使用反射 [英] Using Reflection in .NET Core

查看:40
本文介绍了在 .NET Core 中使用反射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于跨平台开发,我正在尝试制作一个 .NET Core 共享库.我在 VS 2015 中使用了 Class Library (package) 项目模板.我的库需要使用一些我从完整的 .net 4 框架中熟悉的反射机制,但我现在不知道如何在 .NET Core 库中访问这些.具体:

  • Delegate 类型具有返回 MethodInfo 对象的 Method 属性.
  • Type 类具有 BaseType 属性、FilterName 属性、InvokeMember 方法和 FindMembers 我在 .NET Core 中无法访问的方法.

我添加了据称具有我需要的反射部分的 NuGet 包:

框架":{net451":{依赖关系":{"System.Reflection": "4.1.0-beta-23516","System.Reflection.Extensions": "4.0.1-beta-23516","System.Reflection.Primitives": "4.0.1-beta-23516",}},dotnet5.4":{依赖关系":{"Microsoft.CSharp": "4.0.1-beta-23516","System.Collections": "4.0.11-beta-23516","System.Linq": "4.0.1-beta-23516","System.Reflection": "4.1.0-beta-23516","System.Reflection.Extensions": "4.0.1-beta-23516","System.Reflection.Primitives": "4.0.1-beta-23516","System.Runtime": "4.0.21-beta-23516",System.Threading":4.0.11-beta-23516"}}},依赖关系":{System.Reflection.TypeExtensions":4.1.0-beta-23516"}

我还添加了 using System.Reflection,但我仍然收到表明这些属性和类型未定义的错误.

我做错了什么?

如果相关,在同一台机器上,命令 dnvm list 显示:

活动版本运行时架构操作系统别名------ ------- ------- ------------ --------------- ---——1.0.0-rc1-update1 clr x64 获胜1.0.0-rc1-update1 clr x86 胜利1.0.0-rc1-update1 coreclr x64 获胜* 1.0.0-rc1-update1 coreclr x86 win 默认

以上正是我想要的……或者至少是我认为我想要的.;)

解决方案

简答

<块引用>

我做错了什么?

您正在尝试访问在 .NET 4.5.1 中可用但在 5.4 中不可用的成员.

5.x/Core 中的 4.x 解决方法委托.方法.委托.GetMethodInfo()类型.基本类型.Type.GetTypeInfo()Type.FilterName -Type.InvokeMember -Type.FindMembers -

直接从 Visual Studio 剪辑.

如果我们将鼠标悬停在错误上,Visual Studio 会告诉我们这一点.

.NET 可移植性报告

也值得一看

For cross-platform development, I'm trying to make a .NET Core shared library. I used the Class Library (package) project template in VS 2015. My library needs to use a couple reflection mechanisms I am familiar with from the full .net 4 framework, but I don't now how to access these in a .NET Core library. Specifically:

  • The Delegate type has a Method property that returns a MethodInfo object.
  • The Type class has a BaseType property, FilterName property, InvokeMember method and FindMembers method I can't reach in .NET Core.

I added NuGet packages that allegedly have the reflection pieces I need:

"frameworks": {
  "net451": {
    "dependencies": {
      "System.Reflection": "4.1.0-beta-23516",
      "System.Reflection.Extensions": "4.0.1-beta-23516",
      "System.Reflection.Primitives": "4.0.1-beta-23516",
    }
  },
  "dotnet5.4": {
    "dependencies": {
      "Microsoft.CSharp": "4.0.1-beta-23516",
      "System.Collections": "4.0.11-beta-23516",
      "System.Linq": "4.0.1-beta-23516",
      "System.Reflection": "4.1.0-beta-23516",
      "System.Reflection.Extensions": "4.0.1-beta-23516",
      "System.Reflection.Primitives": "4.0.1-beta-23516",
      "System.Runtime": "4.0.21-beta-23516",
      "System.Threading": "4.0.11-beta-23516"
    }
  }
},
"dependencies": {
  "System.Reflection.TypeExtensions": "4.1.0-beta-23516"
}

I've also added using System.Reflection, but I still am getting errors that indicate that these properties and types are undefined.

What am I doing wrong?

In case it is relevant, on this same machine the command dnvm list shows:

Active Version           Runtime Architecture OperatingSystem Alias  
------ -------           ------- ------------ --------------- -----  
    1.0.0-rc1-update1 clr     x64          win                    
    1.0.0-rc1-update1 clr     x86          win                    
    1.0.0-rc1-update1 coreclr x64          win                    
*    1.0.0-rc1-update1 coreclr x86          win             default

The above is exactly what I want...or at least what I think I want. ;)

解决方案

Short Answer

What am I doing wrong?

You are trying to access members that are available in .NET 4.5.1 but not in 5.4.

4.x                        Workaround in 5.x/Core

Delegate.Method.           Delegate.GetMethodInfo()
Type.BaseType.             Type.GetTypeInfo()
Type.FilterName            -
Type.InvokeMember          -
Type.FindMembers           -

Snip directly from Visual Studio.

Visual Studio tells us this if we hover our mouse over the error.

.NET Portability Report

It is also worth looking at the .NET Portability Analyzer. It is an extension that we can install from the Visual Studio Gallery.

Running it tells us, for instance, that Type.BaseType is not available and recommends a workaround.

这篇关于在 .NET Core 中使用反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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