/// <summary> /// Creates the instance of the specified extension object instance type. /// </summary> /// <param name="fullyQualifiedName">The type fully qualified name to consider.</param> /// <param name="object1">The object to consider.</param> /// <param name="attributes">The attributes to consider.</param> public static IBdoLog CreateInstance( string fullyQualifiedName, out object object1, params object[] attributes) { var log = new BdoLog(); object1 = null; try { Type type = Type.GetType(fullyQualifiedName); if (type == null) { log.AddError("Unknown type '" + fullyQualifiedName + "'"); } else { object1 = Activator.CreateInstance(type); } } catch (Exception ex) { log.AddException(ex); } return(log); }
public void ConnectorUsingConnectionTest() { BdoLog log = new BdoLog(); // check bad connection _connector?.UsingConnection((p, l) => { }, log); string xml = string.Empty; if (log.HasErrorsOrExceptions()) { xml = ". Result was '" + log.ToXml() + "'"; } Assert.That(!log.HasErrorsOrExceptions(), "Connection creation failed" + xml); // check bad connection try { _connector?.UsingConnection((p, l) => { string toto = null; int i = toto.Length; }, log); } catch (NullReferenceException ex) { log.AddException(ex); } xml = string.Empty; if (log.HasErrorsOrExceptions()) { xml = ". Result was '" + log.ToXml() + "'"; } Assert.That(log.HasErrorsOrExceptions(), "Connection creation failed" + xml); }
/// <summary> /// Creates the instance of the specified extension object instance type. /// </summary> /// <param name="type">The type to consider.</param> /// <param name="object1">The object to consider.</param> /// <param name="attributes">The attributes to consider.</param> public static IBdoLog CreateInstance( Type type, out Object object1, params object[] attributes) { var log = new BdoLog(); object1 = null; try { object1 = Activator.CreateInstance(type); } catch (Exception ex) { log.AddException(ex); } return(log); }
public void OneTimeSetUp() { var log = new BdoLog(); for (int i = 0; i < 2; i++) { log.AddError("Error" + i); log.AddException("Exception" + i); log.AddMessage("Message" + i); log.AddWarning("Warning" + i); log.AddSubLog(new BdoLog() { DisplayName = "Sub log" + i }); } _testData = new { log }; }
/// <summary> /// Loads a data context from file. /// </summary> /// <param name="filePath">The path of the file to load.</param> /// <param name="log">The log that receives the log of this loading task.</param> /// <returns>Returns the data context loaded from the specified file.</returns> public static BdoDataContext Load(string filePath, ref BdoLog log) { BdoDataContext dataContext = null; Stream fileStream = null; try { fileStream = File.Open(filePath, FileMode.Open); BinaryFormatter formatter = new BinaryFormatter(); dataContext = (BdoDataContext)formatter.Deserialize(fileStream); } catch (IOException exception) { log.AddException(exception); } finally { fileStream?.Close(); } return(dataContext); }
/// <summary> /// Loads the specified library. /// </summary> /// <param name="libraryReference">The library reference to consider.</param> /// <returns>Returns the loaded library.</returns> private IBdoLog LoadLibrary(IBdoAssemblyReference libraryReference) { var log = new BdoLog(); if (libraryReference != null && _loadOptions?.SourceKinds != null) { try { Assembly assembly = null; // first we load the assembly IBdoLog firstLog = new BdoLog() { DisplayName = "Loading library '" + libraryReference.Name + "'" }; foreach (DatasourceKind dataSourceKind in _loadOptions?.SourceKinds) { IBdoLog subLog = firstLog.AddSubLog(title: "Loading assembly from '" + dataSourceKind.ToString() + "'", eventKind: EventKinds.Message); switch (dataSourceKind) { case DatasourceKind.Memory: if (!string.IsNullOrEmpty(libraryReference.Name)) { assembly = AppDomainPool.LoadAssembly(_appDomain, libraryReference.Name, subLog); } else { subLog?.AddWarning("File name missing"); } break; case DatasourceKind.Repository: string fileName = libraryReference.FileName; if (string.IsNullOrEmpty(fileName)) { fileName = libraryReference.Name + ".dll"; } string filePath = _loadOptions.LibraryFolderPath.EndingWith(@"\").ToPath() + fileName; if (!File.Exists(filePath)) { subLog?.AddError("Could not find the assembly file path '" + filePath + "'"); } else { assembly = AppDomainPool.LoadAssemblyFromFile(_appDomain, filePath, subLog); if (assembly == null) { subLog?.AddError("Could not load assembly '" + filePath + "'"); } else { subLog?.AddCheckpoint("Loading assembly from file '" + filePath + "'"); assembly = Assembly.LoadFrom(filePath); } } break; case DatasourceKind.RestApi: break; } if (assembly != null) { subLog?.AddMessage("Assembly loaded"); break; } } // if we have an assembly then we index library items if (assembly == null) { log?.AddSubLog(firstLog, p => p.HasErrorsOrExceptionsOrWarnings()); } else { firstLog.GetEvents(true, EventKinds.Error, EventKinds.Exception).ForEach(p => p.Kind = EventKinds.Warning); log?.AddSubLog(firstLog); // we get the extension definition IBdoExtensionDefinition extensionDefinition = ExtractExtensionDefinition(assembly, null, log); // we load the using assemblies if (extensionDefinition?.Dto?.UsingAssemblyFileNames != null) { foreach (var st in extensionDefinition.Dto.UsingAssemblyFileNames) { var fileName = st; if (!fileName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase)) { fileName += ".dll"; } IBdoAssemblyReference reference = BdoAssemblyReferenceFactory.Create(st).WithFileName(fileName); log.AddSubLog(LoadExtensionsInStore(reference), title: "Loading using extensions..."); } } // we load the item definition specifiying the extension definition foreach (BdoExtensionItemKind kind in new[] { BdoExtensionItemKind.Carrier, BdoExtensionItemKind.Connector, BdoExtensionItemKind.Entity, BdoExtensionItemKind.Handler, BdoExtensionItemKind.Metrics, BdoExtensionItemKind.Routine, BdoExtensionItemKind.Scriptword, BdoExtensionItemKind.Task }) { IBdoLog subSubLog = new BdoLog(); int count = LoadDictionary(assembly, kind, extensionDefinition, subSubLog); if (subSubLog.HasErrorsOrExceptionsOrWarnings()) { log.AddSubLog( subSubLog, title: "Dictionary '" + kind.ToString() + "' loaded (" + count.ToString() + " items added)"); } else { log.AddMessage("Dictionary '" + kind.ToString() + "' loaded (" + count.ToString() + " items added)"); } } } } catch (Exception exception) { log?.AddException(exception); } } return(log); }
/// <summary> /// Sets information of the specified property. /// </summary> /// <param name="aObject">The object to update.</param> /// <param name="elementSet">The set of elements to return.</param> /// <param name="scope">The scope to consider.</param> /// <param name="scriptVariableSet">The script variable set to use.</param> public static IBdoLog UpdateFromElementSet <T>( this Object aObject, IDataElementSet elementSet, IBdoScope scope = null, IScriptVariableSet scriptVariableSet = null) where T : DataElementAttribute { var log = new BdoLog(); if (aObject == null || elementSet.Items == null) { return(null); } foreach (PropertyInfo propertyInfo in aObject.GetType().GetProperties()) { if (propertyInfo.GetCustomAttribute(typeof(T)) is Attribute attribute) { if (attribute is DataElementAttribute elementAttribute) { string name = elementAttribute.Name; if (string.IsNullOrEmpty(name)) { name = propertyInfo.Name; } try { if (elementSet.HasItem(name)) { var type = propertyInfo.PropertyType; var value = elementSet.GetValue(name, scope, scriptVariableSet, log); if (value != null) { if (type.IsEnum) { if (!value.GetType().IsEnum&& Enum.IsDefined(type, value)) { value = Enum.Parse(type, value as string); } } } else if (value?.GetType() == typeof(Dictionary <string, object>) && type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <,>) && type != typeof(Dictionary <string, object>)) { Type itemType = type.GetGenericArguments()[0]; var dictionary = Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(typeof(string), itemType)); var method = dictionary.GetType().GetMethod("Add", new Type[] { typeof(string), itemType }); foreach (var item in (value as Dictionary <string, object>)) { method.Invoke(dictionary, new object[] { item.Key, Convert.ChangeType(item.Value, itemType) }); } value = dictionary; } propertyInfo.SetValue(aObject, value); } } catch (Exception ex) { log.AddException(ex); } } } } return(log); }