在WinRT中,静态字段根本没有初始化

img*_*gen 9 c# windows-8 windows-runtime

我有这样的代码

public class SomeClass
{
    private static int staticField = 10;
}
Run Code Online (Sandbox Code Playgroud)

代码永远不会被执行,而staticField的默认值为0.此外,代码会导致MVVMlight的SimpleIoc抛出异常,代码如下:

SimpleIoc.Default.Register<SomeClass>();
Run Code Online (Sandbox Code Playgroud)

上面的代码导致MVVMLight抛出异常说

 Cannot build instance: Multiple constructors found but none marked with PreferredConstructor.
Run Code Online (Sandbox Code Playgroud)

这很离奇.我正在使用适用于Windows 8的Win8 RTM x64 + VS2012 Express.

Jeh*_*hof 12

这绝对是MVVMLight的SimpleIoc中的一个错误.我已经尝试使用LinqPad,问题是当你向类添加一个静态字段时,字段初始化程序会添加一个静态字符.

结果是SomeClass类有两个用于SimpleIoc的ctors,导致你描述的异常.

解决方法是将默认构造函数添加到类中并使用它进行修饰,PreferredConstructorAttribute但这会导致对SimpleIoc的依赖.

其他解决方案是将静态字段更改为常量值.

public class SomeClass
{
    private const int staticField = 10;
}
Run Code Online (Sandbox Code Playgroud)

或者使用Register方法的重载来提供实例创建的工厂方法.

SimpleIoc.Default.Register<SomeClass>(() => new SomeClass())
Run Code Online (Sandbox Code Playgroud)

我已经在CodePlex上提交了关于MVVM Light项目的错误报告

LinqPad(测试代码):

    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
Run Code Online (Sandbox Code Playgroud)

问题是这条线

var constructorInfos = resolveTo.GetTypeInfo().DeclaredConstructors.ToArray();
Run Code Online (Sandbox Code Playgroud)

返回一个带有2个ConstructorInfos的数组,这两个数组都是在没有PreferredConstructorAttribute的情况下定义的,这会导致异常.

  • 它是*字段初始值设定项*,而不是字段,它导致添加静态构造函数.有趣的是,MVVMLight认为它是一个实例构造函数 (5认同)