gpt4 book ai didi

dependency-injection - Windows Phone 7 的依赖注入(inject)

转载 作者:行者123 更新时间:2023-12-04 01:50:13 26 4
gpt4 key购买 nike

我试图在我的 Windows Phone 7 项目中使用 Unity 2.0 beta 2 for Silverlight,但我一直遇到这个崩溃:

Microsoft.Practices.Unity.Silverlight.dll!Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.DynamicMethodConstructorStrategy() + 0x1f 字节

Microsoft.Practices.Unity.Silverlight.dll!Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.DynamicMethodConstructorStrategy() + 0x1f 字节 mscorlib.dll!System.Reflection.RuntimeConstructorInfo.InternalInvoke(System.Reflection.RuntimeConstructorInfo rtci = {System.Reflection.RuntimeConstructorInfo} , System.Reflection.BindingFlags invokeAttr = Default, System.Reflection.Binder binder = null, object parameters = {object[0]}, System.Globalization.CultureInfoculture = null, bool isBinderDefault = false, System.Reflection.Assembly caller = null, bool verifyAccess = true, ref System.Threading.StackCrawlMark stackMark = LookForMyCaller)
mscorlib.dll!System.Reflection.RuntimeConstructorInfo.InternalInvoke(object obj = null, System.Reflection.BindingFlags invokeAttr = Default, System.Reflection.Binder binder = null, object[] parameters = {object[0]}, System.Globalization .CultureInfo 文化 = null,引用 System.Threading.StackCrawlMark stackMark = LookForMyCaller) + 0x103 字节
mscorlib.dll!System.Activator.InternalCreateInstance(System.Type type = {Name = "DynamicMethodConstructorStrategy"FullName = "Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy"}, bool nonPublic = false, ref System.Threading.StackCrawlMark stackMark = LookForMyCaller) + 0xf0 字节 mscorlib.dll!System.Activator.CreateInstance() + 0xc 字节
Microsoft.Practices.Unity.Silverlight.dll!Microsoft.Practices.ObjectBuilder2.StagedStrategyChain.AddNew(Microsoft.Practices.Unity.ObjectBuilder.UnityBuildStage stage = Creation) + 0x1d 字节
Microsoft.Practices.Unity.Silverlight.dll!Microsoft.Practices.Unity.UnityDefaultStrategiesExtension.Initialize() + 0x6c 字节
Microsoft.Practices.Unity.Silverlight.dll!Microsoft.Practices.Unity.UnityContainerExtension.InitializeExtension(Microsoft.Practices.Unity.ExtensionContext context = {Microsoft.Practices.Unity.UnityContainer.ExtensionContextImpl}) + 0x31 字节
Microsoft.Practices.Unity.Silverlight.dll!Microsoft.Practices.Unity.UnityContainer.AddExtension(Microsoft.Practices.Unity.UnityContainerExtension 扩展 = {Microsoft.Practices.Unity.UnityDefaultStrategiesExtension}) + 0x1a 字节
Microsoft.Practices.Unity.Silverlight.dll!Microsoft.Practices.Unity.UnityContainer.UnityContainer() + 0xf 字节

以为我可以解决它,我尝试了几件事,但无济于事。

事实证明这是一个相当基本的问题,我假设 Windows Phone 7 是 Silverlight 3 + 其他一些东西是错误的。这个page描述 Mobile Silverlight 和 Silverlight 3 之间的区别。

特别感兴趣的是:

The System.Reflection.Emit namespace is not supported in Silverlight for Windows Phone.



这正是 Unity 在手机上崩溃的原因, DynamicMethodConstructorStrategy类使用 System.Reflection.Emit相当广泛...

所以问题是, Windows Phone 7 有什么 Unity 替代品?

最佳答案

因此,本着回答我自己问题的精神,我整理了一个简单的 DI 容器(使用 Activator.CreateInstance 进行实例化)。所有这些都是支持类型注册和实例注册。

似乎正在做这项工作。以后会担心性能。

public class DuplicateRegistrationException : Exception {
public DuplicateRegistrationException() { }
public DuplicateRegistrationException(string message) : base(message) { }
public DuplicateRegistrationException(string message, Exception inner) : base(message, inner) { }
}

public interface IDIContainer {
void Register<TIntf, TClass> () where TIntf: class where TClass : TIntf;
TIntf Resolve<TIntf>() where TIntf : class;
void RegisterInstance<TIntf>(TIntf instance);
}

public class DIContainer : IDIContainer{

Dictionary<Type, Type> m_TypeRegistrations;
Dictionary<Type, object> m_InstanceRegistrations;

public DIContainer() {
m_TypeRegistrations = new Dictionary<Type, Type>();
m_InstanceRegistrations = new Dictionary<Type, object>();
}

#region IDIContainer Members

public void Register<TIntf, TClass>()
where TIntf : class
where TClass : TIntf {
if(DoesRegistrationExist<TIntf>())
throw new DuplicateRegistrationException("Can only contain one registration per type");
m_TypeRegistrations.Add(typeof(TIntf), typeof(TClass));
}

public TIntf Resolve<TIntf>() where TIntf : class {
return Resolve(typeof(TIntf)) as TIntf;
}

private object Resolve(Type type) {
if(!m_TypeRegistrations.ContainsKey(type)) {
if(!m_InstanceRegistrations.ContainsKey(type))
throw new NotSupportedException("Cannot find registration for type " + type.FullName + ".");
else
return m_InstanceRegistrations[type];
} else {
var createdType = m_TypeRegistrations[type];

ConstructorInfo[] constructors = createdType.GetConstructors();
ConstructorInfo mostSpecificConstructor = null;
foreach(var c in constructors) {
if(mostSpecificConstructor == null || mostSpecificConstructor.GetParameters().Length < c.GetParameters().Length) {
mostSpecificConstructor = c;
}
}

List<object> constructorParameters = new List<object>();
foreach(var a in mostSpecificConstructor.GetParameters()) {
constructorParameters.Add(Resolve(a.ParameterType));
}

return Activator.CreateInstance(createdType, constructorParameters.ToArray());
}
}

private bool DoesRegistrationExist<T>() {
return m_InstanceRegistrations.ContainsKey(typeof(T)) || m_TypeRegistrations.ContainsKey(typeof(T));
}

public void RegisterInstance<TIntf>(TIntf instance) {
if(DoesRegistrationExist<TIntf>()) {
throw new DuplicateRegistrationException("Can only contain one registration per type");
}
m_InstanceRegistrations.Add(typeof(TIntf), instance);
}


#endregion

关于dependency-injection - Windows Phone 7 的依赖注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2756151/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com