我有一个类型为Object的变量,其类型为InquireSubscription
public class InquireSubscription: object, System.ComponentModel.INotifyPropertyChanged
{
public string planId {get; set}
}
我想检索planId值,但是我完全被困在寻找如何从“对象”项中访问属性的方法
请查看屏幕截图:
已编辑 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");
}
}
答案 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