public override bool IsCompatibleWith(object value)
        {
            var valueType = value.GetType();

            if (valueType.IsValueType && TypeCacheUtils.IsAnUserStructType(valueType))
            {
                return(true);
            }
            return(false);
        }
Example #2
0
        object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            StateObjectPointer pointer = (StateObjectPointer)existingValue;

            reader.Read();
            var propertyName = reader.Value.ToString();

            if (propertyName == "p")
            {
                var uniqueIdforReferencedObject = reader.ReadAsString();
                pointer._targetUniqueId = uniqueIdforReferencedObject;
                return(pointer);
            }
            if (propertyName == "d")
            {
                reader.Read();
                var value = serializer.Deserialize(reader);
                ((StateObjectPointerReferenceSerializable)pointer).SuperTarget = value;
                return(pointer);
            }
            if (propertyName == "v" || propertyName == "u")
            {
                reader.Read();
                var type = TypeCacheUtils.GetType(reader.ReadAsString());
                if (type == typeof(object))
                {
                    return(new StateObjectPointerReferenceSuperValue());
                }
                else
                {
                    var    str   = reader.ReadAsString();
                    object value = null;
                    if (type.IsEnum)
                    {
                        value = Enum.Parse(type, str, true);
                    }
                    else if (typeof(decimal) == type)
                    {
                        value = decimal.Parse(str, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture);
                    }
                    else if (typeof(double) == type)
                    {
                        value = double.Parse(str, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        if (TypeCacheUtils.IsAnUserStructType(type))
                        {
                            value = JsonConvert.DeserializeObject(str, type);
                        }
                        else
                        {
                            value = System.Convert.ChangeType(str, type);
                        }
                    }

                    ((StateObjectPointerReferenceSuperValue)pointer).SuperTarget = value;
                    return(pointer);
                }
            }
            if (propertyName == "s")
            {
                //TODO surrogate
                var uniqueIDSurrogate = reader.ReadAsString();
                var superSurrogate    = pointer as StateObjectPointerReferenceSuperSurrogate;
                superSurrogate._targetSurrogateUniqueId = uniqueIDSurrogate;
                return(pointer);
            }

            throw new NotSupportedException();
        }
Example #3
0
        /// <summary>
        /// Gets a Reference for an specified value and property.
        /// </summary>
        /// <param name="propEx">The property associated to the value being set.</param>
        /// <param name="parentInstance">The parent instance of the property</param>
        /// <param name="newValueObject">The new value for the property.</param>
        /// <returns></returns>
        private static StateObjectPointerReference GetReference(PropertyInfoEx propEx, IStateObject parentInstance, object newValueObject)
        {
            StateObjectPointerReference reference = null;
            string name        = GetPropertyName(propEx);
            var    relativeUid = UniqueIDGenerator.GetPointerRelativeUniqueID(parentInstance, name);

            AddDependent(parentInstance, UniqueIDGenerator.REFERENCEPrefix + name);
            //Let's try to cast the element to IStateObject
            var newValue = newValueObject as IStateObject;

            //It's an IList, IDisctionary or an IStateObject or the Value is an IStateObject
            if (propEx.isStateObject || propEx.isAnIList || propEx.isAnIDictionary || newValue != null)
            {
                reference = new StateObjectPointerReference();
            }
            //It's a value type or type.
            else if (propEx.isNonStateObjectFixedType)
            {
                reference = new StateObjectPointerReferenceSuperValue();
            }
            //It's declared as an Object.
            else if (propEx.isObjectPropertyType)
            {
                var newValueType = newValueObject.GetType();
                //If the Value is an IStateObject
                if (newValueType.IsValueType || newValueType == typeof(string) || newValueType == typeof(Type))
                {
                    if (TypeCacheUtils.IsAnUserStructType(propEx.prop.PropertyType))
                    {
                        reference = new StateObjectPointerReferenceSuperStruct();
                    }
                    else
                    {
                        reference = new StateObjectPointerReferenceSuperValue();
                    }
                }
                //It's a registered surrogated.
                else if (SurrogatesDirectory.IsSurrogateRegistered(newValueType))
                {
                    reference = new StateObjectPointerReferenceSuperSurrogate();
                }
                else
                {
                    reference = new StateObjectPointerReferenceSerializable();
                }
            }
            else             //Not supported type
            {
                var stackTrace = new StackTrace(3, true);
                throw new InvalidOperationException(
                          "LazyBehaviour::ProcessSetter Property with reference attribute receive a value of unsupported type.\r\n" +
                          "Additional Info: \r\n" +
                          "Value Type: " + propEx.prop.PropertyType.FullName + "\r\n" +
                          "Property Type: " + GetPropertyName(propEx) + "\r\n" +
                          "Error Location: " + stackTrace);
            }
            //If it has the StateManagement attribute set as ServerOnly, then let's respect that.
            if (propEx.stateManagementAttribute == StateManagementValues.ServerOnly)
            {
                StateManager.Current.isServerSideOnly.Add(reference);
            }
            //Let's set the UniqueID to the new reference.
            StateObjectPointer.AssignUniqueIdToPointer(parentInstance, relativeUid, reference);
            //Let's return the pointer of the desired type.
            return(reference);
        }