private bool haveMatchingData(Type formalArgType) { if (formalArgType.IsArray && canBeArray()) { return(true); } else { BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; MemberInfo[] members = formalArgType.GetMembers(flags); /*FieldInfo[] fields = formalArgType.GetFields( flags ); * PropertyInfo[] props = formalArgType.GetProperties( flags ); * * foreach( FieldInfo field in fields ) * if( !properties.Contains( field.Name ) ) * return false; * * foreach( PropertyInfo property in props ) * if( !properties.Contains( property.Name ) ) * return false; */ foreach (MemberInfo member in members) { IMemberRenameAttribute[] renamers = (IMemberRenameAttribute[])member.GetCustomAttributes(typeof(IMemberRenameAttribute), true); string memberName = member.Name; if (renamers.Length > 0) { memberName = renamers[0].GetClientName(formalArgType, member); } if (!properties.Contains(memberName)) { #if ( FULL_BUILD ) SerializationConfigHandler serializationConfig = (SerializationConfigHandler)ORBConfig.GetInstance().GetConfig("weborb/serialization"); if (serializationConfig != null && serializationConfig.Keywords.Contains(memberName)) { memberName = serializationConfig.PrefixForKeywords + memberName; return(properties.Contains(memberName)); } #endif return(false); } } return(true); } }
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); } }
public override void write(object obj, IProtocolFormatter writer) { //Log.log( ORBConstants.INFO, "ObjectWriter.write.begin: " + writer.BaseStream.Length ); #if (FULL_BUILD) if (obj is IRemote) { remoteReferenceWriter.write(RemoteReferenceObject.createReference(obj), writer); return; } SerializationConfigHandler serializationConfig = (SerializationConfigHandler)ORBConfig.GetInstance().GetConfig("weborb/serialization"); if (!configured) { //serializePrivate = ThreadContext.getORBConfig().serializePrivateFields; if (serializationConfig != null) { serializePrivate = serializationConfig.SerializePrivateFields; } configured = true; } #endif Type objectClass = obj.GetType(); /* * if( obj is IAutoUpdate ) * { * //if( Weborb.Util.License.LicenseManager.GetInstance().IsStandardLicense() && !NetUtils.RequestIsLocal( ThreadContext.currentRequest() ) ) * // throw new Exception( "auto-update is disabled, this feature is available in WebORB Professional Edition" ); * * string id = Guid.NewGuid().ToString(); * objectFields[ "_orbid_" ] = id; * ThreadContext.currentHttpContext().Cache.Insert( id, new AutoUpdateObjectWrapper( ((IAutoUpdate) obj).GetUpdateHandler(), obj ) ); * } */ ClassDefinition classDef; cachedClassDefs.TryGetValue(objectClass, out classDef); Dictionary <String, Object> objectFields = new Dictionary <String, Object>(); if (classDef == null) { string className = objectClass.IsGenericType && objectClass.FullName != null ? objectClass.FullName.Substring(0, objectClass.FullName.IndexOf("`")) : objectClass.FullName; string clientSideMapping = GetClientClass(className); if (clientSideMapping != null) { // if( Log.isLogging( LoggingConstants.DEBUG ) ) // Log.log( LoggingConstants.DEBUG, "serializing a named object with client side mapping " + clientSideMapping ); className = clientSideMapping; } // Commented out this code in 4.0.0.4. This code does not make sense anymore // For one these mappings exist between ServiceNames (or Destination names) and // classes. A service class should not be serialized back to the client. // If it is serialized, user should use class mapping for that. //else //{ // className = ServiceRegistry.GetReverseMapping( className ); // if( Log.isLogging( LoggingConstants.DEBUG ) ) // Log.log( LoggingConstants.DEBUG, "serializing object " + className ); //} classDef = getClassDefinition(className, obj); lock (_sync) { cachedClassDefs[objectClass] = classDef; } // TODO: remove this try/catch //try //{ //cachedClassDefs[ objectClass ] = classDef; // } //catch ( Exception e ) // { //if ( Log.isLogging( LoggingConstants.ERROR ) ) // Log.log( LoggingConstants.ERROR, e ); //System.Diagnostics.Debugger.Launch(); //} } else { if (Log.isLogging(LoggingConstants.DEBUG)) { Log.log(LoggingConstants.DEBUG, "serializing using cached Class Def " + classDef.ClassName); } } Dictionary <string, MemberInfo> members = classDef.Members; foreach (KeyValuePair <string, MemberInfo> member in members) { Object val = null; if (member.Value is PropertyInfo) { try { val = ((PropertyInfo)member.Value).GetValue(obj, null); } catch (Exception exception) { if (Log.isLogging(LoggingConstants.ERROR)) { Log.log(LoggingConstants.ERROR, "Unable to retrieve property/field value from an instance of " + classDef.ClassName + ". Value will be set to null. Property name is " + member.Value.Name); } if (Log.isLogging(LoggingConstants.EXCEPTION)) { Log.log(LoggingConstants.EXCEPTION, exception); } } } else { val = ((FieldInfo)member.Value).GetValue(obj); } String memberName = member.Key; #if (FULL_BUILD) if (serializationConfig != null && serializationConfig.Keywords.Contains(memberName)) { memberName = serializationConfig.PrefixForKeywords + memberName; } #endif objectFields[memberName] = val; } onWriteObject(obj, classDef.ClassName, objectFields, writer); }
public void write(object obj, IProtocolFormatter writer) { ITypeWriter typeWriter; if (obj is IDictionary) { typeWriter = MessageWriter.getWriter(typeof(IDictionary), writer, false); typeWriter.write(obj, writer); return; } /* * if( obj is Array ) * { * base.write( obj, writer ); * return; * }*/ object[] array = null; #if (FULL_BUILD) if (VectorUtils.IsVector(obj)) { Type objectType = obj.GetType(); Type elementType = objectType.GetGenericArguments()[0]; Type vectorType = typeof(V3VectorWriter <>).MakeGenericType(elementType); typeWriter = (ITypeWriter)vectorType.GetConstructor(new Type[0]).Invoke(new object[0]); typeWriter.write(obj, writer); return; } if (obj is ICollection) { ICollection coll = (ICollection)obj; SerializationConfigHandler serializationConfig = (SerializationConfigHandler)ORBConfig.GetInstance().GetConfig("weborb/serialization"); if (!serializationConfig.LegacyCollectionSerialization && !(obj is IWebORBArray)) { typeWriter = MessageWriter.getWriter(typeof(IWebORBArrayCollection), writer, false); typeWriter.write(new WebORBArrayCollection(coll), writer); return; } else { array = new object[coll.Count]; coll.CopyTo(array, 0); } } else { #endif IEnumerable collection = (IEnumerable)obj; IEnumerator enumerator = collection.GetEnumerator(); List <Object> arrayList = new List <Object>(); while (enumerator.MoveNext()) { arrayList.Add(enumerator.Current); } array = arrayList.ToArray(); #if (FULL_BUILD) } #endif typeWriter = MessageWriter.getWriter(array.GetType(), writer, false); typeWriter.write(array, writer); //base.write( array, writer ); }
protected virtual ClassDefinition getClassDefinition(String className, Object obj) { #if (FULL_BUILD) SerializationConfigHandler serializationConfig = (SerializationConfigHandler)ORBConfig.GetInstance().GetConfig("weborb/serialization"); #endif ClassDefinition classDef = new ClassDefinition(); Type objectClass = obj.GetType(); IPropertyExclusionAttribute[] propExclusion = (IPropertyExclusionAttribute[])objectClass.GetCustomAttributes(typeof(IPropertyExclusionAttribute), true); while (!Object.ReferenceEquals(objectClass, typeof(object))) { BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static; if (serializePrivate) { flags |= BindingFlags.NonPublic; } PropertyInfo[] props = objectClass.GetProperties(flags); for (int i = 0; i < props.Length; i++) { if (!props[i].CanRead) { continue; } bool skipProperty = false; IPropertyExclusionAttribute[] propExclusionAttr = (IPropertyExclusionAttribute[])props[i].GetCustomAttributes(typeof(IPropertyExclusionAttribute), false); if (propExclusionAttr.Length > 0) { continue; } foreach (IPropertyExclusionAttribute attr in propExclusion) { if (attr.ExcludeProperty(obj, props[i].Name)) { skipProperty = true; break; } } if (skipProperty) { continue; } if (props[i].GetGetMethod().IsStatic) { if (!cacheStaticField(className, props[i].Name)) { continue; } } IMemberRenameAttribute[] renamers = (IMemberRenameAttribute[])props[i].GetCustomAttributes(typeof(IMemberRenameAttribute), true); string memberName; if (renamers.Length > 0) { memberName = renamers[0].GetClientName(objectClass, props[i]); } else { memberName = PropertyRenaming.GetRenamingRule(objectClass, props[i].Name); } #if (FULL_BUILD) if (serializationConfig != null && serializationConfig.Keywords.Contains(memberName)) { memberName = serializationConfig.PrefixForKeywords + memberName; } #endif if (!classDef.ContainsMember(memberName)) { //ITypeWriter typeWriter = MessageWriter.getWriter( props[ i ].PropertyType, null, false ); classDef.AddMemberInfo(memberName, props[i]); } } flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static; if (serializePrivate) { flags |= BindingFlags.NonPublic; } FieldInfo[] fields = objectClass.GetFields(flags); //Log.log( ORBConstants.INFO, "ObjectWriter.write.before writing fields: " + writer.BaseStream.Length ); //if( Log.isLogging( LoggingConstants.DEBUG ) ) // Log.log( LoggingConstants.DEBUG, "number of fields: " + fields.Length ); for (int i = 0; i < fields.Length; i++) { if (fields[i].IsLiteral || fields[i].IsNotSerialized || fields[i].FieldType == typeof(IntPtr)) { continue; } string fieldName = fields[i].Name; if (fields[i].IsStatic) { if (!cacheStaticField(className, fieldName)) { continue; } } IMemberRenameAttribute[] renamers = (IMemberRenameAttribute[])fields[i].GetCustomAttributes(typeof(IMemberRenameAttribute), true); string memberName; if (renamers.Length > 0) { memberName = renamers[0].GetClientName(objectClass, fields[i]); } else { memberName = fields[i].Name; } #if (FULL_BUILD) if (serializationConfig != null && serializationConfig.Keywords.Contains(memberName)) { memberName = serializationConfig.PrefixForKeywords + memberName; } #endif if (!classDef.ContainsMember(memberName)) { //ITypeWriter typeWriter = MessageWriter.getWriter( fields[ i ].FieldType, null, false ); classDef.AddMemberInfo(memberName, fields[i]); } } objectClass = objectClass.BaseType; } IDictionary classStaticsCache = ThreadContext.currentWriterCache(); if (classStaticsCache != null) { classStaticsCache.Remove(className); } classDef.ClassName = className; return(classDef); }