예제 #1
0
            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                JsonProperty property = base.CreateProperty(member, memberSerialization);

                if (property.PropertyName == "Children")
                {
                    property.ShouldSerialize = instance =>
                    {
                        if (instance is IOptionallySerialiseChildren)
                        {
                            // If resource model is under replacements,
                            // always serialize children.
                            if (Apsim.Ancestor <Replacements>(instance as IModel) != null)
                            {
                                return(true);
                            }
                            return((instance as IOptionallySerialiseChildren).DoSerialiseChildren);
                        }
                        return(true);
                    };
                }
                else if (typeof(ModelCollectionFromResource).IsAssignableFrom(member.DeclaringType))
                {
                    property.ShouldSerialize = instance =>
                    {
                        var xmlIgnore  = member.GetCustomAttribute <XmlIgnoreAttribute>();
                        var jsonIgnore = member.GetCustomAttribute <JsonIgnoreAttribute>();
                        if (xmlIgnore != null || jsonIgnore != null)
                        {
                            return(false);
                        }

                        // If this property has a description attribute, then it's settable
                        // from the UI, in which case it should always be serialized.
                        var description = member.GetCustomAttribute <DescriptionAttribute>();
                        if (description != null)
                        {
                            return(true);
                        }

                        // If the model is under a replacements node, then serialize everything.
                        ModelCollectionFromResource resource = instance as ModelCollectionFromResource;
                        if (Apsim.Ancestor <Replacements>(resource) != null)
                        {
                            return(true);
                        }

                        // Otherwise, only serialize if the property is inherited from
                        // Model or ModelCollectionFromResource.
                        return(member.DeclaringType.IsAssignableFrom(typeof(ModelCollectionFromResource)));
                    };
                }

                return(property);
            }
예제 #2
0
            protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
            {
                JsonProperty property = base.CreateProperty(member, memberSerialization);

                if (property.PropertyName == "Children")
                {
                    property.ShouldSerialize = instance =>
                    {
                        if (instance is IOptionallySerialiseChildren opt)
                        {
                            return(opt.DoSerialiseChildren);
                        }

                        return(true);
                    };
                }
                else if (typeof(ModelCollectionFromResource).IsAssignableFrom(member.DeclaringType))
                {
                    property.ShouldSerialize = instance =>
                    {
                        var link       = member.GetCustomAttribute <LinkAttribute>();
                        var jsonIgnore = member.GetCustomAttribute <JsonIgnoreAttribute>();
                        if (link != null || jsonIgnore != null)
                        {
                            return(false);
                        }

                        // If this property has a description attribute, then it's settable
                        // from the UI, in which case it should always be serialized.
                        var description = member.GetCustomAttribute <DescriptionAttribute>();
                        if (description != null)
                        {
                            return(true);
                        }

                        // If the model is under a replacements node, or if the model doesn't have
                        // a resource name (ie if it's a prototype), then serialize everything.
                        ModelCollectionFromResource resource = instance as ModelCollectionFromResource;
                        if (resource.FindAncestor <Replacements>() != null || string.IsNullOrEmpty(resource.ResourceName))
                        {
                            return(true);
                        }

                        // Otherwise, only serialize if the property is inherited from
                        // Model or ModelCollectionFromResource.
                        return(member.DeclaringType.IsAssignableFrom(typeof(ModelCollectionFromResource)));
                    };
                }

                return(property);
            }
예제 #3
0
        private static void ChangeVariableValue(IVariable variable, string value)
        {
            variable.Value = ReflectionUtilities.StringToObject(variable.DataType, value);
            if (variable is VariableComposite composite)
            {
                IModel model = composite.Variables.FirstOrDefault(v => v is VariableObject obj && obj.Value is IModel)?.Value as IModel;
                if (model != null)
                {
                    ModelCollectionFromResource resourceModel = model.FindAncestor <ModelCollectionFromResource>();
                    if (resourceModel != null)
                    {
                        resourceModel.ResourceName = null;
                    }

                    if (model.Parent is Manager manager)
                    {
                        manager.RebuildScriptModel();
                    }
                }
            }
        }