예제 #1
0
        public bool canAdaptTo(Type formalArg)
        {
            if (dictionaryType.IsAssignableFrom(formalArg))
            {
                return(true);
            }
            else if (mappedType != null)
            {
                return(((formalArg.IsInterface || formalArg.IsAbstract) && formalArg.IsAssignableFrom(mappedType)) || formalArg.IsAssignableFrom(mappedType));
            }
            else if (formalArg.Name.Equals(objectName))
            {
                return(true);
            }
            else if (typedObject.canAdaptTo(formalArg))
            {
                return(true);
            }
            else
            {
                try
                {
                    return(TypeLoader.LoadType(objectName).IsAssignableFrom(formalArg));
                }
                catch (Exception)
                {
                }

                // if we got here, the type used by the client cannot be found on the server.
                // the last resort is to check if there is an argument factory
                return(ObjectFactories.GetArgumentObjectFactory(formalArg.FullName) != null);
            }
        }
예제 #2
0
        public bool canAdaptTo(Type formalArg)
        {
            return(typeof(IAdaptingType).IsAssignableFrom(formalArg) ||
                   (!formalArg.IsArray && !formalArg.IsValueType) ||
                   typeof(IDictionary).IsAssignableFrom(formalArg) ||
#if (FULL_BUILD)
                   typeof(StringDictionary).IsAssignableFrom(formalArg) ||
#endif
                   ObjectFactories.GetArgumentObjectFactory(formalArg.FullName) != null ||
                   haveMatchingData(formalArg));
        }
예제 #3
0
 public void GetFactoryTest()
 {
     using (FileLog l = new FileLog("DBProviderFactoryTest", new FileLogSettings()
     {
         DateFolderMask = "yyyy-MM-dd"
     }))
     {
         l.Debug("SpeedTest 1 Start ");
         DbProviderFactory f = null;
         int i = 0;
         for (i = 0; i < 1000000; i++)
         {
             f = ObjectFactories.GetFactory <DbProviderFactory>("System.Data.SqlClient");
         }
         l.Debug($"SpeedTest 2 Finish {i} count result {f}");
         l.Debug("GetFactoryProviderNames:");
         foreach (string s in ObjectFactories.GetFactoryNames <DbProviderFactory>())
         {
             l.Debug(s);
         }
     }
 }
