public void AddWithId() { NetworkableIdRegistry registry = new NetworkableIdRegistry(typeof(DummyClass1)); DummyClass1 obj1 = new DummyClass1(); DummyClass1 obj2 = new DummyClass1(); int id1 = 10; int id2 = 20; // Add item to registry Assert.DoesNotThrow(() => registry.AddWithId(obj1, id1)); Assert.That(registry.ToId(obj1), Is.EqualTo(id1)); // Re-adding the same object with a new ID is not allowed Assert.Throws <ArgumentException>(() => registry.AddWithId(obj1, id2)); // Adding a different object with the same ID is not allowed Assert.Throws <ArgumentException>(() => registry.AddWithId(obj2, id1)); }
/// <summary> /// Add an item to the registry, with a predetermined ID. Once added, the registry can perform item<->ID translation for that item. /// Adding the same item twice is not supported. /// Adding two items with the same ID is not supported. /// </summary> public static int AddWithId(T item, int id) { Assert.IsNotNull(rootRegistry, "NetworkableId<" + typeof(T).Name + "> registry has not yet been initialized"); return(rootRegistry.AddWithId(item, id)); }