/// <summary> /// Register a resources events /// </summary> private void RegisterEvents(Resource instance) { instance.Changed += OnResourceChanged; if (instance is IPublicResource asPublic) { asPublic.CapabilitiesChanged += RaiseCapabilitiesChanged; } foreach (var autoSaveCollection in ResourceReferenceTools.GetAutoSaveCollections(instance)) { autoSaveCollection.CollectionChanged += OnAutoSaveCollectionChanged; } }
/// <summary> /// Register a resources events /// </summary> private void UnregisterEvents(Resource instance) { instance.Changed -= OnResourceChanged; var asPublic = instance as IPublicResource; if (asPublic != null) { asPublic.CapabilitiesChanged -= RaiseCapabilitiesChanged; } foreach (var autoSaveCollection in ResourceReferenceTools.GetAutoSaveCollections(instance)) { autoSaveCollection.CollectionChanged -= OnAutoSaveCollectionChanged; } }
public Resource Create(string type) { if (!_typeCache.ContainsKey(type)) { throw new KeyNotFoundException($"No resource of type {type} found!"); } var linker = _typeCache[type]; if (!linker.Creatable) { throw new InvalidOperationException($"The resource of type {type} is not creatable."); } var instance = linker.IsRegistered ? (Resource)ResourceFactory.Create(type) // Create with factory : (Resource)Activator.CreateInstance(linker.ResourceType); // Create manually // Set reference collections ResourceReferenceTools.InitializeCollections(instance); return(instance); }