public void SetValue(object target, object value) { if (setter == null) { setter = LateBoundDelegateFactory.CreateSet <object>(memberInfo); } setter(target, value); }
public object GetValue(object target) { if (getter == null) { getter = LateBoundDelegateFactory.CreateGet <object>(memberInfo); } return(getter(target)); }
/// <summary> /// Gets the value. /// </summary> /// <param name="target">The target to get the value from.</param> /// <returns>The value.</returns> public object GetValue(object target) { try { if (_getter == null) { _getter = LateBoundDelegateFactory.CreateGet <object>(_memberInfo); } return(_getter(target)); } catch (Exception ex) { throw new JsonSerializationException("Error getting value from '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex); } }
/// <summary> /// Sets the value. /// </summary> /// <param name="target">The target to set the value on.</param> /// <param name="value">The value to set on the target.</param> public void SetValue(object target, object value) { try { if (_setter == null) { _setter = LateBoundDelegateFactory.CreateSet <object>(_memberInfo); } _setter(target, value); } catch (Exception ex) { throw new JsonSerializationException("Error setting value to '{0}' on '{1}'.".FormatWith(CultureInfo.InvariantCulture, _memberInfo.Name, target.GetType()), ex); } }
internal IWrappedCollection CreateWrapper(object list) { if (list is IList && (CollectionItemType == null || !_isCollectionItemTypeNullableType)) { return(new CollectionWrapper <object>((IList)list)); } if (_genericWrapperType == null) { _genericWrapperType = ReflectionUtils.MakeGenericType(typeof(CollectionWrapper <>), CollectionItemType); ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType }); #if !PocketPC _genericWrapperCreator = LateBoundDelegateFactory.CreateMethodHandler(genericWrapperConstructor); #else _genericWrapperCreator = (target, args) => genericWrapperConstructor.Invoke(new[] { args[0] }); #endif } return((IWrappedCollection)_genericWrapperCreator(null, list)); }
internal IWrappedDictionary CreateWrapper(object dictionary) { if (dictionary is IDictionary) { return(new DictionaryWrapper <object, object>((IDictionary)dictionary)); } if (_genericWrapperType == null) { _genericWrapperType = ReflectionUtils.MakeGenericType(typeof(DictionaryWrapper <,>), DictionaryKeyType, DictionaryValueType); ConstructorInfo genericWrapperConstructor = _genericWrapperType.GetConstructor(new[] { _genericCollectionDefinitionType }); #if !PocketPC _genericWrapperCreator = LateBoundDelegateFactory.CreateMethodHandler(genericWrapperConstructor); #else _genericWrapperCreator = (target, args) => genericWrapperConstructor.Invoke(new[] { args[0] }); #endif } return((IWrappedDictionary)_genericWrapperCreator(null, dictionary)); }
private void InitializeContract(JsonContract contract) { JsonContainerAttribute containerAttribute = JsonTypeReflector.GetJsonContainerAttribute(contract.UnderlyingType); if (containerAttribute != null) { contract.IsReference = containerAttribute._isReference; } #if !PocketPC && !NET20 else { DataContractAttribute dataContractAttribute = JsonTypeReflector.GetDataContractAttribute(contract.UnderlyingType); // doesn't have a null value if (dataContractAttribute != null && dataContractAttribute.IsReference) { contract.IsReference = true; } } #endif contract.Converter = ResolveContractConverter(contract.UnderlyingType); if (ReflectionUtils.HasDefaultConstructor(contract.CreatedType, true) || contract.CreatedType.IsValueType) { #if !PocketPC contract.DefaultCreator = LateBoundDelegateFactory.CreateDefaultConstructor(contract.CreatedType); #else ConstructorInfo constructorInfo = ReflectionUtils.GetDefaultConstructor(contract.CreatedType, true); contract.DefaultCreator = () => { if (contract.CreatedType.IsValueType) { return(Activator.CreateInstance(contract.CreatedType)); } return(constructorInfo.Invoke(null)); }; #endif contract.DefaultCreatorNonPublic = (!contract.CreatedType.IsValueType && ReflectionUtils.GetDefaultConstructor(contract.CreatedType) == null); } foreach (MethodInfo method in contract.UnderlyingType.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { // compact framework errors when getting parameters for a generic method // lame, but generic methods should not be callbacks anyway if (method.ContainsGenericParameters) { continue; } Type prevAttributeType = null; ParameterInfo[] parameters = method.GetParameters(); #if !PocketPC && !NET20 if (IsValidCallback(method, parameters, typeof(OnSerializingAttribute), contract.OnSerializing, ref prevAttributeType)) { contract.OnSerializing = method; } if (IsValidCallback(method, parameters, typeof(OnSerializedAttribute), contract.OnSerialized, ref prevAttributeType)) { contract.OnSerialized = method; } if (IsValidCallback(method, parameters, typeof(OnDeserializingAttribute), contract.OnDeserializing, ref prevAttributeType)) { contract.OnDeserializing = method; } if (IsValidCallback(method, parameters, typeof(OnDeserializedAttribute), contract.OnDeserialized, ref prevAttributeType)) { contract.OnDeserialized = method; } #endif if (IsValidCallback(method, parameters, typeof(OnErrorAttribute), contract.OnError, ref prevAttributeType)) { contract.OnError = method; } } }