C# add properties to an existing class at runtime

325 Views Asked by At

I hava a class like;

    public class MainClass
{
    public class Class1
    {
        public string Property1 { get; set; }
        public string Property2 { get; set; }
        public int Property3 { get; set; }
    }
    public class Class2
    {
        public Class2()
        {
            List1 = new List<Class1>();
        }
        public string Property4 { get; set; }
        public List<Class1> List1 { get; set; }
    }
    public List<Class2> List2 { get; set; }
    public string Property5 { get; set; }
    public bool Property6 { get; set; }
}

I want to add parameters in MainClass at runtime

    public class MainClass
{
    ...
    public string Parameter1 { get; set; }
    public string Parameter2 { get; set; }
    ...
}

or create new class with inheritance existing class.

    public class NewClass : MainClass
{
    public string Parameter1 { get; set; }
    public string Parameter2 { get; set; }
}

or something else. These's my ideas. Unfortunately, using a dictionary will not solve my problem because I am using xtrareport and I must show a fieldlist.

1

There are 1 best solutions below

1
ArBR On

You could bind your ExtraReport to a DataSet instead of binding to collection of a strong typed objects (which can't be changed at execution-time). So if you add new fields to the data-source it’s a matter of changing the report for showing that new fields. If you create a Stored Procedure to retrieve data, you can even change the query without having to compile the Application. You can iterate through the DataTable object of the DataSet to read all the fields & place them dynamically using XRBinding DevExpress Objects.

See: https://www.devexpress.com/Support/Center/Question/Details/T408680/binding-datatable-object-to-xtrareport-xrtable