如何从C#中的Object类型的变量获取值

时间:2019-04-02 17:40:58

标签: c# arrays object

我有一个类型为Object的变量,其类型为InquireSubscription

public class InquireSubscription: object, System.ComponentModel.INotifyPropertyChanged 
{
    public string planId {get; set}
}

我想检索planId值,但是我完全被困在寻找如何从“对象”项中访问属性的方法

请查看屏幕截图:

enter image description here

已编辑 eligibilityResult的类型为SubscriptionEligibility

    public partial class SubscriptionEligibility: object, System.ComponentModel.INotifyPropertyChanged 
    {
         private object[] itemsField;

        [System.Xml.Serialization.XmlElementAttribute("InquireSubscription", typeof(SubscriptionEligibility), Order=0)]

        [System.Xml.Serialization.XmlElementAttribute("DeviceEligibilityResult", typeof(SubscriptionEligibility), Order=0)]

       public object[] Items {
           get {
             return this.itemsField;
           } 
           set {
              this.itemsField = value;
              this.RaisePropertyChanged("Items");
           }
        }

1 个答案:

答案 0 :(得分:2)

您可以使用LINQ:

foreach (var item in eligibilityResult.Items.OfType<InquireSubscription>())
{
    item.planId = ...;
}

这将忽略所有不是InquireSubscription或子类的项目。如果要确保不是这种情况,请使用Cast():

foreach (var item in eligibilityResult.Items.Cast<InquireSubscription>())
{
    item.planId = ...;
}

如果您拥有SubscriptionEligibility的代码,则应尽可能重构它,使其不使用object