在.NET Core中使用反射 [英] Using Reflection in .NET Core
问题描述
对于跨平台开发,我正在尝试制作.NET Core共享库。我在VS 2015中使用了 Class Library(程序包)
项目模板。我的库需要使用完整的.net 4框架熟悉的几种反射机制,但我现在,不要再在.NET Core库中访问它们了。具体来说:
-
Delegate
类型具有Method
属性,该属性返回MethodInfo
对象。 -
Type
类具有BaseType
属性,FilterName
属性,InvokeMember
方法和FindMembers
方法在.NET Core中无法到达。
我添加了据称具有我需要的反射部件的NuGet软件包:
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:{
依赖项:{
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-bet a-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
}
我还使用System.Reflection 添加了,但仍然出现错误,表明这些属性和类型是不确定的。
我在做什么错了?
如果相关,请在同一台计算机上使用命令 dnvm列表
显示:
活动版本运行时体系结构操作系统别名
- ----- ------- ------- ------------ --------------- ---- -
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 default
以上正是我想要的...至少我想我想要的。 ;)
简短回答
我在做什么错了?
您正试图访问.NET 4.5.1中可用的成员,但不能访问5.4中的成员。
4.x在5.x / Core
Delegate.Method中的解决方法。 Delegate.GetMethodInfo()
Type.BaseType。 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 aMethod
property that returns aMethodInfo
object. - The
Type
class has aBaseType
property,FilterName
property,InvokeMember
method andFindMembers
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屋!