I have the code like this
public class SomeClass
{
private static int staticField = 10;
}
The code never gets executed and staticField has default value 0.
Also the code causes MVVMlight’s SimpleIoc to throw an exception with code like this:
SimpleIoc.Default.Register<SomeClass>();
Above code causes MVVMLight to throw an exception saying
Cannot build instance: Multiple constructors found but none marked with PreferredConstructor.
This very bizarre.
I’m using Win8 RTM x64 + VS2012 Express for Windows 8.
This is definitely a bug in SimpleIoc of MVVMLight. I have tried it with LinqPad and the problem is when you add a static field to the class an static ctor is added by the field initializer.
The result is that the class SomeClass has two ctors for SimpleIoc, what results in the exception you described.
A Workaround would be to add a default constructor to the class and decorate it with the
PreferredConstructorAttributebut this will result in a dependency to SimpleIoc.Other solution would be to change your static field to a constant value.
or to use an overload of the Register method to provide a factory method for instance creation.
I have submitted a bug report on the MVVM Light project on CodePlex
LinqPad (Test code):
The problem is the line
that returns an array with 2 ConstructorInfos both defined without the PreferredConstructorAttribute which results in the exception.