예제 #4
0
        private void setFieldsDirect(object obj, IDictionary properties, ReferenceCache referenceCache)
        {
            if (ReportUnderflow)
            {
                Dictionary <object, object> propertiesCopy = new Dictionary <object, object>();

                foreach (object key in properties.Keys)
                {
                    propertiesCopy.Add(key, properties[key]);
                }

                properties = propertiesCopy;
            }

            Type type = obj.GetType();



            bool logDebug = Log.isLogging(LoggingConstants.DEBUG);

            while (!Object.ReferenceEquals(type, typeof(object)))
            {
                FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

                foreach (FieldInfo field in fields)
                {
                    if ((field.Attributes & FieldAttributes.Literal) == FieldAttributes.Literal)
                    {
                        continue;
                    }

                    IMemberRenameAttribute[] renamers = (IMemberRenameAttribute[])field.GetCustomAttributes(typeof(IMemberRenameAttribute), true);
                    string memberName = field.Name;

                    if (renamers.Length > 0)
                    {
                        memberName = renamers[0].GetClientName(type, field);
                    }

                    object fieldValue = properties[memberName];

                    if (fieldValue == null)
                    {
#if (FULL_BUILD)
                        SerializationConfigHandler serializationConfig = (SerializationConfigHandler)ORBConfig.GetInstance().GetConfig("weborb/serialization");

                        if (serializationConfig != null && serializationConfig.Keywords.Contains(memberName))
                        {
                            memberName = serializationConfig.PrefixForKeywords + memberName;
                            fieldValue = properties[memberName];

                            if (fieldValue == null)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
#endif
#if (SILVERLIGHT || PURE_CLIENT_LIB || WINDOWS_PHONE8)
                        continue;
#endif
                    }

                    if (fieldValue is IAdaptingType)
                    {
                        if (logDebug)
                        {
                            Log.log(LoggingConstants.DEBUG, "initializing field " + field.Name);
                            Log.log(LoggingConstants.DEBUG, "field type - " + field.FieldType.FullName);
                        }

                        object val = ObjectFactories.CreateArgumentObject(field.FieldType, (IAdaptingType)fieldValue);

                        if (val != null)
                        {
                            if (logDebug)
                            {
                                Log.log(LoggingConstants.DEBUG, "argument factory created object for the field " + val);
                            }

                            //referenceCache[ fieldValue ] = val;

                            referenceCache.AddObject((IAdaptingType)fieldValue, field.FieldType, val);

                            fieldValue = val;
                        }
                        else
                        {
                            if (logDebug)
                            {
                                Log.log(LoggingConstants.DEBUG, "argument factory is missing or returned no value. will use type adaptation");
                            }

                            if (fieldValue is ICacheableAdaptingType)
                            {
                                fieldValue = ((ICacheableAdaptingType)fieldValue).adapt(field.FieldType, referenceCache);
                            }
                            else
                            {
                                fieldValue = ((IAdaptingType)fieldValue).adapt(field.FieldType);
                            }
                        }
                    }

                    if (ReportUnderflow)
                    {
                        properties.Remove(memberName);
                    }

                    try
                    {
                        field.SetValue(obj, fieldValue);
                    }
                    catch (Exception e)
                    {
                        if (Log.isLogging(LoggingConstants.INFO))
                        {
                            Log.log(LoggingConstants.INFO, "field name - " + field.Name);
                        }
                        throw e;
                    }
                }

                PropertyInfo[] props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

                foreach (PropertyInfo prop in props)
                {
                    if (!prop.CanWrite)
                    {
                        continue;
                    }

                    IMemberRenameAttribute[] renamers = (IMemberRenameAttribute[])prop.GetCustomAttributes(typeof(IMemberRenameAttribute), true);
                    string memberName = prop.Name;

                    if (renamers.Length > 0)
                    {
                        memberName = renamers[0].GetClientName(type, prop);
                    }

                    object propValue = properties[memberName];

                    if (propValue == null)
                    {
#if (FULL_BUILD)
                        SerializationConfigHandler serializationConfig = (SerializationConfigHandler)ORBConfig.GetInstance().GetConfig("weborb/serialization");

                        if (serializationConfig != null && serializationConfig.Keywords.Contains(memberName))
                        {
                            memberName = serializationConfig.PrefixForKeywords + memberName;
                            propValue  = properties[memberName];

                            if (propValue == null)
                            {
                                continue;
                            }
                        }
                        else
                        {
                            continue;
                        }
#endif
#if (SILVERLIGHT || PURE_CLIENT_LIB || WINDOWS_PHONE8)
                        continue;
#endif
                    }

                    if (propValue is IAdaptingType)
                    {
                        if (logDebug)
                        {
                            Log.log(LoggingConstants.DEBUG, "initializing property " + prop.Name);
                            Log.log(LoggingConstants.DEBUG, "property type - " + prop.PropertyType.FullName);
                        }

                        object val = ObjectFactories.CreateArgumentObject(prop.PropertyType, (IAdaptingType)propValue);

                        if (val != null)
                        {
                            if (logDebug)
                            {
                                Log.log(LoggingConstants.DEBUG, "argument factory created object for the field " + val);
                            }

                            //referenceCache[ propValue ] = val;
                            referenceCache.AddObject((IAdaptingType)propValue, prop.PropertyType, val);

                            propValue = val;
                        }
                        else
                        {
                            if (logDebug)
                            {
                                Log.log(LoggingConstants.DEBUG, "argument factory is missing or returned no value. will use type adaptation");
                            }

                            if (propValue is ICacheableAdaptingType)
                            {
                                propValue = ((ICacheableAdaptingType)propValue).adapt(prop.PropertyType, referenceCache);
                            }
                            else
                            {
                                propValue = ((IAdaptingType)propValue).adapt(prop.PropertyType);
                            }
                        }
                    }

                    if (ReportUnderflow)
                    {
                        properties.Remove(memberName);
                    }

                    prop.SetValue(obj, propValue, null);
                }

                type = type.BaseType;
            }

            if (ReportUnderflow && properties.Count > 0)
            {
                ReportObjectUnderflow(obj, properties);
            }
        }
예제 #5
0
        public object adapt(Type type, ReferenceCache refCache)
        {
            if (refCache.HasObject(this, type))
            {
                return(refCache.GetObject(this, type));
            }

            object obj = ObjectFactories.CreateArgumentObject(type, this);

            if (obj != null)
            {
                refCache.AddObject(this, type, obj);
                return(obj);
            }

            if (type.Equals(typeof(IAdaptingType)))
            {
                refCache.AddObject(this, type, obj);
                return(this);
            }

            if (!type.IsArray)
            {
                obj = ObjectFactories.CreateServiceObject(type);
                refCache.AddObject(this, type, obj);
            }

            //refCache[ this ] = obj;

            if (obj is IDictionary)
            {
                IDictionary dictionary = (IDictionary)obj;
                ICollection keys       = properties.Keys;

                foreach (object key in keys)
                {
                    object valueObj = properties[key];

                    if (valueObj != null)
                    {
                        if (type.IsGenericType)
                        {
                            Type[] args = type.GetGenericArguments();

                            if (valueObj is ICacheableAdaptingType)
                            {
                                valueObj = ((ICacheableAdaptingType)valueObj).adapt(args[1]);
                            }
                            else if (valueObj is IAdaptingType)
                            {
                                valueObj = ((IAdaptingType)valueObj).adapt(args[1]);
                            }
                        }
                        else
                        {
                            if (valueObj is ICacheableAdaptingType)
                            {
                                valueObj = ((ICacheableAdaptingType)valueObj).defaultAdapt(refCache);
                            }
                            else if (valueObj is IAdaptingType)
                            {
                                valueObj = ((IAdaptingType)valueObj).defaultAdapt();
                            }
                        }
                    }

                    object keyValue = key;

                    if (type.IsGenericType && key is IAdaptingType)
                    {
                        Type[] args = type.GetGenericArguments();
                        keyValue = ((IAdaptingType)key).adapt(args[0]);
                    }

                    dictionary.Add(keyValue, valueObj);
                }
            }
            else if (type.IsArray && canBeArray())
            {
                Type  elementType = type.GetElementType();
                Array newArray    = Array.CreateInstance(elementType, properties.Count);
                refCache.AddObject(this, type, newArray);

                foreach (String key in properties.Keys)
                {
                    int index;
                    int.TryParse(key, out index);
                    object valueObj = properties[key];

                    if (valueObj is ICacheableAdaptingType)
                    {
                        valueObj = ((ICacheableAdaptingType)valueObj).adapt(elementType, refCache);
                    }
                    else if (valueObj is IAdaptingType)
                    {
                        valueObj = ((IAdaptingType)valueObj).adapt(elementType);
                    }

                    newArray.SetValue(valueObj, index);
                }

                obj = newArray;
            }
            else if (type.BaseType == null)
            {
                return(defaultAdapt(refCache));
            }
#if (FULL_BUILD)
            else if (obj is StringDictionary || typeof(StringDictionary).IsAssignableFrom(type))
            {
                StringDictionary strDict = (StringDictionary)obj;
                ICollection      keys    = properties.Keys;

                foreach (object key in keys)
                {
                    string mappedValue;
                    object valueObj = properties[key];

                    if (valueObj is ICacheableAdaptingType)
                    {
                        mappedValue = (string)((ICacheableAdaptingType)valueObj).adapt(typeof(string), refCache);
                    }
                    else if (valueObj is IAdaptingType)
                    {
                        mappedValue = (string)((IAdaptingType)valueObj).adapt(typeof(string));
                    }
                    else
                    {
                        mappedValue = valueObj.ToString();
                    }

                    strDict.Add(key.ToString(), mappedValue);
                }
            }
#endif
            else
            {
                //if( accessCheckSuppressed )
                setFieldsDirect(obj, properties, refCache);
                //else
                //    setFieldsAsBean( obj, properties );
            }

            return(obj);
        }
예제 #6
0
        public object adapt(Type type, ReferenceCache refCache)
        {
            if (refCache.HasObject(this, type))
            {
                /*object obj = refCache[ this ];
                 *
                 * if( type.IsAssignableFrom( obj.GetType() ) )
                 *  return obj;*/

                return(refCache.GetObject(this, type));
            }

            if (Log.isLogging(LoggingConstants.DEBUG))
            {
                Log.log(LoggingConstants.DEBUG, "ArrayType.adapt, adapting type: " + type.FullName);
            }

            int size = arrayObject.Length;

            if (type == typeof(IAdaptingType))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is an adapting type");
                }

                return(this);
            }
            else if (type.IsArray)
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is array");
                }

                Type componentType = type.GetElementType();

                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    if (componentType != null)
                    {
                        Log.log(LoggingConstants.DEBUG, "array element type is " + componentType.FullName);
                    }
                    else
                    {
                        Log.log(LoggingConstants.DEBUG, "array element type is null");
                    }
                }

                Array newArray = Array.CreateInstance(componentType, size);
                //refCache[ this ] = newArray;

                refCache.AddObject(this, type, newArray);

                for (int i = 0; i < size; i++)
                {
                    newArray.SetValue(adaptArrayComponent(arrayObject[i], componentType, refCache), i);
                }

                return(newArray);
            }
            else if (type.IsGenericType && type.GetGenericArguments().Length == 2)
            {
                Type constructedType = Types.Types.GetAbstractClassMapping(type);

                Type   genericTypeDef = type.GetGenericTypeDefinition();
                Type[] genericArgs    = type.GetGenericArguments();

                if (constructedType == null)
                {
                    constructedType = genericTypeDef.MakeGenericType(genericArgs);
                }

                object newDictionary = Activator.CreateInstance(constructedType);
                refCache.AddObject(this, type, newDictionary);
                Type       keyValuePairType = typeof(KeyValuePair);
                MethodInfo addMethod        = constructedType.GetMethod("Add", genericArgs);
                object[]   args             = new object[2];

                for (int i = 0; i < arrayObject.Length; i++)
                {
                    KeyValuePair kvPair = (KeyValuePair)((IAdaptingType)arrayObject[i]).adapt(keyValuePairType);

                    args[0] = kvPair.key.adapt(genericArgs[0]);
                    args[1] = kvPair.value.adapt(genericArgs[1]);

                    addMethod.Invoke(newDictionary, args);
                }

                return(newDictionary);
            }
            else if (type.GetInterface("System.Collections.Generic.ICollection`1") != null)
            {
                Type   genericCollection = type.GetInterface("System.Collections.Generic.ICollection`1");
                Type   elementType       = genericCollection.GetGenericArguments()[0];
                object collection        = ObjectFactories.CreateServiceObject(type);

                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "created service object which is a collection");
                }

                refCache.AddObject(this, type, collection);
                MethodInfo addMethod = type.GetMethod("Add", new Type[] { elementType });

                if (addMethod == null)
                {
                    addMethod = collection.GetType().GetMethod("Add", new Type[] { elementType });
                }

                object[] args = new object[1];

                for (int i = 0; i < arrayObject.Length; i++)
                {
                    //args[ 0 ] = ((IAdaptingType) arrayObject[ i ]).adapt( elementType );
                    args[0] = adaptArrayComponent(arrayObject[i], elementType, refCache);

                    if (Log.isLogging(LoggingConstants.DEBUG))
                    {
                        Log.log(LoggingConstants.DEBUG, "invoking method Add to add item to collection");
                    }

                    addMethod.Invoke(collection, args);

                    if (Log.isLogging(LoggingConstants.DEBUG))
                    {
                        Log.log(LoggingConstants.DEBUG, "item has been added");
                    }
                }

                return(collection);
            }
            else if (typeof(IList).IsAssignableFrom(type))
            {
#if FULL_BUILD
                if (!(type is WebORBArrayCollection) && this is ArrayCollectionType)
                {
                    return(((ArrayCollectionType)this).GetArrayType().adapt(type));
                }
#endif
                IList listObject = (IList)ObjectFactories.CreateServiceObject(type);
                //refCache[ this ] = listObject;

                refCache.AddObject(this, type, listObject);

                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "will populate service objects with elements. got " + size + " items");
                }

                Type componentType = null;

                if (type.IsGenericType)
                {
                    componentType = type.GetGenericArguments()[0];
                }

                for (int i = 0; i < size; i++)
                {
                    if (Log.isLogging(LoggingConstants.DEBUG))
                    {
                        Log.log(LoggingConstants.DEBUG, "element " + i + "   " + arrayObject[i]);
                    }

                    listObject.Add(adaptArrayComponent(arrayObject[i], componentType, refCache));
                }

                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "returning list. list size is " + listObject.Count);
                }

                return(listObject);
            }
