1

Hi I have below model in c#. These are Avro files.

public ProductMessageEvents GetValue(TestPayload parameter)
{
    return new ProductMessageEvents()
        {
            Metadata = new KafkaProductEvent.Metadata
                {
                    Timestamp = "test",
                    Environment = "test"
                },
            Product = new KafkaProductEvent.Product
                {
                    AgeGrading = "test",
                    KeycodeType = "type1",
                    FamilyTree = new KafkaProductEvent.FamilyTree
                        {
                            Class = new KafkaProductEvent.CodeNamePair
                                {
                                    Code = "test code",
                                    Name = "test name"
                                }
                        },
                    DssProduct = new KafkaProductEvent.DssProduct
                        {
                            DocumentStatus = "active"
                        }
                      Options = new Option[]
            {
              new Option
              {
                PrimaryColour = new CodeNamePair
                {
                  Name = "White",
                },
                SecondaryColour = new CodeNamePair
                {
                  Name = "red",
                }
              },
              new Option
              {
                PrimaryColour = new CodeNamePair
                {
                  Name = "White",
                }
              },
                },
            Version = "latestVersion"
        };
    }
}

If I try to access the below value it works.

 object ageGrading = ((GenericRecord)response.Message.Value["Product"])["AgeGrading"];

If I try to access the below value, then it throws

object familyTree = ((GenericRecord)response.Message.Value["Product"])["FamilyTree"]["Class"]["Code"];

It throws error Cannot apply indexing with [] to an expression of type object

Option data

Can someone help me to identify this error? Any help would be appreciated. Thanks.

Niranjan
  • 1,881
  • 6
  • 44
  • 71
  • Hi. It is not working. Thanks – Niranjan Jul 15 '19 at 09:40
  • 1
    nobody ever reads exception messages :( _"Cannot apply indexing with [] to an expression of type object"_ means that you are trying to apply indexing with [] to an expression of type object – vasily.sib Jul 15 '19 at 09:51

1 Answers1

2

You are casting this object to a GenericRecord ((GenericRecord)response.Message.Value["Product"]) but it returns this ((GenericRecord)response.Message.Value["Product"])["FamilyTree"] as an object. You'll need to cast each level to GenericRecord to get to it's properties.

((GenericRecord)((GenericRecord)((GenericRecord)response.Message.Value["Product"])["FamilyTree"])["Class"])["Code"]
Tyddlywink
  • 876
  • 7
  • 28
  • May I know how Can I do at each level? – Niranjan Jul 15 '19 at 09:45
  • Hi, If I have some enumerable data then how we can get data? I have added some code above. – Niranjan Jul 16 '19 at 10:39
  • Cast it to `IEnumerable` instead. – Tyddlywink Jul 16 '19 at 13:04
  • I tired to cast. I have attached screen shot. But how can I get non public members? Inside non public members there is content which holds my value. – Niranjan Jul 16 '19 at 13:42
  • I really like this guys answer to that. https://stackoverflow.com/questions/5816870/how-to-get-the-value-of-non-public-member. If you must do it you'll need to use reflection and that's a whole other ball game. – Tyddlywink Jul 16 '19 at 14:12
  • Thanks. Instead of manually doing this do we have any library for doing this? – Niranjan Jul 16 '19 at 14:13
  • Google "Access nonpublic member c#" and you'll see suggestions. As far as I know you are going to have to write code to access the properties you want. – Tyddlywink Jul 16 '19 at 15:18