84 lines
3.4 KiB
C#
84 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace BS.Shared.Core
|
|
{
|
|
public class AbstractIDSpecificDefaultClass<T> where T : AbstractIDSpecificDefaultClass<T>, new()
|
|
{
|
|
protected string _SpecificID = string.Empty;
|
|
|
|
private static readonly Dictionary<string, T> _InstanceCache = new Dictionary<string, T>();
|
|
|
|
protected AbstractIDSpecificDefaultClass() { }
|
|
|
|
public static T GetInstance(string specificId, string tenant = null)
|
|
{
|
|
if (specificId == null)
|
|
{
|
|
specificId = tenant;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(specificId))
|
|
{
|
|
if (_InstanceCache.ContainsKey(specificId))
|
|
{
|
|
return _InstanceCache[specificId];
|
|
}
|
|
|
|
var allemblyTypes = typeof(T).Assembly.GetTypes();
|
|
var assignableForms = allemblyTypes.Where(i => typeof(T).IsAssignableFrom(i));
|
|
var x = assignableForms.FirstOrDefault(t => t.GetCustomAttributes(typeof(IDSpecificClassAttribute), true).ToList().Exists(i =>
|
|
{
|
|
if (i is IDSpecificClassAttribute attribute)
|
|
{
|
|
if (attribute.Identifiers != null)
|
|
{
|
|
return attribute.Identifiers.Contains(specificId);
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(attribute.Identifier))
|
|
{
|
|
return attribute.Identifier.Equals(specificId);
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}));
|
|
|
|
var cbSpecific = typeof(T).Assembly.GetTypes()
|
|
.Where(i => typeof(T).IsAssignableFrom(i))
|
|
.FirstOrDefault(t => t.GetCustomAttributes(typeof(IDSpecificClassAttribute), true)
|
|
.ToList()
|
|
.Exists(i =>
|
|
{
|
|
var attr = (IDSpecificClassAttribute)i;
|
|
if (attr.Identifiers != null)
|
|
return attr.Identifiers.Contains(specificId);
|
|
if (!String.IsNullOrEmpty(attr.Identifier))
|
|
return attr.Identifier.Equals(specificId);
|
|
return false;
|
|
|
|
}));
|
|
if (cbSpecific != null)
|
|
{
|
|
var instance = Activator.CreateInstance(cbSpecific) as T;
|
|
instance._SpecificID = specificId;
|
|
_InstanceCache.Add(specificId, instance);
|
|
return _InstanceCache[specificId];
|
|
}
|
|
}
|
|
|
|
if (!_InstanceCache.ContainsKey(string.Empty))
|
|
{
|
|
_InstanceCache.Add(string.Empty,
|
|
new T
|
|
{
|
|
_SpecificID = specificId
|
|
});
|
|
}
|
|
|
|
return _InstanceCache[string.Empty];
|
|
}
|
|
}
|
|
} |