Child is Unable to inherit Index

107 views Asked by At

Updated the code..

public class UpdateProductTemplate
{
    [Index(0)]
    public string ProductID { get; set; }
    [Index(2)]
    public string ProductName { get; set; }
    [Index(3)]
    public string ShortDescription { get; set; }
}

public class UpdateProductTemplateWithPrice: UpdateProductTemplate
{
    // For PRICE Updates!
    [Index(4)]
    public double? SitePrice { get; set; }
    [Index(5)]
    public double? ListPrice { get; set; }
    [Index(6)]
    public double? WebSitePrice { get; set; }
}


List<UpdateProductTemplateWithPrice> Products = new List<UpdateProductTemplateWithPrice>() {
            new UpdateProductTemplateWithPrice
            {
                ProductID = "123",
                ProductName = "Test Product",
                ShortDescription = "Test",
                SitePrice = 123.00,
                ListPrice = 123.00,
                WebSitePrice = 123.00
            }
        };

Type type = products.ToArray()[0].GetType();

PropertyInfo[] properties = type.GetProperties();

for (int i = 0; i < properties.Length; i++)
{
    var cell = headerRow.CreateCell(i);
    cell.SetCellValue(properties[i].Name);
}

When I do GetProperties(), it ends up with the SitePrice first on the array. I don't want that I want ProductID to be first in the properties array...

I already did placed a higher index value in the child class, and expected ProductID to be first when I do GetProperties(), but it didn't.

2

There are 2 answers

0
PashaW On

based on the question and sample, created a simple Index attribute as below.

public class Index : Attribute
{            
    public Index(int index) { _index = index; }

    private int _index;
    public int Value { get { return _index; } }
}   

I think the simplest answer would be to call the Sort method with a custom comparer. Like this:

 List<PropertyInfo> properties = new List<PropertyInfo>(typeof(UpdateProductTemplateWithPrice).GetProperties());

        properties.Sort((PropertyInfo a, PropertyInfo b) =>
        {
            Index indexA = a.GetCustomAttribute<Index>();
            Index indexB = b.GetCustomAttribute<Index>();

            if ((indexA is null) || (indexB is null))
                return 0;

            if (indexA.Value > indexB.Value)
                return 1;
            if (indexA.Value < indexB.Value)
                return -1;
            if (indexA.Value == indexB.Value)
                return 0;

            return 0;
        });

That will return the properties in the expected order.

1
Qiang Fu On

You could try this:
Install package Newtonsoft.Json

    public class UpdateProductTemplate
    {
        [JsonPropertyOrder(0)]
        public string ProductID { get; set; }
        [JsonPropertyOrder(1)]
        public int CompanyID { get; set; }
        [JsonPropertyOrder(2)]
        public string ProductName { get; set; }
        [JsonPropertyOrder(3)]
        public string ShortDescription { get; set; }
    }
    public class UpdateProductTemplateWithPrice : UpdateProductTemplate
    {
        // For PRICE Updates!
        [JsonPropertyOrder(4)]
        public double? SitePrice { get; set; }
        [JsonPropertyOrder(5)]
        public double? ListPrice { get; set; }
        [JsonPropertyOrder(6)]
        public double? WebSitePrice { get; set; }
    }
List<UpdateProductTemplateWithPrice> Products = new List<UpdateProductTemplateWithPrice>() {
            new UpdateProductTemplateWithPrice
            {
                ProductID = "123",
                ProductName = "Test Product",
                ShortDescription = "Test",
                SitePrice = 123.00,
                ListPrice = 123.00,
                WebSitePrice = 123.00
            }
        };

var product = Products[0];

var json = System.Text.Json.JsonSerializer.Serialize(product,
    new JsonSerializerOptions { WriteIndented = true }
);
Console.WriteLine(json);

var jsonData = (JObject)JsonConvert.DeserializeObject(json);
foreach (var item in jsonData)
{
    Console.WriteLine(item.Key);
}

OutPut
enter image description here