// /** // * File: ManagedNativeObject.cs // * Author: haraldwolff // * // * This file and it's content is copyrighted by the Author and / or copyright holder. // * Any use wihtout proper permission is illegal and may lead to legal actions. // * // * // **/ using System; using System.Collections.Generic; namespace ln.manage { public class ManagedNativeObject : IManagedObject where T:class,IDisposable { T instance; Dictionary properties = new Dictionary(); public ManagedNativeObject(IManagedContainer container) { Container = container; } public IManagedContainer Container { get; } public object Identity => GetValue(Container.IdentityPropertyName); public bool Enabled { get => instance != null; set { if (value && !Enabled) { instance = (T)Activator.CreateInstance(); Sync(); } else if (!value && Enabled) { instance.Dispose(); SyncBack(); instance = null; } } } public bool Valid => throw new NotImplementedException(); public object GetValue(string propertyName) { return (instance != null) ? (Container as ManagedNativeContainer).GetNativePropertyDescriptor(propertyName).GetValue(instance) : properties[propertyName]; } public void SetValue(string propertyName, object value) { if (instance != null) (Container as ManagedNativeContainer).GetNativePropertyDescriptor(propertyName).SetValue(instance, value); properties[propertyName] = value; } private void Sync() { foreach (NativePropertyDescriptor nativePropertyDescriptor in (Container as ManagedNativeContainer).NativePropertyDescriptors) { if (properties.ContainsKey(nativePropertyDescriptor.PropertyName)) nativePropertyDescriptor.SetValue(instance, properties[nativePropertyDescriptor.PropertyName]); } } private void SyncBack() { foreach (NativePropertyDescriptor nativePropertyDescriptor in (Container as ManagedNativeContainer).NativePropertyDescriptors) { properties[nativePropertyDescriptor.PropertyName] = nativePropertyDescriptor.GetValue(instance); } } } }