-2

I'm trying to get the highest number of all propreties in my class:

public class aClass{ 

        public int PropA{ get; set; } = 1;
        public int PropB{ get; set; } = 18;
        public int PropC{ get; set; } = 25; 
}  

Here's my code:

public int GetMaxConfiguratableColumns()
        {
            int _HighestNumber = 0;
            PropertyInfo[] _Info = this.GetType().GetProperties(); 
            foreach(PropertyInfo _PropretyInfo in _Info)
            {
                //I'm lost here!!!!!!!
            }
            return _HighestNumber;
        }

Any suggestions? Thanks!

Mikael Sauriol Saad
  • 112
  • 1
  • 2
  • 10

4 Answers4

1

If it doesn't have to use reflection, might I suggest something like this?

public class aClass
{
    public int PropA { get; set; } = 1;
    public int PropB { get; set; } = 18;
    public int PropC { get; set; } = 25;

    public int GetMaxConfiguratableColumns()
    {
        return new List<int> {this.PropA, this.PropB, this.PropC}.Max();
    }
}
Nate Anderson
  • 690
  • 4
  • 20
0

use _PropretyInfo.GetValue(myObject) to get the value of your current property then store it in temp variable and iterate and compare the temp value with the rest

ma1169
  • 659
  • 1
  • 8
  • 26
0

I think you would wind up with something like this:

        int highestNumber = 0;      

        PropertyInfo[] info = this.GetType().GetProperties(); 
        foreach(PropertyInfo propInfo in info)
        {
            if (propInfo.PropertyType == typeof(int))
            {
                int propValue = (int)(propInfo.GetValue(this, null));
                if (propValue > highestNumber) {
                    highestNumber = propValue;
                }
            }
        }

        return highestNumber;
Paddy
  • 33,309
  • 15
  • 79
  • 114
0

Got my answer:

 public int GetMaxConfiguratableColumns()
        {
            PaymentHeader PaymentHeader = new PaymentHeader();
            int max = typeof(PaymentHeader)
                .GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
                .Select(x => (int)x.GetValue(PaymentHeader)).Max();

            return max;
        } 

To whoever needs this.

Mikael Sauriol Saad
  • 112
  • 1
  • 2
  • 10