Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 无法使Type.GetType(字符串TypeName)方法和MemberInfo.CustomAttributes属性在DNX Core 5.0中可用_Asp.net Mvc_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

Asp.net mvc 无法使Type.GetType(字符串TypeName)方法和MemberInfo.CustomAttributes属性在DNX Core 5.0中可用

Asp.net mvc 无法使Type.GetType(字符串TypeName)方法和MemberInfo.CustomAttributes属性在DNX Core 5.0中可用,asp.net-mvc,asp.net-core,asp.net-core-mvc,Asp.net Mvc,Asp.net Core,Asp.net Core Mvc,我试图获取控制器的属性和控制器的方法,但我似乎无法在DNX Core 5.0中提供必要的方法和属性。我想获得用户访问给定方法所需的角色。例如,如果我有这个 [Authorize(Roles = "Admin")] public class UserController : Controller { public IActionResult Index() { return View(); } } 我希望能够得到用户需要管理员角色才能访问此控制器。d

我试图获取控制器的属性和控制器的方法,但我似乎无法在DNX Core 5.0中提供必要的方法和属性。我想获得用户访问给定方法所需的角色。例如,如果我有这个

 [Authorize(Roles = "Admin")]
 public class UserController : Controller
 {
    public IActionResult Index()
    {
        return View();
    }
}
我希望能够得到用户需要管理员角色才能访问此控制器。dnxcore50的My project.json文件如下所示:

"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-*"
  }
}    

关于如何在DNX Core 5.0中获得必要的方法/属性,或者对如何解决此问题有何其他建议?

如果您查看github上的源代码,您可以看到它是如何处理的(第94行):

如您所见,必须使用GetTypeInfo()来使用GetCustomAttributes()方法。在代码中,您可以执行以下操作:

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

您需要
System.Runtime
System.Reflection
包才能在DNX Core中使用这些方法,并且您已经在project.json中引用了它们。你会犯什么错误?