-3

I have a simple class defined like this:

public class StickColumns
{
    public string wellname { get; set; }
    public double WellLength { get; set; }
} 

In the code, I get some data as list<double> perfdepth; assume this is perfdepth1,perfdepth2,perfdepth3. Of course, this list is dynamic hence, I wouldnt know beforehand to change my class definition to:

 public class StickColumns
 {
     public string wellname { get; set; }
     public double WellLength { get; set; }
     public double perfdepth1 { get; set; }
     public double perfdepth2 { get; set; }
     public double perfdepth3 { get; set; }
 } 

Can these new members be created during run time? The reason why I think I would need this is because of data binding in WPF. Eventually I need to display "point series"; Perfdepth1 as one series, perfdepth2 as another series and so on, i.e, dynamic number of Perfdepths.

If there is a simpler way to do it, I am all ears!

user7157732
  • 347
  • 1
  • 8
  • 24
  • 4
    While this is certainly possible, this is a much harder way to go about this. I suggest another approach, maybe use a List? – FailedUnitTest Dec 12 '16 at 18:59
  • Have you looked at http://stackoverflow.com/questions/15819720/dynamically-add-c-sharp-properties-at-runtime? – Johnny Dec 12 '16 at 18:59
  • 1
    Please, have a look at `ExpandoObject` https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject(v=vs.110).aspx – Dmitry Bychenko Dec 12 '16 at 19:00
  • @FailedUnitTest: Each member is to be used as series.Valuememberpath in the WPF data binding that's why. List will not work there. – user7157732 Dec 12 '16 at 19:14
  • @user7157732 chances are for everything your attempting to do, you can substitute with a dictionary – johnny 5 Dec 12 '16 at 19:25

1 Answers1

3

You might just want to use the dynamic type with ExpandoObject..

dynamic stickColumns = new ExpandoObject();
stickColumns.wellName  = "Something";
stickColumns.perfdepth1 = "Something Else";

It has its drawbacks as it does mean you end up with runtime errors etc... but it can be useful for this type of scenario.

gmn
  • 4,199
  • 4
  • 24
  • 46
  • thnx. Can this be then used in an observable collection? – user7157732 Dec 12 '16 at 19:09
  • well it would be observablecollection or object but would contain the data thats in stick columns. I think it would work. – gmn Dec 12 '16 at 19:11
  • Buddy I am confused on how to use this. In your example, Are you assuming that "perfdepth1" member already exists in the class? and you are just populating the value? – user7157732 Dec 12 '16 at 20:04
  • No. You need to look up ExpandoObject on msdn for a good description. Basically it's an object that allows you to use it to dynamically add class members on the fly. Also take a look at anonymous classes. Both can be assigned to dynamic and both can be used to dynamically construct objects. – gmn Dec 12 '16 at 21:17
  • https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/ – gmn Dec 12 '16 at 21:19