【问题标题】:Identify methodinfo that has HTTPPost() attribute识别具有 HTTPPost() 属性的方法信息
【发布时间】:2014-11-15 13:26:46
【问题描述】:

我目前正在使用以下内容从程序集中读取控制器

Public Shared Function GetControllers() As List(Of Type)
    ' Go through all assemblies referenced by the application and search for
    ' controllers and controller factories.

    Dim controllerTypes As New List(Of Type)()
    Dim Assemblies = AppDomain.CurrentDomain.GetAssemblies
    For Each assembly As Reflection.Assembly In Assemblies
        Dim typesInAsm As Type()
        Try
            typesInAsm = assembly.GetTypes()
        Catch ex As Reflection.ReflectionTypeLoadException
            typesInAsm = ex.Types
        End Try
        controllerTypes.AddRange(typesInAsm.Where(AddressOf IsControllerType))
    Next
    Return controllerTypes
End Function

然后我刚刚填充的列表用于读取所有操作,现在我正在尝试过滤掉具有 HTTPPost 的操作。

Public Shared Sub GetActionMethods(Controllers As List(Of Type))
    For Each y In Controllers
        Dim selector = New ActionMethodSelector(y)

        Dim allValidMethods As New List(Of Reflection.MethodInfo)()
        allValidMethods.AddRange(selector.AliasedMethods)
        allValidMethods.AddRange(selector.NonAliasedMethods.SelectMany(Function(g) g))

        Dim z = allValidMethods.ToArray()
    Next
End Sub

问题是我不知道如何设置通过 linq 执行此操作的位置。我尝试了各种方法,但总是得到一个空列表。

Dim k = (From o As Reflection.MethodInfo In allValidMethods
         Select o).ToArray

【问题讨论】:

    标签: .net asp.net-mvc vb.net linq asp.net-mvc-5


    【解决方案1】:

    我能够在另一个网站上找到我的解决方案,为了表示感谢,我将发布链接。

    Using LINQ with System.Reflection Classes

    我的代码在下面

    Dim k = (From o As Reflection.MethodInfo In allValidMethods
             Where o.GetCustomAttributes(GetType(HttpPostAttribute), True).Length > 0
             Select o).ToArray
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      相关资源
      最近更新 更多