Failed to create Type.GetType (string TypeName) method and MemberInfo.CustomAttributes property available in DNX Core 5.0

I'm trying to get controller attributes and controller methods, but I can't seem to imagine the required methods and properties available in DNX Core 5.0. I want to get what roles a user should get to access a given method. For example, if I have this

 [Authorize(Roles = "Admin")]
 public class UserController : Controller
 {
    public IActionResult Index()
    {
        return View();
    }
}

      

I want the user to be able to get the admin role in order to have access to this controller. My project.json file for dnxcore50 looks like this:

"dnxcore50": {
  "dependencies": {
    "System.Collections": "4.0.10-beta-*",
    "System.Collections.Concurrent": "4.0.10-beta-*",
    "System.IO": "4.0.10-beta-*",
    "System.Linq": "4.0.0-beta-*",
    "System.Linq.Expressions": "4.0.10-beta-*",
    "System.Reflection": "4.0.10-beta-*",
    "System.Reflection.Emit": "4.0.0-beta-*",
    "System.Reflection.Emit.Lightweight": "4.0.0-beta-*",
    "System.Reflection.Extensions": "4.0.0-beta-*",
    "System.Reflection.TypeExtensions": "4.0.0-beta-*",
    "System.Reflection.Primitives": "4.0.0-beta-*",
    "System.Runtime": "4.0.20-*",
    "System.Runtime.Extensions": "4.0.10-beta-*",
    "System.Runtime.CompilerServices.VisualC": "4.0.0-beta-*",
    "System.Runtime.InteropServices": "4.0.20-beta-*",
    "System.Text.Encoding": "4.0.10-beta-*",
    "System.Text.RegularExpressions": "4.0.10-beta-*",
    "System.Threading": "4.0.10-beta-*"
  }
}    

      

Any ideas on how I would get the required methods / properties to access in DNX Core 5.0, or any other suggestions on how to approach this issue?

+3


source to share


1 answer


If you look at the github source code, you can see how it is parsed (line 94):

https://github.com/aspnet/Mvc/blob/eef6c3883a7e27b8387b0925f0b6a88df0a484c5/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Metadata/ModelAttributes.cs



As you can see, you must use GetTypeInfo () to use the GetCustomAttributes () method. In code, you can:

using System.Reflection;
...
obj.GetType().GetTypeInfo().GetCustomAttributes();

      

+3


source







All Articles