#if (FULL_BUILD)
            else if (typeof(Queue).IsAssignableFrom(type))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is Queue");
                }

                Queue qObject = (Queue)ObjectFactories.CreateServiceObject(type);
                //refCache[ this ] = qObject;

                refCache.AddObject(this, type, qObject);

                for (int i = 0; i < size; i++)
                {
                    qObject.Enqueue(adaptArrayComponent(arrayObject[i], null, refCache));
                }

                return(qObject);
            }
            else if (typeof(Stack).IsAssignableFrom(type))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is Stack");
                }

                Stack stackObject = (Stack)ObjectFactories.CreateServiceObject(type);
                //refCache[ this ] = stackObject;

                refCache.AddObject(this, type, stackObject);

                for (int i = 0; i < size; i++)
                {
                    stackObject.Push(adaptArrayComponent(arrayObject[i], null, refCache));
                }

                return(stackObject);
            }
            else if (typeof(SortedList).IsAssignableFrom(type))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is SortedList");
                }

                SortedList sortedList = (SortedList)ObjectFactories.CreateServiceObject(type);
                //refCache[ this ] = sortedList;

                refCache.AddObject(this, type, sortedList);

                for (int i = 0; i < size; i++)
                {
                    object obj = adaptArrayComponent(arrayObject[i], null, refCache);
                    sortedList.Add(obj, obj);
                }

                return(sortedList);
            }
