Possible Duplicate:
Invoking methods with optional parameters through reflection
Name of the constructor arguments in c#
Right now I am constructing objects using reflection. I am using this to fill out API documentation. In many cases I want a non-default constructor, but sometimes they have optional parameters. These optional parameters need to be overridden with a new object other than the default. The problem is I cant figure out how to get them. The normal parameters are easy with constructorInfo.GetParameters(), however it seems the optional do not come back. Am I missing something here?
Sample code:
ConstructorInfo[] constructorInfoList = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
foreach (ConstructorInfo constructorInfo in constructorInfoList)
{
var parameters = constructorInfo.GetParameters();
if (parameters.Count() > 0)
{
Answer: It turns out they do come back... this was user error.
Sample:
void Main()
{
var ctors = typeof(Foo).GetConstructors();
foreach(var ctor in ctors)
{
foreach(var param in ctor.GetParameters())
{
Console.WriteLine("Name: {0} Optional: {1}", param.Name, param.IsOptional);
}
}
}
public class Foo
{
public Foo(string option1, string option2 = "")
{
}
}
Output:
Name: option1 Optional: False Name: option2 Optional: True