// // Licensed under the MIT License. See LICENSE file in the project root for full license information. // namespace MUnique.OpenMU.Persistence; using System.Collections.Concurrent; using System.Reflection; /// /// Helper class which offers functions related to the extended data model types. /// public static class TypeHelper { private static readonly string ConfigurationNamespace = "MUnique.OpenMU.DataModel.Configuration"; /// /// A cache which holds extended types (Value) for their corresponding base type (Key). /// private static readonly IDictionary BaseToPersistentTypes = new ConcurrentDictionary(); /// /// Gets the ef core type of . /// /// Base type of the data model. /// The originating assembly of the persistent type of . /// Extended ef core type of . public static Type GetPersistentTypeOf(this Assembly origin) { return origin.GetPersistentTypeOf(typeof(TBase)); } /// /// Gets the ef core type of the given base type. /// /// The originating assembly of the persistent type of the base type. /// /// Base type of the data model. /// Extended ef core type of the base type. public static Type GetPersistentTypeOf(this Assembly origin, Type baseType) { if (baseType.Assembly == origin) { // TBase is already the persistent type return baseType; } if (!BaseToPersistentTypes.TryGetValue(baseType, out var persistentType)) { persistentType = origin.GetTypes().First(t => t.BaseType == baseType); BaseToPersistentTypes.Add(baseType, persistentType); } return persistentType; } /// /// Creates a new object of the extended ef core type of the . /// /// The base type of the data model. /// The originating assembly of the persistent type of . /// The arguments. /// /// A new object of the extended ef core type of the . /// public static TBase CreateNew(this Assembly origin, params object?[] args) where TBase : class { var persistentType = origin.GetPersistentTypeOf(); if (args.Length == 0) { return (TBase)Activator.CreateInstance(persistentType)!; } return (TBase)Activator.CreateInstance(persistentType, args)!; } /// /// Creates a new object of the extended ef core type of the given type. /// /// The originating assembly of the persistent type of . /// The type which should get created. /// The arguments. /// /// A new object of the extended ef core type of the . /// public static object CreateNew(this Assembly origin, Type type, params object?[] args) { var persistentType = origin.GetPersistentTypeOf(type); if (args.Length == 0) { return Activator.CreateInstance(persistentType)!; } return Activator.CreateInstance(persistentType, args)!; } /// /// Determines whether the given type is a is configuration type. /// /// The type. /// /// true if the given type is a configuration type; otherwise, false. /// public static bool IsConfigurationType(this Type type) { if (type.Namespace != null && type.Namespace.StartsWith(ConfigurationNamespace, StringComparison.InvariantCulture)) { return true; } if (type.BaseType is { Namespace: { } } && type.BaseType.Namespace.StartsWith(ConfigurationNamespace, StringComparison.InvariantCulture)) { return true; } if (type.Name.Contains("Definition", StringComparison.InvariantCulture)) { return true; } if (type.Name is "AttributeRelationship" or "PlugInConfiguration" or "ConstValueAttribute") { return true; } return false; } }