Exception when registering property class with observablecollection

Mar 3, 2015 at 7:05 PM
Hello all.

I have this class:
public class ScoreSettings : XmlFileBase
    {
        private ObservableCollection<Team> _teams = new ObservableCollection<Team>();

        public ObservableCollection<Team> Teams
        {
            get { return _teams; }
            set
            {
                if (Equals(value, _teams)) return;
                _teams = value;
                OnPropertyChanged();
            }
        }

        public ScoreSettings()
        {
            _teams = new ObservableCollection<Team>();
        }
    }
XmlFileBase is a custom class that basically implements INotifyPropertyChanged.

When i register this to the SimpleInjector container as singleton like so:
container.RegisterSingle<ScoreSettings>();
I get the following exception:

The configuration is invalid. Creating the instance for type MainViewModel failed. The registered delegate for type MainViewModel threw an exception. The registered delegate for type ScoreSettings threw an exception. Parameter count mismatch.

Where MainViewModel gets ScoreSettings injected.

If i change
private ObservableCollection<Team> _teams = new ObservableCollection<Team>();
to
private ObservableCollection<Team> _teams;
it does work. Whats going on here?
Coordinator
Mar 3, 2015 at 7:50 PM
I'm unable to reproduce this issue. Would you be able to supply me with the minimal code that reproduces the issue? For instance by supplying me with a small console application or solution that demonstrates this issue?
Mar 4, 2015 at 8:05 AM
Hey dot_NET_Junkie, thanks for having a look for me.

I created a minimal example that throws the exception. You can get the entire solution here: https://www.mediafire.com/?9109iz9fo8i019d

It seems to be related tot he XmlFileBase class I am using. This is a class from the XmlConfigLib.Core NuGet package which i published myself aswell. You can find its source on bitbucket: https://bitbucket.org/MJLHThomassen/xmlconfiglib

The classes that XmlFileBase inherits from themself (PropertyChangedTracker and PropertyChangedChecker) take no arguments for their constructors. I can't see what it is that I'm doing wrong here. The exception thrown by Simple Injector also doesn't tell me much about what is exactly is that goes wrong.

Regards,
Maarten
Coordinator
Mar 4, 2015 at 7:12 PM
Hi Maarten,

I've downloaded your solution and the application is thrown on my machine as well. But the issue unrelated to Simple Injector, because even if I remove Simple Injector from the project, even a simple call to the constructor of Settings throws the exact same exception. The following code reproduces the issue:
static void Main(string[] args)
{
    new Settings();
}
If you take a look at the stack trace you will see that the exception is coming from deep within your own library:
   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at System.Reflection.PropertyInfo.GetValue(Object obj)
   at XmlConfigLib.Core.PropertyChangedChecker.RegisterINotifyPropertyChangedProperty(Object parent, PropertyInfo property)
   at XmlConfigLib.Core.PropertyChangedChecker.RegisterINotifyPropertyChangedProperty(Object parent, PropertyInfo property)
   at XmlConfigLib.Core.PropertyChangedChecker..ctor()
   at XmlConfigLib.Core.PropertyChangedTracker..ctor()
   at XmlConfigLib.Core.XmlFileBase..ctor()
   at SimpleInjectorExceptionDemo.Settings..ctor()
   at SimpleInjectorExceptionDemo.Program.Main(String[] args)
I'm afraid I will be unable to assist you any further with this. Good look tracking down the problem.