#endif
            else if (type.GetInterface("Iesi.Collections.ISet") != null || type.FullName.Equals("Iesi.Collections.ISet"))
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "type is Iesi.Collections.ISet");
                }

                Object iSet = ObjectFactories.CreateServiceObject(type);

                refCache.AddObject(this, type, iSet);
                MethodInfo addMethod = iSet.GetType().GetMethod("Add", new Type[] { typeof(Object) });
                Object[]   args      = new Object[1];

                for (int i = 0; i < size; i++)
                {
                    args[0] = adaptArrayComponent(arrayObject[i], null, refCache);
                    addMethod.Invoke(iSet, args);
                }

                return(iSet);
            }
#if (FULL_BUILD)
            // this is a hack for Flash Builder 4
            else if (typeof(IDictionary).IsAssignableFrom(type) && (arrayObject == null || arrayObject.Length == 0))
            {
                return(new Hashtable());
            }
#endif
            else
            {
                if (Log.isLogging(LoggingConstants.DEBUG))
                {
                    Log.log(LoggingConstants.DEBUG, "will do default adaptation");
                }

                return(defaultAdapt(refCache));
            }
        }
예제 #7
0
        public IAdaptingType read(FlashorbBinaryReader reader, ParseContext parseContext)
        {
            int refId = reader.ReadVarInteger();

            if ((refId & 0x1) == 0)
            {
                return((IAdaptingType)parseContext.getReference(refId >> 1));
            }

            ClassInfo classInfo = getClassInfo(refId, reader, parseContext);

            Type mappedType = null;

            if (!string.IsNullOrEmpty(classInfo.className))
            {
                mappedType = Types.Types.getServerTypeForClientClass(classInfo.className);
            }

            if (classInfo.externalizable || typeof(IExternalizable).IsAssignableFrom(mappedType))
            {
                Type   type   = Types.Types.getServerTypeForClientClass(classInfo.className);
                object extobj = null;

                if (type != null)
                {
                    extobj = ObjectFactories.CreateServiceObject(type);
                }
                else
                {
                    extobj = ObjectFactories.CreateServiceObject(classInfo.className);
                }

                if (!(extobj is IExternalizable))
                {
                    throw new Exception("object must implement IExternalizable");
                }
                else
                {
                    CacheableAdaptingTypeWrapper wrapper = new CacheableAdaptingTypeWrapper();
                    parseContext.addReference(wrapper);

                    IAdaptingType returnValue = null;
                    extobj = ((IExternalizable)extobj).readExternal(reader, parseContext);

                    if (extobj is IAdaptingType)
                    {
                        returnValue = (IAdaptingType)extobj;
                    }
                    else
                    {
                        returnValue = new ConcreteObject(extobj);
                    }

                    wrapper.setType(returnValue);
                    return(returnValue);
                }
            }
            else
            {
                Dictionary <String, IAdaptingType> props = new Dictionary <String, IAdaptingType>();
                AnonymousObject anonObj     = new AnonymousObject();
                IAdaptingType   returnValue = anonObj;

                if (classInfo.className != null && classInfo.className.Length > 0)
                {
                    returnValue = new NamedObject(classInfo.className, anonObj);
                }

                parseContext.addReference(returnValue);
                int propCount = classInfo.getPropertyCount();

                for (int i = 0; i < propCount; i++)
                {
                    if (Log.isLogging(LoggingConstants.DEBUG))
                    {
                        Log.log(LoggingConstants.DEBUG, "reading object property " + classInfo.getProperty(i));
                    }

                    props[classInfo.getProperty(i)] = RequestParser.readData(reader, parseContext);
                }

                if (classInfo.looseProps)
                {
                    while (true)
                    {
                        string propName = ReaderUtils.readString(reader, parseContext);

                        if (propName == null || propName.Length == 0)
                        {
                            break;
                        }

                        props[propName] = RequestParser.readData(reader, parseContext);
                    }
                }

                anonObj.Properties = props;
                return(returnValue);
            }
        }