/// <summary> /// Maps the target's properties using a Dictionary that contains {Name, Value} properties. /// </summary> /// /// <param name="target"> /// Target object. /// </param> /// /// <param name="properties"> /// Dictionary that contains {Name, Value} properties. /// </param> /// /// <param name="throwMappingException"> /// Value indicating whether exception is thrown when a property cannot be mapped. /// </param> public static void MapProperties(object target, IDictionary <string, object> properties, bool throwMappingException = true) { if (properties != null && properties.Count != 0) { Type targetType = target.GetType(); PropertyInfo[] props = targetType.GetProperties().Where(p => p.CanWrite).ToArray(); foreach (var property in properties) { PropertyInfo pi = props.FirstOrDefault(p => p.Name == property.Key); if (pi != null) { pi.SetValue(target, property.Value); continue; } if (throwMappingException) { ThrowException.Throw( "The '{0}' property (from '{1}.{2}' class) cannot be found or setted", property.Key, targetType.Namespace, targetType.Name); } } } }
public static Stream GetEmbeddedStream(string assemblyFullName, string assemblyName, string file) { Assembly assembly = Assembly.Load(assemblyFullName); Stream stream = assembly.GetManifestResourceStream(string.Format("{0}.{1}", assemblyName, file)); if (stream == null) { ThrowException.Throw( @"Could not locate embedded resource '{0}' in assembly '{1}'", file, assemblyName); } return(stream); }
/// <summary> /// Unregisters an action. /// </summary> /// /// <typeparam name="T"> /// Type of the object. /// </typeparam> /// /// <param name="key"> /// Key of the messenger. /// </param> /// /// <param name="throwExceptionOnError"> /// Value indicating whether an exception is thrown when an unregistration error occurred. /// </param> public void Unregister <T>(string key, bool throwExceptionOnError = true) { var t = typeof(T); var messengerEntry = _actions.Where(kvp => kvp.Key.Type == t && kvp.Key.Key == key).FirstOrDefault(); if (messengerEntry.Key == null) { if (throwExceptionOnError) { ThrowException.Throw( "Cannot unregister (key = '{0}', Type = {1}) because it does not exist", key, t.FullName); } } object removedObj; _actions.TryRemove(messengerEntry.Key, out removedObj); removedObj = null; }
/// <summary> /// Transforms a flat collection to a hierarchy object. /// Elements must implements TreeNodeBase abstract class. /// </summary> /// /// <typeparam name="TNode"> /// Type of the items. /// </typeparam> /// /// <param name="flatCollection"> /// The collection that contains all the elements of a hierarchy. /// Elements must implements TreeNodeBase abstract class. /// </param> /// /// <param name="idSelector"> /// Func that returns the Id property of an item. /// </param> /// /// <param name="idParentSelector"> /// Func that returns the Parent Id property of an item. /// </param> /// /// <param name="orderBySelector"> /// Func that sorts items on same parent node. /// </param> /// /// <returns> /// The hierarchy. /// </returns> public static IEnumerable <ITreeNode <TNode> > Hierarchize <TNode, TOrderKey>( IEnumerable <ITreeNode <TNode> > flatCollection, Func <TNode, long> idSelector, Func <TNode, long?> idParentSelector, Func <TNode, TOrderKey> orderBySelector = null) where TNode : class { if (flatCollection.IsNullOrEmpty()) { return(flatCollection); } var orderByFunc = new Func <IEnumerable <ITreeNode <TNode> >, IEnumerable <ITreeNode <TNode> > >(collection => { return((orderBySelector != null) ? collection = collection.OrderBy(o => orderBySelector(o.Item)) : collection); }); var dirtyFlatCollection = flatCollection.DeepCopy(); // not to modify flatCollection input foreach (var item in dirtyFlatCollection) { long?idParent = idParentSelector(item.Item); if (idParent != null) { var parent = dirtyFlatCollection.FirstOrDefault(i => idSelector(i.Item) == idParent); if (parent == null) { ThrowException.Throw("Cannot hierarchize the flatcollection because it is not complete (missing parent item with Id = {0}", idParent); } item.Parent = parent; } item.Children = orderByFunc(dirtyFlatCollection.Where(i => i != item && idParentSelector(i.Item) != null && idParentSelector(i.Item) == idSelector(item.Item))); } return(orderByFunc(dirtyFlatCollection.Where(i => idParentSelector(i.Item) == null))); }
/// <summary> /// Get a new random value. /// </summary> /// /// <param name="type"> /// Type of the current value. /// </param> /// /// <param name="currentValue"> /// Current value. /// </param> /// /// <returns> /// The new value. /// </returns> public static object GetNewValue(Type type, object currentValue) { if (type == typeof(string)) { string value = (string)currentValue; if (value == null) { value = "A"; } return(RandomHelper.GetRandomString(value.Length)); } if (type == typeof(int) || type == typeof(int?)) { int?value = (int?)currentValue; if (value == null) { value = 0; } if (value.Value == int.MaxValue) { return(value.Value - 1); } if (value.Value == int.MinValue) { return(value.Value + 1); } return(value.Value + 1); } if (type == typeof(long) || type == typeof(long?)) { long?value = (long?)currentValue; if (value == null) { value = 0; } if (value.Value == long.MaxValue) { return(value.Value - 1); } if (value.Value == long.MinValue) { return(value.Value + 1); } return(value.Value + 1); } if (type == typeof(decimal) || type == typeof(decimal?)) { decimal?value = (decimal?)currentValue; if (value == null) { value = 0; } if (value.Value == decimal.MaxValue) { return(value.Value - 1); } if (value.Value == decimal.MinValue) { return(value.Value + 1); } return(value.Value + 1); } if (type == typeof(DateTime) || type == typeof(DateTime?)) { DateTime?value = (DateTime?)currentValue; if (value == null) { value = DateTime.Now; } return(value.Value.AddMinutes(1)); } if (type == typeof(bool) || type == typeof(bool?)) { bool?value = (bool?)currentValue; if (value == null) { value = false; } return(!value.Value); } if (type == typeof(Guid)) { return(DateTime.Now.ToLongTimeString().ToGuid()); } ThrowException.Throw( "RandomHelper::GetNewValue, type '{0}' not supported!", (currentValue != null) ? currentValue.GetType().FullName : "null"); return(null); // for compilation only }
private string CreateCacheKey(object[] inputs, bool withGuidFormat = true) { string key = string.Format("{0}.{1}", _operation.DeclaringContract.ContractType, _operation.Name); if (_keyGeneratorType != null) { key = KeyGeneratorHelper.GenerateKey <string>(_keyGeneratorType, inputs); } else { string inputsKey = string.Empty; // *** BEGIN SPECIFIC CODE *** // *************************** // Note: if you plan to use this class in another project, you will probably remove this code. if (_withUserContextDependency) // -> The operation result depends on the user context { // Design Constraint: input[0] is always IUserContext parameter (LayerCake Generator process checks this point). if (inputs.Length > 0) { inputsKey = SerializerHelper.ToXml(SerializerType.DataContract, inputs[0]); } #if DEBUG if (!inputsKey.Contains("ClientContext") && !inputsKey.Contains("MemberContext")) { ThrowException.Throw( "Something is going wrong: Business & Service methods must define at first the IUserContext parameter."); } #endif } // *** END SPECIFIC CODE *** // *************************** StringBuilder sbXml = new StringBuilder(inputsKey); for (int i = 1; i < inputs.Length; i++) { sbXml.Append(SerializerHelper.ToXml(SerializerType.DataContract, inputs[i])); } inputsKey = sbXml.ToString(); key = string.Format("{0}({1})", key, inputsKey); } if (withGuidFormat) { using (var provider = MD5.Create()) { byte[] data = provider.ComputeHash(Encoding.Default.GetBytes(key)); key = new Guid(data).ToString(); } } return(key); }