-2

I have a variable of type Object which is typeof InquireSubscription

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

I want to retrieve the planId value but i am totally stuck in finding out how to access the properties out of the Object item

Please have a look at the Screenshot:

enter image description here

EDITED eligibilityResult is of type 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");
           }
        }
Huma Ali
  • 1,759
  • 7
  • 40
  • 66
  • 1
    You can use `dynamic` or reflection, but you *really* shouldn't if you can avoid it. What is the type of `eligibilityResult.Items`? If it's a strongly typed collection, replace `object item` with `var item` – p.s.w.g Apr 02 '19 at 17:43
  • Possible duplicate of [How to cast Object to its actual type?](https://stackoverflow.com/questions/12234097/how-to-cast-object-to-its-actual-type) – hatchet - done with SOverflow Apr 02 '19 at 17:44
  • What is the type of eligibilityResult.Items? Can you post your code? You should be able to write `foreach(InquireSubscribtion item in eligibilityResult)` in your code. – Justin Lessard Apr 02 '19 at 17:44
  • 2
    A side note, all classes inherit from `object` by default so you don't need to specify that. – yazanpro Apr 02 '19 at 17:46
  • The tooltip will tell you what type to use for `item`, or you could just write `foreach (var item in...` and then referencing `item.planId` will just work. – iakobski Apr 02 '19 at 17:57
  • Edited my post for further clarity – Huma Ali Apr 02 '19 at 18:07
  • @iakobski I get error `object' does not contain a definition for 'planId' and no extension method 'planId' accepting a first argument of type 'object' could be found` when I reference like `item.planId` – Huma Ali Apr 02 '19 at 18:10
  • `SubscriptionEligibility.Items` should be `InquireSubscription[]`, not `object[]`. – Dour High Arch Apr 02 '19 at 18:44

1 Answers1

2

You can use LINQ:

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

This will ignore any Items that are not InquireSubscription or a subclass. Use Cast() if you want to be sure that it isn't the case:

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

If you own the code of SubscriptionEligibility you should instead try to refactor it to not use object if possible

user1781290
  • 2,674
  • 22
  • 26