获取另一个类的属性的值

时间:2019-06-25 18:24:30

标签: c# asp.net .net

我在ASP .NET中遇到C#问题。我从JSON格式的API中获取了一些代码。我已经创建了一个存储数据的对象。我不知道如何在类中获得类的属性值(通过反射)。

我尝试进行反射(如下面的第一段代码所示),但这仅使我降低了一步。

public class AdditionalServices
{
    public string id { get; set; }
}

public class ImpliedWarranty
{
    public string id { get; set; }
}

public class ReturnPolicy
{
    public string id { get; set; }
}

public class Warranty
{
    public string id { get; set; }
}

public class AfterSalesServices
{
    public ImpliedWarranty impliedWarranty { get; set; }
    public ReturnPolicy returnPolicy { get; set; }
    public Warranty warranty { get; set; }
}

public class Category
{
    public string id { get; set; }
}

public class Item
{
    public string text { get; set; }
}

public class CompatibilityList
{
    public List<Item> items { get; set; }
}

public class Contact
{
    public string id { get; set; }
}

public class ShippingRates
{
    public string id { get; set; }
}

public class Delivery
{
    public string additionalInfo { get; set; }
    public string handlingTime { get; set; }
    public DateTime shipmentDate { get; set; }
    public ShippingRates shippingRates { get; set; }
}

public class Item2
{
    public string type { get; set; }
}

public class Section
{
    public List<Item2> items { get; set; }
}

public class Description
{
    public List<Section> sections { get; set; }
}

public class External
{
    public string id { get; set; }
}

public class Image
{
    public string url { get; set; }
}

public class Location
{
    public string city { get; set; }
    public string countryCode { get; set; }
    public string postCode { get; set; }
    public string province { get; set; }
}

public class RangeValue
{
    public string from { get; set; }
    public string to { get; set; }
}

public class Parameter
{
    public string id { get; set; }
    public RangeValue rangeValue { get; set; }
    public List<string> values { get; set; }
    public List<string> valuesIds { get; set; }
}

public class Payments
{
    public string invoice { get; set; }
}

public class Product
{
    public string id { get; set; }
}

public class Promotion
{
    public bool bold { get; set; }
    public bool departmentPage { get; set; }
    public bool emphasized { get; set; }
    public bool emphasizedHighlightBoldPackage { get; set; }
    public bool highlight { get; set; }
}

public class Publication
{
    public string duration { get; set; }
    public DateTime endingAt { get; set; }
    public DateTime startingAt { get; set; }
    public string status { get; set; }
    public string endedBy { get; set; }
}

public class Price
{
    public string amount { get; set; }
    public string currency { get; set; }
}

public class MinimalPrice
{
    public string amount { get; set; }
    public string currency { get; set; }
}

public class StartingPrice
{
    public string amount { get; set; }
    public string currency { get; set; }
}

public class SellingMode
{
    public string format { get; set; }
    public Price price { get; set; }
    public MinimalPrice minimalPrice { get; set; }
    public StartingPrice startingPrice { get; set; }
}

public class SizeTable
{
    public string id { get; set; }
}

public class Stock
{
    public int available { get; set; }
    public string unit { get; set; }
}

public class Error
{
    public string code { get; set; }
    public string details { get; set; }
    public string message { get; set; }
    public string path { get; set; }
    public string userMessage { get; set; }
}

public class Validation
{
    public List<Error> errors { get; set; }
    public DateTime validatedAt { get; set; }
}

public class RootObject
{
    public AdditionalServices additionalServices { get; set; }
    public AfterSalesServices afterSalesServices { get; set; }
    public Category category { get; set; }
    public CompatibilityList compatibilityList { get; set; }
    public Contact contact { get; set; }
    public DateTime createdAt { get; set; }
    public Delivery delivery { get; set; }
    public Description description { get; set; }
    public string ean { get; set; }
    public External external { get; set; }
    public string id { get; set; }
    public List<Image> images { get; set; }
    public Location location { get; set; }
    public string name { get; set; }
    public List<Parameter> parameters { get; set; }
    public Payments payments { get; set; }
    public Product product { get; set; }
    public Promotion promotion { get; set; }
    public Publication publication { get; set; }
    public SellingMode sellingMode { get; set; }
    public SizeTable sizeTable { get; set; }
    public Stock stock { get; set; }
    public DateTime updatedAt { get; set; }
    public Validation validation { get; set; }
}
RootObject obj = (RootObject)Session["get_offer"];

Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();

foreach (PropertyInfo propertyInfo in properties)
{
    Type child_type = propertyInfo.GetType();
    PropertyInfo[] child_prop = child_type.GetProperties();

    foreach (PropertyInfo final_prop in child_prop)
    {
        parametr.Items.Add(final_prop.GetValue().ToString());
    }
}

例如,我想获取IdProduct的{​​{1}}的值。我知道我可以简单地做RootObject obj,但是如何动态地做它,以便可以遍历整个对象?

编辑:此处是新代码

obj.Product.Id

0 个答案:

没有答案
相关问题