Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...