Bre*_*ias 26 .net c# reflection .net-core asp.net-core
对于跨平台开发,我正在尝试创建.NET Core共享库.我Class Library (package)在VS 2015中使用了项目模板.我的库需要在完整的.net 4框架中使用我熟悉的一些反射机制,但我现在不知道如何在.NET Core库中访问它们.特别:
Delegate类型具有Method返回MethodInfo对象的属性.Type类有一个BaseType属性,FilterName属性,InvokeMember方法和FindMembers方法,我不能在.NET核心达到.我添加了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": {
"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"
}
Run Code Online (Sandbox Code Playgroud)
我也添加了using System.Reflection,但我仍然收到错误,表明这些属性和类型未定义.
我究竟做错了什么?
如果它是相关的,在同一台机器上命令dnvm list显示:
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
Run Code Online (Sandbox Code Playgroud)
以上就是我想要的......或者至少我认为我想要的东西.;)
Sha*_*tin 22
我究竟做错了什么?
您正在尝试访问.NET 4.5.1中可用但不在5.4中的成员.
4.x Workaround in 5.x/Core
Delegate.Method. Delegate.GetMethodInfo()
Type.BaseType. Type.GetTypeInfo()
Type.FilterName -
Type.InvokeMember -
Type.FindMembers -
Run Code Online (Sandbox Code Playgroud)
如果我们将鼠标悬停在错误上,Visual Studio会告诉我们.
还值得一看.NET可移植性分析器.它是我们可以从Visual Studio库安装的扩展.
例如,运行它告诉我们,这Type.BaseType是不可用的,并建议一种解决方法.
如果您不希望原始代码充满#if ... #else ... #endif语句,则可以使用https://www.nuget.org/packages/ReflectionBridge/等帮助程序库,它提供了一些扩展,这些扩展定义了Type和TypeInfo之间差异的桥梁.
(源代码https://github.com/StefH/ReflectionBridge)
| 归档时间: |
|
| 查看次数: |
29015 次 |
| 最近记录: |