Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:03:45+00:00 2026-06-14T01:03:45+00:00

I have the code like this public class SomeClass { private static int staticField

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:03:47+00:00Added an answer on June 14, 2026 at 1:03 am

    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 PreferredConstructorAttribute but this will result in a dependency to SimpleIoc.

    Other solution would be to change your static field to a constant value.

    public class SomeClass
    {
        private const int staticField = 10;
    }
    

    or to use an overload of the Register method to provide a factory method for instance creation.

    SimpleIoc.Default.Register<SomeClass>(() => new SomeClass())
    

    I have submitted a bug report on the MVVM Light project on CodePlex

    LinqPad (Test code):

        void Main()
    {
        var x = GetConstructorInfo(typeof(SomeClass));
    
        x.Dump();
        x.IsStatic.Dump();
    }
    
    
    public class PreferredConstructorAttribute : Attribute{
    
    }
    public class SomeClass{
      private static int staticField = 10;
    
    }
    
    private ConstructorInfo GetConstructorInfo(Type serviceType)
            {
                Type resolveTo = serviceType;
    
    
    //#if NETFX_CORE
                var constructorInfos = resolveTo.GetTypeInfo().DeclaredConstructors.ToArray();
                constructorInfos.Dump();
    //#else
    //          var constructorInfos = resolveTo.GetConstructors();
    //constructorInfos.Dump();
    //#endif
    
                if (constructorInfos.Length > 1)
                {
                    var preferredConstructorInfos
                        = from t in constructorInfos
    //#if NETFX_CORE
                           let attribute = t.GetCustomAttribute(typeof (PreferredConstructorAttribute))
    //#else
    //                     let attribute = Attribute.GetCustomAttribute(t, typeof(PreferredConstructorAttribute))
    //#endif
                           where attribute != null
                           select t;
    
    preferredConstructorInfos.Dump();
    
    var preferredConstructorInfo = preferredConstructorInfos.FirstOrDefault ( );
                    if (preferredConstructorInfo == null)
                    {
                        throw new InvalidOperationException(
                            "Cannot build instance: Multiple constructors found but none marked with PreferredConstructor.");
                    }
    
                    return preferredConstructorInfo;
                }
    
                return constructorInfos[0];
            }
    // Define other methods and classes here
    

    The problem is the line

    var constructorInfos = resolveTo.GetTypeInfo().DeclaredConstructors.ToArray();
    

    that returns an array with 2 ConstructorInfos both defined without the PreferredConstructorAttribute which results in the exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that follows this structure: class someClass { private static $pay =
I have code that looks like this: template<class T> class list { public: class
my code is like this: i have two classes first class: public class Box<E>
I have a similar code snippet like this class Search { public function search($for,
I have a code like this: class PacketDAO{ //... public void UpdatePacketStatus(Guid packetID, Status
Say, I have a code snippet like this: public static void main(String[] args) {
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have code like this: public class test { public string aa { get;
I have a class like this: class SomeClass { public: QString data; SomeClass(const QString
I currently have a class something like this [DataContractAttribute] public class someClass { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.