Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
138 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...