public bool? TryCreateRef(Type interfaceType, out IObjectRef newObjectRef) { RefTrackedObject cleanupContainer; Validate.IsNotNull<Type>(interfaceType, "interfaceType"); IObjectRef innerRef = this.innerRef; if (innerRef == null) { newObjectRef = null; return null; } bool? nullable = innerRef.TryCreateRef(interfaceType, out newObjectRef); if (!nullable.GetValueOrDefault()) { return null; } ObjectRefProxy proxy = this; lock (proxy) { this.EnsureCleanupContainerCreatedWhileLocked(); cleanupContainer = this.cleanupContainer; } ICleanupContainer container = newObjectRef as ICleanupContainer; if (container == null) { ObjectRefProxy proxy2 = Create(interfaceType, newObjectRef, ObjectRefProxyOptions.AssumeOwnership); proxy2.AddCleanupRef(cleanupContainer); newObjectRef = proxy2; return nullable; } container.AddCleanupRef(cleanupContainer); return nullable; }
private IObjectRef GetCachedOrCreateResource(IResourceSource resourceSource, Type interfaceType, bool addRef = true) { object obj2; IObjectRef ref3; Validate.IsNotNull <IResourceSource>(resourceSource, "resourceSource"); if (resourceSource.IsResource) { return(GetRef((IObjectRef)resourceSource, interfaceType, addRef)); } TupleStruct <ResourceID, long> resourceKey = TupleStruct.Create <ResourceID, long>(resourceSource.ResourceID, resourceSource.Version); Label_0036: obj2 = this.sync; lock (obj2) { IObjectRef objectRef = this.TryGetCachedResourceImpl(resourceKey, interfaceType, addRef); if (objectRef != null) { return(GetRef(objectRef, interfaceType, addRef)); } objectRef = resourceSource.CreateResource(this); if (this.genCaches[0].TryAdd <TupleStruct <ResourceID, long>, IObjectRef>(resourceKey, objectRef)) { ref3 = GetRef(objectRef, interfaceType, addRef); } else { objectRef.Dispose(); goto Label_0036; } } return(ref3); }
private void DisposeCore(bool disposing) { if (Interlocked.Exchange(ref this.isDisposed, 1) == 0) { try { this.Dispose(disposing); } finally { IObjectRef innerRef = this.innerRef; this.InnerRef = null; if ((this.proxyOptions & ObjectRefProxyOptions.DoNotCreateRef) != ObjectRefProxyOptions.DoNotCreateRef) { IRefTrackedObject obj2 = innerRef as IRefTrackedObject; if (obj2 != null) { obj2.ReleaseRef(this, disposing); } } } if (disposing) { RefTrackedObject cleanupContainer; ObjectRefProxy proxy = this; lock (proxy) { cleanupContainer = this.cleanupContainer; this.cleanupContainer = null; } DisposableUtil.Free<RefTrackedObject>(ref cleanupContainer, disposing); } } }
public ObjectRefProxy(IObjectRef innerRef, ObjectRefProxyOptions proxyOptions) { Validate.IsNotNull<IObjectRef>(innerRef, "innerRef"); this.InnerRef = innerRef; this.proxyOptions = proxyOptions; this.Initialize(true); }
/// <summary> /// Registers an instance. /// </summary> /// <typeparam name="T">The type of the instance to register.</typeparam> /// <param name="key">The key.</param> /// <param name="reference">The object reference.</param> public void Register <T>(string key, IObjectRef <T> reference) { if (reference != null) { GetInstances(typeof(T))[key ?? string.Empty] = reference; } }
public void Register_uniquely_named_using_derived_type() { var implRef = _ctx.RegisterUniquelyNamed <Calculator>().GetReference(); IObjectRef <ICalculator> reference = _ctx.RegisterUniquelyNamedAlias <ICalculator>().ToRegistered(implRef) .GetReference(); Assert.That(_ctx.GetObject(reference), Is.TypeOf <Calculator>()); }
private static IObjectRef GetRef(IObjectRef objectRef, Type interfaceType, bool addRef) { if (!addRef) { return(objectRef); } return(objectRef.CreateRef(interfaceType)); }
internal void AddService(Type serviceType, IObjectRef service) { Validate.Begin().IsNotNull <Type>(serviceType, "serviceType").IsNotNull <IObjectRef>(service, "service").Check(); object sync = this.sync; lock (sync) { this.services.Add(KeyValuePairUtil.Create <Type, IObjectRef>(serviceType, service)); } }
public static TInterface TryCreateRef <TInterface>(this IObjectRef objectRef) where TInterface : class, IObjectRef { TInterface local; if (!objectRef.TryCreateRef <TInterface>(out local).GetValueOrDefault()) { return(default(TInterface)); } return(local); }
public static UnsafeObjectRef Create(IObjectRef objectRef) { Validate.IsNotNull <IObjectRef>(objectRef, "objectRef"); UnsafeObjectRef?nullable = TryCreate(objectRef); if (!nullable.HasValue) { ExceptionUtil.ThrowObjectDisposedException(objectRef.GetType().FullName); } return(nullable.Value); }
public static IObjectRef GetInnermostRef(this IObjectRef objectRef) { Validate.IsNotNull <IObjectRef>(objectRef, "objectRef"); while (true) { IObjectRefProxy proxy = objectRef as IObjectRefProxy; if (proxy == null) { return(objectRef); } objectRef = proxy.InnerRef; } }
/// <summary> /// Resolves a singleton instance. Register one if non-exist. /// </summary> /// <typeparam name="T">The type of the instance to register.</typeparam> /// <param name="key">The key.</param> /// <param name="reference">The object reference.</param> /// <returns>An instance resolved.</returns> public T EnsureResolve <T>(string key, IObjectRef <T> reference) { if (key == null) { key = string.Empty; } var set = GetInstances(typeof(T)); if (!set.TryGetValue(key, out var result)) { if (reference == null) { return(default);
public static IObjectRef CreateRef(this IObjectRef objectRef, Type interfaceType) { IObjectRef ref2; bool? nullable = objectRef.TryCreateRef(interfaceType, out ref2); if (!nullable.HasValue) { throw new ObjectDisposedException(objectRef.GetType().FullName); } if (!nullable.Value) { throw new InterfaceNotSupportedException(interfaceType); } return(ref2); }
public static bool?TryCreateRef <TInterface>(this IObjectRef objectRef, out TInterface newObjectRefT) where TInterface : class, IObjectRef { IObjectRef ref2; Validate.IsNotNull <IObjectRef>(objectRef, "objectRef"); bool?nullable = objectRef.TryCreateRef(typeof(TInterface), out ref2); if (!nullable.GetValueOrDefault()) { newObjectRefT = default(TInterface); return(nullable); } newObjectRefT = (TInterface)ref2; return(true); }
public void Dispose() { if (this.objectRef != null) { RefTrackedObject objectRef = this.objectRef as RefTrackedObject; if (objectRef != null) { objectRef.ReleaseUntrackedRef(true); } else { this.objectRef.Dispose(); } this.objectRef = null; } }
public static CastOrRefHolder <T> TryCastOrCreateRef <T>(this IObjectRef objectRef) where T : class, IObjectRef { T objectRefT = objectRef as T; if (objectRefT != null) { return(new CastOrRefHolder <T>(objectRefT, false)); } T local2 = objectRef.TryCreateRef <T>(); if (local2 != null) { return(new CastOrRefHolder <T>(local2, true)); } return(new CastOrRefHolder <T>()); }
public static UnsafeObjectRef?TryCreate(IObjectRef objectRef) { IObjectRef ref2; Validate.IsNotNull <IObjectRef>(objectRef, "objectRef"); RefTrackedObject obj2 = objectRef as RefTrackedObject; if (obj2 != null) { return(TryCreate(obj2)); } if (objectRef.TryCreateRef(typeof(IObjectRef), out ref2).GetValueOrDefault()) { return(new UnsafeObjectRef(ref2)); } return(null); }
public bool?TryGetAttachedObjectRef(object key, Type interfaceType, out IObjectRef newObjectRef) { Validate.IsNotNull <object>(key, "key"); IObjectRef ref2 = null; object sync = this.Sync; lock (sync) { if (this.attachedObjectRefs == null) { newObjectRef = null; return(false); } List <KeyValuePair <object, IObjectRef> > attachedObjectRefs = this.attachedObjectRefs as List <KeyValuePair <object, IObjectRef> >; if (attachedObjectRefs != null) { foreach (KeyValuePair <object, IObjectRef> pair in attachedObjectRefs) { if (pair.Key.Equals(key)) { ref2 = pair.Value; goto Label_00BB; } } } else { KeyValuePair <object, IObjectRef> pair2 = (KeyValuePair <object, IObjectRef>) this.attachedObjectRefs; if (pair2.Key.Equals(key)) { ref2 = pair2.Value; } } } Label_00BB: if (ref2 != null) { return(ref2.TryCreateRef(interfaceType, out newObjectRef).GetValueOrDefault()); } newObjectRef = null; return(false); }
public static ObjectRefProxy Create(Type interfaceType, IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) { Validate.Begin().IsNotNull<Type>(interfaceType, "interfaceType").IsNotNull<IObjectRef>(objectRef, "objectRef").Check(); if (interfaceType.ContainsGenericParameters) { ExceptionUtil.ThrowArgumentException($"Only closed generic interface types are allowed (interfaceType = '{interfaceType.FullName}')"); } if (interfaceType == typeof(IObjectRef)) { return new ObjectRefProxy(objectRef, proxyOptions); } ObjectRefProxyFactory orAdd = null; if (!staticInterfaceTypeToProxyFactoryMap.TryGetValue(interfaceType, out orAdd) && !dynamicInterfaceTypeToProxyFactoryMap.TryGetValue(interfaceType, out orAdd)) { orAdd = dynamicInterfaceTypeToProxyFactoryMap.GetOrAdd(interfaceType, createProxyFactory); if (orAdd.IsNullReference<ObjectRefProxyFactory>()) { ExceptionUtil.ThrowInvalidOperationException($"There is no proxy registered for '{interfaceType.FullName}'"); } } return orAdd.CreateProxy(objectRef, proxyOptions); }
public bool?TryCreateRef(Type interfaceType, out IObjectRef newObjectRef) { Validate.IsNotNull <Type>(interfaceType, "interfaceType"); if (!this.TryAddUntrackedRef()) { newObjectRef = null; return(null); } try { newObjectRef = this.TryCreateProxy(interfaceType, ObjectRefProxyOptions.Default); if (newObjectRef == null) { return(false); } } finally { this.ReleaseUntrackedRef(true); } return(true); }
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new DeviceBitmapProxy((IDeviceBitmap)objectRef, proxyOptions);
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new DxgiFactoryProxy((IDxgiFactory)objectRef, proxyOptions);
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new MetadataQueryReaderProxy((IMetadataQueryReader)objectRef, proxyOptions);
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new PaletteProxy((IPalette)objectRef, proxyOptions);
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new BitmapDecoderProxy((IBitmapDecoder)objectRef, proxyOptions);
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new DeviceResourceFactoryProxy((IDeviceResourceFactory)objectRef, proxyOptions);
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new TextFormatProxy((ITextFormat)objectRef, proxyOptions);
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new DxgiSwapChainProxy((IDxgiSwapChain)objectRef, proxyOptions);
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new GeometryFactory1Proxy((IGeometryFactory1)objectRef, proxyOptions);
public override ObjectRefProxy CreateProxy(IObjectRef objectRef, ObjectRefProxyOptions proxyOptions) => new DxgiKeyedMutexProxy((IDxgiKeyedMutex)objectRef, proxyOptions);