75 lines
2.5 KiB
C#
75 lines
2.5 KiB
C#
// /**
|
|
// * 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<T> : IManagedObject where T:class,IDisposable
|
|
{
|
|
T instance;
|
|
Dictionary<string, object> properties = new Dictionary<string, object>();
|
|
|
|
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<T>();
|
|
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<T>).GetNativePropertyDescriptor(propertyName).GetValue(instance) : properties[propertyName];
|
|
}
|
|
|
|
public void SetValue(string propertyName, object value)
|
|
{
|
|
if (instance != null)
|
|
(Container as ManagedNativeContainer<T>).GetNativePropertyDescriptor(propertyName).SetValue(instance, value);
|
|
properties[propertyName] = value;
|
|
}
|
|
|
|
private void Sync()
|
|
{
|
|
foreach (NativePropertyDescriptor nativePropertyDescriptor in (Container as ManagedNativeContainer<T>).NativePropertyDescriptors)
|
|
{
|
|
if (properties.ContainsKey(nativePropertyDescriptor.PropertyName))
|
|
nativePropertyDescriptor.SetValue(instance, properties[nativePropertyDescriptor.PropertyName]);
|
|
}
|
|
}
|
|
private void SyncBack()
|
|
{
|
|
foreach (NativePropertyDescriptor nativePropertyDescriptor in (Container as ManagedNativeContainer<T>).NativePropertyDescriptors)
|
|
{
|
|
properties[nativePropertyDescriptor.PropertyName] = nativePropertyDescriptor.GetValue(instance);
|
|
}
|
|
}
|
|
}
|
|
}
|