Home > Software engineering >  Setting Order in the properties of a class
Setting Order in the properties of a class

Time:10-04

I have a class with 10 properties and I am using reflection to assign values to these properties. My Property names are like,

            private double? _col1;
    public double? Col1
    {
        get
        {
            return _col1;
        }
        set
        {
            _col1 = value;
            OnPropertyChanged("Col1");
        }
    }

    private double? _col2;
    public double? Col2
    {
        get
        {
            return _col2;
        }
        set
        {
            _col2 = value;
            OnPropertyChanged("Col2");
        }
    }

And I am using them like,

        MyClass md = new MyClass();
       
        PropertyInfo[] properties = typeof(MyClass).GetProperties();
         {
             foreach (PropertyInfo property in properties)
                        {
                            double? d1 = from some method();

                            if (property.PropertyType == typeof(Double?))
                            {
                                if (property.GetValue(md) == null)
                                {
                                    property.SetValue(md, d1);                                        
                                }
                            }
                        }

         }

Here I want to use an orderby property name of the class. How ??

               PropertyInfo[] properties = typeof(MyClass).GetProperties();

CodePudding user response:

You need to sort properties alphabetically based on their name.

Add the following line before the foreach() loop

Array.Sort(properties, (x,y)=> x.Name.CompareTo(y.Name)

CodePudding user response:

I think this is an XY problem.

I think what you actually need could be something like this:

public class MyClass
{
    public ObservableCollection<double?> Cols { get; }

    public MyClass(int initialSize)
    {
        if(initialSize < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(initialSize));
        }

        Cols = new(Enumerable.Repeat((double?)null, initialSize));
    }
}

Then, ditching the reflection, you can just use a for loop on the observable collection. The observable collection is great cause it already raises events informing about changes.

MyClass md = new MyClass(2);

for (int i = 0; i < md.Cols.Count; i  )
{
    double? d1 = from some method();

    if (md.Cols[i] is null)
    {
        md.Cols[i] = d1;
    }
}

Then, if someone needs to listen to changes made to Cols, they subscribe to the Cols.CollectionChanged event.

CodePudding user response:

Order of properties in reflection is the same as in their declaration in program code. Property declared first is first. Property declared second is second. I have never tested myself, but in case of partial classes, when a class is split into several files, the situation might be tricky.

how we can ensure that it takes first Col1, then secondly Col2, etc ??

You should use names of the properties for that. Example

if (property.Name == nameof(ClassName.Col1))
{
    property.SetValue(instanceOfClassName, value);
}

Alternative for if is switch statement

switch (property.Name)
{
    case nameof(ClassName.Col1):
        break;
    case nameof(ClassName.Col2):
        break;
}
  •  Tags:  
  • c#
  • Related