private void LoadTabFields(XmlNode tab, string idPrefix)
        {
            string       tabName            = tab.Attributes[NameAtt].Value;
            XmlAttribute hideOnAddAttribute = tab.Attributes[HideOnAddAtt];

            if (hideOnAddAttribute != null && hideOnAddAttribute.Value.ToLower() == "true")
            {
                hideOnAddTabs.Add(tabName);
            }

            List <Field> tabFields;

            if (!tabs.TryGetValue(tabName, out tabFields))
            {
                tabFields     = new List <Field>();
                tabs[tabName] = tabFields;
            }

            DynamicPropertyManager <DataItem> propertyManager = new DynamicPropertyManager <DataItem>();

            foreach (XmlNode field in tab)
            {
                Action <Field> callback = (cF) => {
                    allFields[cF.Id]    = cF;
                    allAppFields[cF.Id] = cF;
                    bool isNested = string.IsNullOrEmpty(idPrefix) ? cF.IsNested : cF.Id.Replace(idPrefix, "").Contains(".");
                    if (!isNested)
                    {
                        tabFields.Add(cF);
                    }
                    propertyManager.Properties.Add(
                        DynamicPropertyManager <DataItem> .CreateProperty <DataItem, string>(
                            cF.Id,
                            t => {
                        Field f = t.GetField(cF.Id);
                        return(f != null ? f.GetValue() : null);
                    },
                            null
                            ));
                };

                Field newField = Field.CreateField(field, callback, idPrefix);
                if (newField == null)
                {
                    LogError(Properties.Resources.Error_ElementParseError.Replace("%v1%", field.Name).Replace("%v2%", field.Attributes[IdAtt].Value));
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            //For Question
            //https://stackoverflow.com/questions/46888732/how-to-serialize-runtime-added-properties-to-json

            var obj = new object();

            //Add prop to instance
            int propVal     = 0;
            var propManager = new DynamicPropertyManager <object>(obj);

            propManager.Properties.Add(
                DynamicPropertyManager <object> .CreateProperty <object, int>(
                    "Value", t => propVal, (t, y) => propVal = y, null));

            propVal = 3;


            var obj2 = new object();
            //Add prop to instance
            int propVal2     = 1;
            var propManager2 = new DynamicPropertyManager <object>(obj2);

            propManager2.Properties.Add(
                DynamicPropertyManager <object> .CreateProperty <object, int>(
                    "Value2", t => propVal2, (t, y) => propVal2 = y, null));

            propVal2 = 7;

            var settings = new JsonSerializerSettings
            {
                ContractResolver = new TypeDescriptorContractResolver(),
            };

            //Serialize object here
            var json = JsonConvert.SerializeObject(obj, Formatting.Indented, settings);

            Console.WriteLine(json);

            var json2 = JsonConvert.SerializeObject(obj2, Formatting.Indented, settings);


            Console.WriteLine(json2);

            Console.ReadKey();
        }
    public void CanDumpObjectWithDynamicallyAddedProperties()
    {
        // Arrange
        var input = new MyType {
            Name = "Hello world"
        };

        using var manager = new DynamicPropertyManager <MyType>(input);
        manager.Properties.Add(DynamicPropertyManager.CreateProperty <MyType, string>("Name2", _ => "Name 2", null));
        manager.Properties.Add(DynamicPropertyManager.CreateProperty <MyType, string>("Name3", _ => "Name 3", null));

        // Act
        var actual = input.Dump();

        // Assert
        actual.Should().Be(@"{
    ""Name"": ""Hello world"" [System.String],
    ""Age"": 0 [System.Int32],
    ""Weight"": 0 [System.Double],
    ""Name2"": ""Name 2"" [System.String],
    ""Name3"": ""Name 3"" [System.String]
} [CrossCutting.Utilities.ObjectDumper.Tests.Helpers.MyType]");
    }