Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Reflection in .NET Core

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. ;)

like image 729
Brent Arias Avatar asked Mar 20 '16 20:03

Brent Arias


People also ask

Does .NET core support reflection?

Yes, it is.

Is reflection bad for performance C#?

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.

What is reflection used for C#?

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.

Is .NET reflection slow?

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.


3 Answers

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.

ype.BaseType is not available...

.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 Portability Analyzer output

like image 172
Shaun Luttin Avatar answered Oct 24 '22 20:10

Shaun Luttin


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)

like image 36
Stef Heyenrath Avatar answered Oct 24 '22 22:10

Stef Heyenrath


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": { }
    }
}
like image 4
Saleem Avatar answered Oct 24 '22 21:10

Saleem