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:
Delegate
type has a Method
property that returns a MethodInfo
object.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. ;)
Yes, it is.
The use of reflection is not recommended due to its bad performance because of all the security checks being done when calling a method or iterating through an object's members.
Reflection in C# is used to retrieve metadata on types at runtime. In other words, you can use reflection to inspect metadata of the types in your program dynamically -- you can retrieve information on the loaded assemblies and the types defined in them.
It's common knowledge that reflection in . NET is slow, but why is that the case? This post aims to figure that out by looking at what reflection does under-the-hood.
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 -
Visual Studio tells us this if we hover our mouse over the error.
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.
If you don't want your original code full of #if ... #else ... #endif
statements, you could use a helper library like https://www.nuget.org/packages/ReflectionBridge/ which provides some extensions which define a bridge for the differences between Type and TypeInfo.
(Source code at https://github.com/StefH/ReflectionBridge)
I'm using .net Core 1.0. Try following snippet of project.json and see if works for you. I have also noticed that you are using beta API so if possible stay way from beta.
{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},
"dependencies": {
"NETStandard.Library": "1.0.0-rc2-23811",
"System.Reflection.TypeExtensions": "4.0.0"
},
"frameworks": {
"dnxcore50": { }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With