'Find if method has optional parameters using System.Reflection in VB.Net
I am trying to find a way to know if there is an optional parameter in a method being called and its data types, by using Reflection in VB.Net. Can someone please explain with an example?
Solution 1:[1]
This is really something that you could have figured out by reading the relevant documentation but, regardless, here's an example:
Module Module1
Sub Main()
Dim method = GetType(SomeClass).GetMethod(NameOf(SomeClass.DoSomething))
For Each parameter In method.GetParameters().Where(Function(pi) pi.IsOptional)
Console.WriteLine("{0} As {1}", parameter.Name, parameter.ParameterType)
Next
Stop
End Sub
End Module
Public Class SomeClass
Public Sub DoSomething(p1 As String, Optional p2 As Integer = 0, Optional p3 As Boolean = False)
End Sub
End Class
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user18387401 |
