91 lines
3.9 KiB
C#
91 lines
3.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.ComponentModel;
|
|
using System.Security.Cryptography;
|
|
using System.Reflection;
|
|
namespace ln.manage
|
|
{
|
|
public class ManagedNativeContainer<T> : ManagedContainerBase where T:class,IDisposable
|
|
{
|
|
Type managedType;
|
|
Dictionary<string, NativePropertyDescriptor> propertyDescriptors = new Dictionary<string, NativePropertyDescriptor>();
|
|
HashSet<ManagedNativeObject<T>> managedNativeObjects = new HashSet<ManagedNativeObject<T>>();
|
|
|
|
public ManagedNativeContainer(IManagedContainer parent) :this(parent, typeof(T).Name) { }
|
|
public ManagedNativeContainer(IManagedContainer parent,String name)
|
|
:base(parent,name)
|
|
{
|
|
managedType = typeof(T);
|
|
|
|
foreach (FieldInfo fieldInfo in managedType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
|
|
{
|
|
if (fieldInfo.IsPublic || fieldInfo.GetCustomAttribute<PropertyDescriptorAttribute>() != null)
|
|
{
|
|
NativePropertyDescriptor nativePropertyDescriptor = new NativePropertyDescriptor(this, fieldInfo);
|
|
propertyDescriptors.Add(nativePropertyDescriptor.PropertyName,nativePropertyDescriptor);
|
|
}
|
|
}
|
|
foreach (PropertyInfo propertyInfo in managedType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
|
|
{
|
|
if (propertyInfo.GetGetMethod().IsPublic || propertyInfo.GetCustomAttribute<PropertyDescriptorAttribute>() != null)
|
|
{
|
|
NativePropertyDescriptor nativePropertyDescriptor = new NativePropertyDescriptor(this, propertyInfo);
|
|
propertyDescriptors.Add(nativePropertyDescriptor.PropertyName, nativePropertyDescriptor);
|
|
}
|
|
}
|
|
|
|
foreach (NativePropertyDescriptor nativePropertyDescriptor in NativePropertyDescriptors)
|
|
{
|
|
if (nativePropertyDescriptor.Identity)
|
|
IdentityDescriptor = nativePropertyDescriptor;
|
|
}
|
|
|
|
}
|
|
public IEnumerable<NativePropertyDescriptor> NativePropertyDescriptors => propertyDescriptors.Values;
|
|
public NativePropertyDescriptor GetNativePropertyDescriptor(string propertyName) => propertyDescriptors[propertyName];
|
|
public NativePropertyDescriptor IdentityDescriptor { get; }
|
|
|
|
public override IEnumerable<PropertyDescriptor> PropertyDescriptors => propertyDescriptors.Values;
|
|
public override string IdentityPropertyName => IdentityDescriptor.PropertyName;
|
|
|
|
public override IManagedObject CreateManagedObject()
|
|
{
|
|
ManagedNativeObject<T> managedNativeObject = new ManagedNativeObject<T>(this);
|
|
managedNativeObjects.Add(managedNativeObject);
|
|
return managedNativeObject;
|
|
}
|
|
|
|
public override bool DisposeManagedObject(object identity) => DisposeManagedObject(GetManagedObject(identity));
|
|
public bool DisposeManagedObject(ManagedNativeObject<T> managedNativeObject)
|
|
{
|
|
if (managedNativeObjects.Contains(managedNativeObject))
|
|
{
|
|
managedNativeObject.Enabled = false;
|
|
managedNativeObjects.Remove(managedNativeObject);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public override IManagedObject GetManagedObject(object identity)
|
|
{
|
|
foreach (ManagedNativeObject<T> managedNativeObject in managedNativeObjects)
|
|
{
|
|
if (Object.Equals(managedNativeObject.Identity, identity))
|
|
return managedNativeObject;
|
|
}
|
|
throw new KeyNotFoundException();
|
|
}
|
|
|
|
public override IManagedObject[] GetManagedObjects()
|
|
{
|
|
return managedNativeObjects.Cast<IManagedObject>().ToArray();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|