1

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

Community
  • 1
  • 1
CrazyDart
  • 3,803
  • 2
  • 23
  • 29
  • 1
    I believe you mean "optional" and not "named." http://msdn.microsoft.com/en-us/library/dd264739.aspx – Tom Jun 12 '12 at 21:22
  • Are you talking about attribute properties? – SLaks Jun 12 '12 at 21:23
  • 2
    Can you give an example of (the code of) a constructor for which `constructorInfo.GetParameters()` does not give you the information you want? – Jeppe Stig Nielsen Jun 12 '12 at 21:25
  • http://jonfuller.codingtomusic.com/2010/06/04/teaching-structuremap-about-c-4-0-optional-parameters-and-default-values/ – Tom Jun 12 '12 at 21:30
  • yes optional, not named sorry. – CrazyDart Jun 12 '12 at 21:32
  • Perhaps this might be the same solution? http://stackoverflow.com/questions/2421994/invoking-methods-with-optional-parameters-through-reflection – Tom Jun 12 '12 at 21:36
  • Ummm, why the -1? Who ever did that should try to contribute... so far I dont see a good answer. I am looking at Tom's link now... – CrazyDart Jun 12 '12 at 21:36
  • Is this how your "bad" constructors look: `public MyClass(int required, int optionalInt = 10) { ... }` The parameter `optionalInt` is "optional" because it says `= 10`. Or are you in fact searching for attributes? – Jeppe Stig Nielsen Jun 12 '12 at 21:44

2 Answers2

1

Possible duplicate. It appears that you can call the parameter, but have to set the values manually.

I found a similar problem here:

Invoking methods with optional parameters through reflection

Community
  • 1
  • 1
Tom
  • 1,330
  • 11
  • 21
  • Tom, thanks for the links. It turned out that I was Reflecting over 2 classes named the same thing, one had them and one didnt... I didnt write all this code, so I had no clue this could have been the case. The optional params do come back, and they do have an attribute that marks them. Again, thanks. – CrazyDart Jun 12 '12 at 21:44
0

See here, it works:

var parameterName =
    typeof(Foo)
    .GetConstructor(new[] { typeof(string) })
    .GetParameters()
    .Single().Name;

public class Foo
{
    public Foo(string paramName)
    {   
    }
}
Matteo Migliore
  • 925
  • 8
  • 22