protected virtual Object[] CreateMethodArgument(IList <Object> businessObjects, Type expectedType) { int objectCount = businessObjects.Count; if (typeof(IEnumerable).IsAssignableFrom(expectedType)) { Object targetCollection = ListUtil.CreateCollectionOfType(expectedType); MethodInfo addMethod = targetCollection.GetType().GetMethod("Add"); Object[] parameters = new Object[1]; for (int a = 0; a < objectCount; a++) { parameters[0] = businessObjects[a]; addMethod.Invoke(targetCollection, parameters); } return(new Object[] { targetCollection }); } else if (expectedType.IsArray) { Array array = Array.CreateInstance(expectedType.GetElementType(), objectCount); for (int a = 0; a < objectCount; a++) { array.SetValue(businessObjects[a], a); } return(new Object[] { array }); } return(ListUtil.ToArray(businessObjects)); }
protected void BuildSetterCommands(Object entity, IObjRef[] newORIs, RelationMember member, IReader reader) { if (!member.IsToMany) { if (newORIs.Length == 0) { return; } else if (newORIs.Length == 1) { IObjectFuture objectFuture = new ObjRefFuture(newORIs[0]); IObjectCommand command = CommandBuilder.Build(reader.CommandTypeRegistry, objectFuture, entity, member); reader.AddObjectCommand(command); } else { throw new ArgumentException("Multiple values for to-one relation"); } } else { Object coll = ListUtil.CreateCollectionOfType(member.RealType, newORIs.Length); MethodInfo addMethod = coll.GetType().GetMethod("Add"); Object[] parameters = new Object[1]; bool useObjectFuture = false; ICommandBuilder commandBuilder = CommandBuilder; ICommandTypeRegistry commandTypeRegistry = reader.CommandTypeRegistry; foreach (IObjRef ori in newORIs) { if (!(ori is IDirectObjRef)) { IObjectFuture objectFuture = new ObjRefFuture(ori);; IObjectCommand command = commandBuilder.Build(commandTypeRegistry, objectFuture, coll, addMethod); reader.AddObjectCommand(command); useObjectFuture = true; continue; } Object item = ((IDirectObjRef)ori).Direct; if (useObjectFuture) { IObjectCommand command = commandBuilder.Build(commandTypeRegistry, null, coll, addMethod, item); reader.AddObjectCommand(command); } else { parameters[0] = item; addMethod.Invoke(coll, parameters); } } } }
protected Object ConvertToExpectedType(Type expectedType, Object result) { if (typeof(void).Equals(expectedType) || result == null) { return(null); } else if (expectedType.IsAssignableFrom(result.GetType())) { return(result); } if (typeof(IEnumerable).IsAssignableFrom(expectedType) && !typeof(String).Equals(expectedType)) { Object targetCollection = ListUtil.CreateCollectionOfType(expectedType); MethodInfo addMethod = targetCollection.GetType().GetMethod("Add"); Type addType = addMethod.GetParameters()[0].ParameterType; Object[] parameters = new Object[1]; if (result is IEnumerable) { foreach (Object item in (IEnumerable)result) { Object convertedItem = ConversionHelper.ConvertValueToType(addType, item); parameters[0] = convertedItem; addMethod.Invoke(targetCollection, parameters); } } return(targetCollection); } else if (expectedType.IsArray) { List <Object> list = new List <Object>(); if (result is IEnumerable) { foreach (Object item in (IEnumerable)result) { list.Add(item); } } Array array = Array.CreateInstance(expectedType.GetElementType(), list.Count); for (int a = 0, size = list.Count; a < size; a++) { array.SetValue(list[a], a); } return(array); } throw new Exception("Can not convert result " + result + " to expected type " + expectedType.FullName); }
protected virtual Object PostProcessCacheResult(IList <IObjRef> objRefs, IList <Object> cacheResult, Type expectedType, IServiceResult serviceResult, Object[] originalArgs, Attribute annotation) { int cacheResultSize = cacheResult != null ? cacheResult.Count : objRefs.Count; if (typeof(IEnumerable).IsAssignableFrom(expectedType)) { Object targetCollection = ListUtil.CreateCollectionOfType(expectedType); MethodInfo addMethod = targetCollection.GetType().GetMethod("Add"); Object[] parameters = new Object[1]; if (cacheResult != null) { for (int a = 0; a < cacheResultSize; a++) { parameters[0] = cacheResult[a]; addMethod.Invoke(targetCollection, parameters); } } else { for (int a = 0; a < cacheResultSize; a++) { parameters[0] = objRefs[a]; addMethod.Invoke(targetCollection, parameters); } } return(targetCollection); } else if (expectedType.IsArray) { Array array = Array.CreateInstance(expectedType.GetElementType(), cacheResultSize); if (cacheResult != null) { for (int a = 0; a < cacheResultSize; a++) { array.SetValue(cacheResult[a], a); } } else { for (int a = 0; a < cacheResultSize; a++) { array.SetValue(objRefs[a], a); } } return(array); } IEntityMetaData metaData = EntityMetaDataProvider.GetMetaData(expectedType, true); if (metaData != null) { // It is a simple entity which can be returned directly if (cacheResultSize == 0) { return(null); } else if (cacheResultSize == 1) { return(cacheResult != null ? cacheResult[0] : objRefs[0]); } } Object additionalInformation = serviceResult != null ? serviceResult.AdditionalInformation : null; if (additionalInformation == null) { throw new Exception("Can not convert list of " + cacheResultSize + " results from cache to type " + expectedType.FullName); } IServiceResultProcessor serviceResultProcessor = ServiceResultProcessorRegistry.GetServiceResultProcessor(expectedType); return(serviceResultProcessor.ProcessServiceResult(additionalInformation, objRefs, cacheResult, expectedType, originalArgs, annotation)); }