0

我使用mvvm light工具包,但simpleIOC. 我想解析一个接口,但是类的构造函数有两个,simpleIOC不知道哪个是默认的。如何签署类的默认构造函数?

//ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            ////if (ViewModelBase.IsInDesignModeStatic)
            ////{
            ////    // Create design time view services and models
            ////    SimpleIoc.Default.Register<IDataService, DesignDataService>();
            ////}
            ////else
            ////{
            ////    // Create run time view services and models
            ////    SimpleIoc.Default.Register<IDataService, DataService>();
            ////}

这是ninject,如您所见,有2个cunstructor,我想使用第一个所以我必须签名,因为有2个构造函数,ioc不知道,必须注入。我想使用 mvvm light 工具包中的 SimpleIOC 来执行此操作,因为现在我使用 Ninject IOC

 [Inject]
        public RepositoryFactories()
        {
            repositoryFactories = GetSpecFactories();
        }

        public RepositoryFactories(IDictionary<Type, Func<DbContext, object>> factories)
        {
            repositoryFactories = factories;
        }
4

1 回答 1

4

尝试[PreferredConstructor]而不是[Inject]

根据MVVM Light Toolkit源代码中的文档,这正是您正在寻找的:

/// <summary>
/// When used with the SimpleIoc container, specifies which constructor
/// should be used to instantiate when GetInstance is called.
/// If there is only one constructor in the class, this attribute is
/// not needed.
/// </summary>
//// [ClassInfo(typeof(SimpleIoc))]
[AttributeUsage(AttributeTargets.Constructor)]
public sealed class PreferredConstructorAttribute : Attribute
{
}

PS。看起来 CodePlex 更喜欢带有实际空格和括号的链接,而不是特殊的 HTML 字符(如 %20 和 %28),因此指向原始源文件的链接有点不完整。无论如何,您可以在此位置找到文件:

https://mvvmlight.codeplex.com/SourceControl/latest#GalaSoft.MvvmLight/GalaSoft.MvvmLight.Extras (NET35)/Ioc/PreferredConstructor.cs
于 2013-05-29T15:34:11.367 回答