30

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

3
  • .net Core is still under development and is not complete. It's possible System.Reflection is not ported into .net core yet. Commented Mar 20, 2016 at 20:28
  • 2
    At least for the BaseType you can use type.GetTypeInfo().BaseType, that's where it has been moved in since WinRT/UWP (which is roughly on what .NET Core is based on, the System.Runtime). Much of the standard calls have been removed behind GetTypeInfo() and similar methods Commented Mar 20, 2016 at 20:30
  • When are you getting errors? What are those errors? Commented Mar 22, 2016 at 2:57

3 Answers 3

25

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

Sign up to request clarification or add additional context in comments.

Comments

7

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)

Comments

4

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": { }
    }
}

3 Comments

NETStandard.Library is not available from NuGet; I am unable to use it. Where did you get it from? Evidently, it is not in a published location. Also, what exactly is NETStandard.Library? I didn't find any articles or documentation written about it.
I ran dnvm restore a couple weeks before creating this project. I've updated my question to show dnvm list results on my machine. Are you saying this is still not good enough?
Please see your project.json file. It's referencing beta assemblies. e.g. "System.Reflection": "4.1.0-beta-23516". Please update version to non-beta and restore again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.