using System; using System.Collections.Generic; using System.Reflection; using Deltares.Geometry; using Deltares.Geotechnics; using Deltares.Geotechnics.GeotechnicalGeometry; using Deltares.Geotechnics.Soils; using Deltares.Stability; using Deltares.Standard; using Deltares.Standard.IO; namespace Deltares.MStab { public class ClassFactory : IClassFactory { private List assembliesList = null; private MStabProject mStabProject = null; private Dictionary typesDictionary = null; public ClassFactory() { typesDictionary = new Dictionary(); assembliesList = new List(); AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(OnAssemblyLoad); UpdateTypeDictionaries(); } #region IClassFactory Members public object Create(string aTypeFullName) { object requiredObject = null; switch (aTypeFullName) { case "Deltares.MStab.MStabProject": { requiredObject = new MStabProject(); mStabProject = requiredObject as MStabProject; break; } //case "Deltares.Stability.Soil": // { // requiredObject = new Deltares.Soils.Data.Soil(); // break; // } case "Deltares.Stability.StressCurve": { requiredObject = new StressCurve(); break; } case "Deltares.Geometry.GeometryData": { requiredObject = new GeometryData(); //((GeometryData) requiredObject).CoordinateSystems = this.coordinateSystem; break; } /*case "Deltares.Standard.ProjectIdentification": { requiredObject = new Deltares.Standard.Project(); break; } case "Deltares.Stability.SoilGeometryModel": { requiredObject = new Deltares.Stability.SoilGeometryModel(this.mStabProject.Geometry, this.mStabProject.Soils); break; }*/ case "Deltares.Stability.SoilModel": { requiredObject = new SoilModel(); break; } case "Deltares.Stability.StabilityModel": { requiredObject = new StabilityModel(); break; } case "Deltares.Geotechnics.GeotechnicsModel": { requiredObject = new GeotechnicsModel(); break; } default: { Type aType = GetClassType(aTypeFullName); if (aType != null) { requiredObject = Activator.CreateInstance(aType); } break; } } return requiredObject; } public object Create(Type aType) { if (aType.FullName.Contains("unit")) {} return Create(aType.FullName); } #endregion private string GetShortName(string aTypeFullName) { if (aTypeFullName.IndexOf('.') != -1) { string[] types = aTypeFullName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); return (string) types.GetValue(types.Length - 1); } return aTypeFullName; } private void OnAssemblyLoad(object aObject, AssemblyLoadEventArgs aAssemblyLoadEventArgs) { UpdateTypesFromAssembly(aAssemblyLoadEventArgs.LoadedAssembly); } /// /// Stores the type information in order to fetch the type fastly. /// private void UpdateTypeDictionaries() { typesDictionary.Clear(); Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); int length = assemblies.Length; if (length > 0) { for (int i = 0; i < length; i++) { UpdateTypesFromAssembly(assemblies[i]); } } } private void UpdateTypesFromAssembly(Assembly aAssembly) { if (!assembliesList.Contains(aAssembly.FullName) && aAssembly.FullName.StartsWith("Delft")) { try { Type[] types = aAssembly.GetTypes(); if (types.Length > 0) { if (!assembliesList.Contains(aAssembly.FullName)) { assembliesList.Add(aAssembly.FullName); } foreach (var loopType in types) { // Usual case if (!typesDictionary.ContainsKey(loopType.FullName)) { typesDictionary.Add(loopType.FullName, loopType); } } } } catch { // could be com dll } } } /// /// Gets the type based on a name /// /// The name that has to be checked /// Type is returned private Type GetClassType(string aName) { if (typesDictionary.ContainsKey(aName)) { return typesDictionary[aName]; } return null; } } }