Using Reflection in .NET Core

前端 未结 3 1649
悲&欢浪女
悲&欢浪女 2020-12-08 13:56

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 nee

相关标签:
3条回答
  • 2020-12-08 14:45

    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)

    0 讨论(0)
  • 2020-12-08 14:48

    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": { }
        }
    }
    
    0 讨论(0)
  • 2020-12-08 14:59

    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.

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

    0 讨论(0)
提交回复
热议问题