标识具有HTTPPost()属性的methodinfo

时间:2014-09-21 13:08:55

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

我正在使用下面的

从程序集中读取控制器
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

1 个答案:

答案 0 :(得分:0)

我能够在另一个网站上找到我的解决方案,并且我将发布链接。

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