开发者

Convert method parameters into array

Is there a Visual Basic.NET method that can convert method parameters into an array?

For instance instead of:

Function functionName(p开发者_开发知识库aram1 as object, param2 as object) as object
ArrayName = {param1, param2}

you could do something like:

Function functionName(param1 as object, param2 as object) as object
ArrayName = MethodThatGetsAllFunctionParams

Just curious really.


There is no way of doing that. The language itself doesn’t allow that. You can use reflection to get the currently executing method of the StackFrame of the execution. But even then it’s still impossible to retrieve the parameter values.

The only solution is to “patch” the applications by introducing point cuts into the method call. The linked answer mentions a possibility for that.


Take a look at ParamArrays. I think this solves what you're asking for?

http://msdn.microsoft.com/en-us/library/538f81ec%28v=VS.100%29.aspx

EDIT:

You could initialise a custom collection using your current function signature

Public Class CustomCollection(Of T)
    Inherits System.Collections.Generic.List(Of T)

    Sub New(param1 As T, param2 As T)
        MyBase.New()
        MyBase.Add(param1)
        MyBase.Add(param2)
    End Sub
End Class

and then call the function using

Dim result = functionName(New CustomCollection(Of Object)(param1, param2))

The Function signature would be changed to:

Public Function functionName(ByVal args As CustomCollection(Of Object)) As String
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