/// <summary> /// Reflects the types of an assembly. /// </summary> /// <param name="fileName">The file name of the assembly to reflect.</param> /// <param name="filter">The type filter used to determine which types to reflect.</param> /// <param name="useNewAppDomain"> /// If set to true, a new app domain will be used for /// the reflection. /// </param> /// <returns>The result of the reflection.</returns> public NRAssembly Reflect(string fileName, ref IFilter filter, bool useNewAppDomain = true) { if (useNewAppDomain) { // Create the new AppDomain AppDomain newAppDomain = AppDomain.CreateDomain("NReflect-AppDomain", null, null); // Create a new instance of Reflector at the new AppDomain string dllName = Assembly.GetAssembly(typeof(Reflector)).FullName; Reflector reflector = ( Reflector )newAppDomain.CreateInstanceAndUnwrap(dllName, "NReflect.Reflector"); // Move the filter from this to the new instance reflector.Filter = filter; NRAssembly nrAssembly = reflector.Reflect(fileName); // Move the filter back from the new to this instance filter = reflector.Filter; AppDomain.Unload(newAppDomain); return(nrAssembly); } return(Reflect(fileName)); }
// ======================================================================== // Methods #region === Methods /// <summary> /// Extracts the relationships between the types of <paramref name="nrAssembly"/>. /// </summary> /// <param name="nrAssembly">The relationships are extracted from the types within /// this <see cref="NRAssembly"/>.</param> /// <param name="createNesting">Set to <c>true</c> to create nesting relationships.</param> /// <param name="createGeneralization">Set to <c>true</c> to create generalization relationships.</param> /// <param name="createRealization">Set to <c>true</c> to create realization relationships.</param> /// <param name="createAssociation">Set to <c>true</c> to create association relationships.</param> /// <returns>The extracted relationships.</returns> public NRRelationships CreateRelationships(NRAssembly nrAssembly, bool createNesting = true, bool createGeneralization = true, bool createRealization = true, bool createAssociation = true) { NRRelationships nrRelationships = new NRRelationships(); Dictionary <string, NRTypeBase> entities = nrAssembly.Types.ToDictionary(nrTypeBase => nrTypeBase.FullName); //Create the nesting relationships if (createNesting) { foreach (NRTypeBase nrTypeBase in entities.Values) { if (!String.IsNullOrWhiteSpace(nrTypeBase.DeclaringTypeFullName)) { if (entities.ContainsKey(nrTypeBase.DeclaringTypeFullName)) { if (entities[nrTypeBase.DeclaringTypeFullName] is NRSingleInheritanceType parent) { nrRelationships.Nestings.Add(new NRNesting(parent, nrTypeBase)); } } } } } //Create the generalization relationships if (createGeneralization) { foreach (NRSingleInheritanceType derivedType in nrAssembly.SingleInheritanceTypes) { if (derivedType.BaseType != null && derivedType.BaseType.FullName != null) { var key = string.IsNullOrEmpty(derivedType.BaseType.FullName) ? derivedType.BaseType.Name : derivedType.BaseType.FullName; if (entities.ContainsKey(key)) { if (entities[key] is NRSingleInheritanceType baseType) { nrRelationships.Generalizations.Add(new NRGeneralization(baseType, derivedType)); } } } } // Interfaces may derive from other interfaces as well. foreach (NRInterface derivedInterface in nrAssembly.Interfaces) { foreach (NRTypeUsage implementedInterface in derivedInterface.ImplementedInterfaces) { var key = string.IsNullOrEmpty(implementedInterface.FullName) ? implementedInterface.Name : implementedInterface.FullName; if (entities.ContainsKey(key)) { if (entities[key] is NRInterface nrInterface) { nrRelationships.Generalizations.Add(new NRGeneralization(nrInterface, derivedInterface)); } } } } } //Create the realization relationships if (createRealization) { foreach (NRSingleInheritanceType implementingType in nrAssembly.SingleInheritanceTypes) { foreach (NRTypeUsage implementedInterface in implementingType.ImplementedInterfaces) { var key = string.IsNullOrEmpty(implementedInterface.FullName) ? implementedInterface.Name : implementedInterface.FullName; if (entities.ContainsKey(key)) { if (entities[key] is NRInterface nrInterface) { nrRelationships.Realizations.Add(new NRRealization(nrInterface, implementingType)); } } } } } //Create the association relationships if (createAssociation) { foreach (NRSingleInheritanceType startType in nrAssembly.SingleInheritanceTypes) { foreach (NRField nrField in startType.Fields) { string fullName = nrField.TypeFullName; bool array = false; if (fullName.EndsWith("[]")) { //Array! fullName = fullName.Substring(0, fullName.IndexOf('[')); array = true; } if (fullName.Contains("[")) { //Generic! fullName = fullName.Substring(0, fullName.IndexOf('[')); } if (entities.ContainsKey(fullName)) { NRTypeBase endType = entities[fullName]; NRAssociation association = new NRAssociation { StartType = startType, EndMultiplicity = array ? "*" : "1", StartRole = nrField.Name, EndType = endType }; nrRelationships.Associations.Add(association); } } } } return(nrRelationships); }