public static IPooledObject GetInstanceFromPool(Type serviceType) { if (!serviceType.GetInterfaces().Contains(typeof(IPooledObject))) { throw new InvalidCastException("InstanceType must implement Artech.WCFExtensions.IPooledInstance"); } if (!ServiceInstancePool.ContainsKey(serviceType)) { ServiceInstancePool[serviceType] = new WeakReferenceCollection(); } WeakReferenceCollection instanceReferenceList = ServiceInstancePool[serviceType]; lock (serviceType) { IPooledObject serviceInstance = null; foreach (WeakReference weakReference in instanceReferenceList) { serviceInstance = weakReference.Target as IPooledObject; if (serviceInstance != null && !serviceInstance.IsBusy) { serviceInstance.IsBusy = true; return(serviceInstance); } } serviceInstance = Activator.CreateInstance(serviceType) as IPooledObject; serviceInstance.IsBusy = true; instanceReferenceList.Add(new WeakReference(serviceInstance)); return(serviceInstance); } }
public void WeakReferenceCollection_Remove_Test1() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List <string>(); list.Add(item1); list.Add(item2); var result = new WeakReferenceCollection <string>(); result.AddRange(list); verify.ItemsAreEqual(new[] { "AAA", "BBB" }, result, "Two items should have been in the collection."); verify.IsTrue(result.Remove(item2), "item 1 should have been removable"); verify.ItemsAreEqual(new[] { "AAA" }, result, "One item should have been left in the collection."); verify.IsTrue(result.Remove(item1), "item 2 should have been removable"); verify.IsEmpty(result, "Removal failed"); verify.IsFalse(result.Remove(item1), "item was already removed"); verify.ArgumentNullException("item", () => result.Remove(null)); } }
public void AliveItemsToList_ShouldReturnAllAliveItems() { var list = new object[] { new object(), new object(), new object() }; var wrc = new WeakReferenceCollection<object>(); var obj1 = new object(); wrc.Add(obj1); foreach (object obj in list) { wrc.Add(obj); } var obj2 = new object(); wrc.Add(obj2); Assert.AreEqual(list.Length + 2, wrc.AliveItemsToList().Count, "Should have same number of items!"); obj1 = obj2 = null; GC.Collect(); GC.WaitForPendingFinalizers(); var aliveItems = wrc.AliveItemsToList(); Assert.AreEqual(list.Length, aliveItems.Count, "Should have 2 less items!"); Assert.AreEqual(list[0], aliveItems[0]); Assert.AreEqual(list[1], aliveItems[1]); Assert.AreEqual(list[2], aliveItems[2]); }
public static IPooledObject GetInstanceFromPool(Type serviceType) { if (!serviceType.GetInterfaces().Contains(typeof(IPooledObject))) { throw new InvalidCastException("InstanceType must implement PooledInstanceProvider.WCFExtensions.IPooledInstance"); } if (!ServiceInstancePool.ContainsKey(serviceType)) { ServiceInstancePool[serviceType] = new WeakReferenceCollection(); } WeakReferenceCollection instanceReferenceList = ServiceInstancePool[serviceType]; lock (serviceType) { IPooledObject serviceInstance = null; foreach (WeakReference weakReference in instanceReferenceList) { serviceInstance = weakReference.Target as IPooledObject; if (serviceInstance != null && !serviceInstance.IsBusy) { serviceInstance.IsBusy = true; return serviceInstance; } } serviceInstance = Activator.CreateInstance(serviceType) as IPooledObject; serviceInstance.IsBusy = true; instanceReferenceList.Add(new WeakReference(serviceInstance)); return serviceInstance; } }
public void WeakReferenceCollection_CopyTo_Test1() { using (var verify = new Verify()) { var result = new WeakReferenceCollection <string>(); verify.ArgumentNullException("array", () => result.CopyTo(null, 0)); } }
public void WeakReferenceCollection_Add() { using (var verify = new Verify()) { var result = new WeakReferenceCollection<string>(); verify.ArgumentNullException("item", () => result.Add(null)); } }
public void WeakReferenceCollection_Add() { using (var verify = new Verify()) { var result = new WeakReferenceCollection <string>(); verify.ArgumentNullException("item", () => result.Add(null)); } }
public void WeakReferenceCollection_AddRange_Test1() { using (var verify = new Verify()) { IList<string> list = null; var result = new WeakReferenceCollection<string>(); verify.ArgumentNullException("list", () => result.AddRange(list)); } }
public void WeakReferenceCollection_AddRange_Test1() { using (var verify = new Verify()) { IList <string> list = null; var result = new WeakReferenceCollection <string>(); verify.ArgumentNullException("list", () => result.AddRange(list)); } }
public void WeakReferenceCollection_CopyTo_Test2() { using (var verify = new Verify()) { var result = new WeakReferenceCollection <string>(); result.Add(""); var array = new string[0]; verify.ArgumentException("array", () => result.CopyTo(array, 0), "array is too small"); } }
public void WeakReferenceCollection_CopyTo_Test5() { using (var verify = new Verify()) { var result = new WeakReferenceCollection <string>(); result.Add(""); var array = new string[0]; verify.ArgumentOutOfRangeException("arrayIndex", -1, () => result.CopyTo(array, -1)); } }
public void WeakReferenceCollection_CopyTo_Test3() { using (var verify = new Verify()) { var result = new WeakReferenceCollection <string>(); result.Add("AAA"); var array = new string[1]; result.CopyTo(array, 0); verify.ItemsAreEqual(new[] { "AAA" }, array, ""); } }
public void AliveItemsToList_ShouldReturnAllItems() { var list = new object[] {new object(), new object(), new object()}; var wrc = new WeakReferenceCollection<object>(); foreach (object obj in list) { wrc.Add(obj); } Assert.AreEqual(list.Length, wrc.AliveItemsToList().Count, "Should have same number of items!"); }
public void Remove_ObjectShouldGetRemoved() { var obj = new object(); var wrc = new WeakReferenceCollection<object>(); wrc.Add(obj); Assert.AreEqual(1, wrc.AliveItemsToList().Count, "Should have 1 item!"); wrc.Remove(obj); Assert.AreEqual(0, wrc.AliveItemsToList().Count, "Should have 0 item!"); }
/// <summary> /// Remove a reference to the specified object (if there is a live reference). /// </summary> /// <param name="collection">The collection to remove the reference from.</param> /// <param name="id">The id of the object to remove references to.</param> public static void RemoveReferenceTo(this WeakReferenceCollection collection, DisposableObjectId id) { foreach (WeakReferenceOf <IManagedDisposableObject> weakreference in collection) { if (weakreference.Id.Equals(id)) { // We expect only one reference to the object collection.Remove(weakreference); Debug.Assert(collection.Where(x => x.Id.Equals(id)).Count() == 0, "Expected only one reference"); break; } } }
public void WeakReferenceCollection_AddRange_Test3() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List<string>(); list.Add(item1); list.Add(null); list.Add(item2); var result = new WeakReferenceCollection<string>(); verify.ArgumentException("list", () => result.AddRange(list), "The list contains a null item"); } }
public void WeakReferenceCollection_AddRange_Test3() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List <string>(); list.Add(item1); list.Add(null); list.Add(item2); var result = new WeakReferenceCollection <string>(); verify.ArgumentException("list", () => result.AddRange(list), "The list contains a null item"); } }
private void AddIndexEntries(PartManager partManager) { foreach (string contractName in partManager.GetImportedContractNames()) { if (!_partManagerIndex.TryGetValue(contractName, out WeakReferenceCollection <PartManager>?indexEntries)) { indexEntries = new WeakReferenceCollection <PartManager>(); _partManagerIndex.Add(contractName, indexEntries); } if (!indexEntries.Contains(partManager)) { indexEntries.Add(partManager); } } }
public void WeakReferenceCollection_AddRange_Test2() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List <string>(); list.Add(item1); list.Add(item2); var result = new WeakReferenceCollection <string>(); result.AddRange(list); verify.ItemsAreSame(new[] { "AAA", "BBB" }, result, "Two items should have been in the collection."); } }
public void WeakReferenceCollection_AddRange_Test2() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List<string>(); list.Add(item1); list.Add(item2); var result = new WeakReferenceCollection<string>(); result.AddRange(list); verify.ItemsAreSame(new[] { "AAA", "BBB" }, result, "Two items should have been in the collection."); } }
public static void Scavenge() { foreach (Type serviceType in ServiceInstancePool.Keys) { lock (serviceType) { WeakReferenceCollection instanceReferenceList = ServiceInstancePool[serviceType]; for (int i = instanceReferenceList.Count - 1; i > -1; i--) { if (instanceReferenceList[i].Target == null) { instanceReferenceList.RemoveAt(i); } } } } }
static void Main(string[] args) { Dictionary <string, WeakReferenceCollection> list = new System.Collections.Generic.Dictionary <string, WeakReferenceCollection>(); int count = 0; while (true) { WeakReferenceCollection wc = new WeakReferenceCollection(); list.Add(count.ToString(), wc); count++; wc.Add(new WeakReference(new A() { Name = "Jim" })); wc.Add(new WeakReference(new A() { Name = "Tom" })); wc.Add(new WeakReference(new A() { Name = "Will" })); object obj = new object(); wc.Add(new WeakReference(obj)); obj = null; GC.Collect(); string name = (wc.First().Target as A).Name; string str = "[amount] = [100.1m] and [committed] >= [20.0m]"; string pattern1 = @"\[\d+\.\d+m\]"; MatchEvaluator evaluator = new MatchEvaluator(test); string result = Regex.Replace(str, pattern1, evaluator, RegexOptions.IgnoreCase); string tt = ((OfferSide)3).ToString(); OfferSide aa; bool bb = Enum.TryParse <OfferSide>("3", out aa); bool cc = Enum.IsDefined(typeof(OfferSide), aa); } Console.ReadKey(); }
public void WeakReferenceCollection_GetEnumerator() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List <string>(); list.Add(item1); list.Add(item2); var result = new WeakReferenceCollection <string>(); result.AddRange(list); verify.ItemsAreEqual((IEnumerable <string>) new[] { "AAA", "BBB" }, list, ""); verify.IsFalse(((ICollection <string>)result).IsReadOnly, "Is read only should be false"); } }
public void Add_ObjectShouldGetCollected() { var obj = new object(); var wrc = new WeakReferenceCollection<object>(); wrc.Add(obj); var wr = new WeakReference(obj); obj = null; Assert.IsNotNull(wr.Target, "Object should NOT have been collected yet!"); GC.Collect(); GC.WaitForPendingFinalizers(); Assert.IsNull(wr.Target, "Object should have been collected!"); GC.KeepAlive(wrc); }
/// <summary> /// Call the dispose method of all dependent objects. /// </summary> /// <param name="obj"> /// The depended on object. This object will not be disposed, but its /// dependents will be. /// </param> private void DisposeDependents(IManagedDisposableObject obj) { // Disposing an object removes it from the list. To avoid trying to iterate a list // that is being modified, we copy the list first. var referencesToDispose = new WeakReferenceCollection(this.dependencies[obj.Id]); foreach (WeakReferenceOf <IManagedDisposableObject> reference in referencesToDispose) { IManagedDisposableObject objectToDispose = reference.Target; if (null != objectToDispose) { this.DisposeObject(objectToDispose); } else { // The weak reference can't be resolved. Remove all entries. this.dependencies[obj.Id].RemoveReferenceTo(reference.Id); this.RemoveEntriesFor(reference.Id); } } }
public void WeakReferenceCollection_Remove_Test2() { using (var verify = new Verify()) { var result = new WeakReferenceCollection <Foo>(); Action add = () => result.Add(new Foo()); add(); Memory.CycleGC(); var f = new Foo(); result.Add(f); result.Add(f); result.Remove(f); result.Remove(f); result.CleanUp(); verify.IsEmpty(result, ""); } }
public void WeakReferenceCollection_Remove_Test2() { using (var verify = new Verify()) { var result = new WeakReferenceCollection <Foo>(); #pragma warning disable IDE0039 // Use local function Action add = () => result.Add(new Foo()); #pragma warning restore IDE0039 // Use local function add(); Memory.CycleGC(); var f = new Foo(); result.Add(f); result.Add(f); result.Remove(f); result.Remove(f); result.CleanUp(); verify.IsEmpty(result, ""); } }
public void WeakReferenceCollection_CopyTo_Test1() { using (var verify = new Verify()) { var result = new WeakReferenceCollection<string>(); verify.ArgumentNullException("array", () => result.CopyTo(null, 0)); } }
public void WeakReferenceCollection_CopyTo_Test3() { using (var verify = new Verify()) { var result = new WeakReferenceCollection<string>(); result.Add("AAA"); var array = new string[1]; result.CopyTo(array, 0); verify.ItemsAreEqual(new[] { "AAA" }, array, ""); } }
private void AddIndexEntries(PartManager partManager) { foreach (string contractName in partManager.GetImportedContractNames()) { WeakReferenceCollection<PartManager> indexEntries; if (!this._partManagerIndex.TryGetValue(contractName, out indexEntries)) { indexEntries = new WeakReferenceCollection<PartManager>(); this._partManagerIndex.Add(contractName, indexEntries); } if (!indexEntries.Contains(partManager)) { indexEntries.Add(partManager); } } }
/// <summary>The initialize.</summary> /// <returns> /// The <see cref="Dictionary" />. /// </returns> private static Dictionary<string, WeakContext> Initialize() { WeakCleanUp.RegisterAction(CleanContexts); _weakContexts = new WeakReferenceCollection<WeakContext>(); return new Dictionary<string, WeakContext>(); }
public void WeakReferenceCollection_CopyTo_Test5() { using (var verify = new Verify()) { var result = new WeakReferenceCollection<string>(); result.Add(""); var array = new string[0]; verify.ArgumentOutOfRangeException("arrayIndex", -1, () => result.CopyTo(array, -1)); } }
public void WeakReferenceCollection_GetEnumerator() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List<string>(); list.Add(item1); list.Add(item2); var result = new WeakReferenceCollection<string>(); result.AddRange(list); verify.ItemsAreEqual((IEnumerable<string>)new[] { "AAA", "BBB" }, list, ""); verify.IsFalse(((ICollection<string>)result).IsReadOnly, "Is read only should be false"); } }
public void WeakReferenceCollection_Remove_Test1() { using (var verify = new Verify()) { string item1 = "AAA"; string item2 = "BBB"; var list = new List<string>(); list.Add(item1); list.Add(item2); var result = new WeakReferenceCollection<string>(); result.AddRange(list); verify.ItemsAreEqual(new[] { "AAA", "BBB" }, result, "Two items should have been in the collection."); verify.IsTrue(result.Remove(item2), "item 1 should have been removable"); verify.ItemsAreEqual(new[] { "AAA" }, result, "One item should have been left in the collection."); verify.IsTrue(result.Remove(item1), "item 2 should have been removable"); verify.IsEmpty(result, "Removal failed"); verify.IsFalse(result.Remove(item1), "item was already removed"); verify.ArgumentNullException("item", () => result.Remove(null)); } }
public void WeakReferenceCollection_Remove_Test2() { using (var verify = new Verify()) { var result = new WeakReferenceCollection<Foo>(); Action add = () => result.Add(new Foo()); add(); Memory.CycleGC(); var f = new Foo(); result.Add(f); result.Add(f); result.Remove(f); result.Remove(f); result.CleanUp(); verify.IsEmpty(result, ""); } }
public void WeakReferenceCollection_CopyTo_Test2() { using (var verify = new Verify()) { var result = new WeakReferenceCollection<string>(); result.Add(""); var array = new string[0]; verify.ArgumentException("array", () => result.CopyTo(array, 0), "array is too small"); } }
private HotkeyManager() { _keyBindings = new WeakReferenceCollection<KeyBinding>(); var parameters = new HwndSourceParameters("Hotkey sink") { HwndSourceHook = HandleMessage, ParentWindow = HwndMessage }; _source = new HwndSource(parameters); SetHwnd(_source.Handle); }
public void AliveItemsToList_ShouldReturnEmpty() { var wrc = new WeakReferenceCollection<object>(); Assert.AreEqual(0, wrc.AliveItemsToList().Count, "Should have 0 items!"); }