/// <summary> /// Creates the specified assembly name. /// </summary> /// <param name="assemblyName">Name of the assembly.</param> /// <param name="name">The name.</param> /// <returns></returns> public static T Create(string assemblyName, string name) { Register(assemblyName); object typeToCreate = null; if (AssemblyAssetRegistry.ContainsKey(assemblyName)) { HybridDictionary assetRegistry = AssemblyAssetRegistry[assemblyName]; if (assetRegistry.Contains(name)) { AssetRegistryValue registryValue = (AssetRegistryValue)assetRegistry[name]; Assembly assembly = GetAssembly(registryValue.Assembly); typeToCreate = assembly.CreateInstance(registryValue.FullName); } } return((T)typeToCreate); }
/// <summary> /// Casts the type of the instance to. /// </summary> /// <param name="instance">The instance.</param> /// <param name="assemblyName">Name of the assembly.</param> /// <param name="nameSpace">The name space.</param> /// <param name="convertToTypeName">Name of the convert to type.</param> /// <returns></returns> public static object CastInstanceToType(object instance, string assemblyName, string nameSpace, string convertToTypeName) { Type theTypeToConvertTo = instance.GetType(); if (AssemblyAssetRegistry.ContainsKey(assemblyName)) { HybridDictionary assetRegistry = AssemblyAssetRegistry[assemblyName]; if (assetRegistry.Contains(convertToTypeName)) { AssetRegistryValue registryValue = (AssetRegistryValue)assetRegistry[convertToTypeName]; Assembly assembly = GetAssembly(registryValue.Assembly); convertToTypeName = $"{nameSpace}.{convertToTypeName}"; theTypeToConvertTo = assembly.GetType(convertToTypeName); } } return(Convert.ChangeType(instance, theTypeToConvertTo)); }
/// <summary> /// Gets the type of the asset. /// </summary> /// <param name="assemblyName">Name of the assembly.</param> /// <param name="name">The name.</param> /// <returns></returns> public static Type GetAssetType(string assemblyName, string name) { Type theType = null; Register(assemblyName); if (AssemblyAssetRegistry.ContainsKey(assemblyName)) { HybridDictionary assetRegistry = AssemblyAssetRegistry[assemblyName]; if (assetRegistry.Contains(name)) { AssetRegistryValue registryValue = (AssetRegistryValue)assetRegistry[name]; Assembly assembly = GetAssembly(registryValue.Assembly); theType = assembly.GetType(name); } } return(theType); }
/// <summary> /// Registers the assembly types. /// </summary> /// <param name="assemblyTypes">The assembly types.</param> private static void RegisterAssemblyTypes(List <Type> assemblyTypes) { Type theTypeImInterestedIn = typeof(T); foreach (Type assemblyType in assemblyTypes) { string name = assemblyType.Name; string fullName = $"{assemblyType.Namespace}.{assemblyType.Name}"; Type[] extendedTypes = { }; if (theTypeImInterestedIn.IsGenericType) { extendedTypes = assemblyType.GetInterfaces(); } else { Type extendedType = assemblyType.GetInterface(theTypeImInterestedIn.Name); if (extendedType != null) { extendedTypes = new[] { extendedType }; } } var baseTypes = extendedTypes.Length > 0 ? new List <Type>(extendedTypes) : GetBaseTypeList(assemblyType); if (baseTypes.Contains(theTypeImInterestedIn)) { string assemblyName = assemblyType.Assembly.GetName().Name; HybridDictionary assetRegistry = new HybridDictionary(); if (AssemblyAssetRegistry.ContainsKey(assemblyName)) { assetRegistry = AssemblyAssetRegistry[assemblyName]; assetRegistry[name] = new AssetRegistryValue(assemblyName, assemblyType.FullName); assetRegistry[fullName] = new AssetRegistryValue(assemblyName, assemblyType.FullName); } else { assetRegistry[name] = new AssetRegistryValue(assemblyName, assemblyType.FullName); assetRegistry[fullName] = new AssetRegistryValue(assemblyName, assemblyType.FullName); AssemblyAssetRegistry.Add(assemblyName, assetRegistry); } } } }