Constructor called 10 times when converting
See original GitHub issueWhy this code:
public class Program
{
static int Main(string[] args)
{
var verbs = typeof(Program).Assembly.DefinedTypes
.Where(t => t.CustomAttributes.Any(a => a.AttributeType == typeof(VerbAttribute)))
.Select(ti => ti.AsType())
.ToArray();
var resultCode = Parser.Default.ParseArguments(args, verbs).MapResult(
(IVerb verb) => verb.Execute(),
errs => 1
);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
return resultCode;
}
}
[Verb("testWeeklySchedule", HelpText = "Test using some fake data.")]
internal class TestWeeklySchedule : IVerb
{
public TestWeeklySchedule(TimeOfDay startTime, IEnumerable<DayOfWeek> daysOfWeek)
{
//StartTime = TimeSpan.Parse(startTime);
StartTime = startTime;
DaysOfWeek = daysOfWeek;
}
[Option(HelpText = "Time to start event.")]
public TimeOfDay StartTime { get; }
[Option(Required = true, HelpText = "Days to execute event.", Min = 1)]
public IEnumerable<DayOfWeek> DaysOfWeek { get; }
public int Execute()
{
Console.WriteLine("Hello world with schedule " + JsonConvert.SerializeObject(this));
return 0;
}
}
internal class TimeOfDay
{
public static int timesCalled = 0;
public TimeOfDay(string timeAsString)
{
timesCalled++;
Console.WriteLine("Constructor called with " + timeAsString);
Time = TimeSpan.Parse(timeAsString);
}
public TimeSpan Time { get; }
}
Calls the constructor 10 times?!?!?!?!?!?!?! The output is
Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Hello world with schedule {“StartTime”:{“Time”:“00:00:00”},“DaysOfWeek”:[1]} Press any key to exit…
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Constructors, C++ FAQ
Which constructor gets called when I create an array of Fred objects? ... int main(); {; Fred a[10]; // Calls the default constructor...
Read more >What is conversion constructor in C++?
A conversion constructor is a single-parameter constructor that is declared without the function specifier explicitly.
Read more >c++ - Number of times a constructor is called
In this case as well, I thought 2 constructor calls should be made - converting constructor call for CustomStr("Some char pointer") and copy ......
Read more >Conversion constructors (C++ only)
A conversion constructor is a single-parameter constructor that is declared without the function specifier explicit . The compiler uses conversion ...
Read more >9.6. Constructors And Initializer Lists
Programs call a constructor whenever they instantiate an object. ... This constructor converts an integer into an instance of Time (i.e., into a...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Tested with v2.4.3+ and Constructor is called only once.
Sure, it is just what we are doing, just simple and quick converters that do not cause trouble if they are called some extra times and just at the beginning of the app, however, is nice to see the root of the problem, i could not make the time to check the source so thanks a lot for the clarification.
I know is too radical to change the code to avoid this, but maybe a note in the documentation to warn the newcomers to be careful about this could be great.