无法将通用记录转换为对象
Unable to cast Generic record to object
您好,我在 C# 中有以下模型。这些是 Avro 文件。
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"
};
}
}
如果我尝试访问以下值,它会起作用。
object ageGrading = ((GenericRecord)response.Message.Value["Product"])["AgeGrading"];
如果我尝试访问以下值,则会抛出
object familyTree = ((GenericRecord)response.Message.Value["Product"])["FamilyTree"]["Class"]["Code"];
它抛出错误 Cannot apply indexing with [] to an expression of type object
谁能帮我找出这个错误?任何帮助,将不胜感激。谢谢。
您正在将此对象转换为 GenericRecord ((GenericRecord)response.Message.Value["Product"])
,但它 returns 此 ((GenericRecord)response.Message.Value["Product"])["FamilyTree"]
作为对象。您需要将每个级别转换为 GenericRecord 才能获取它的属性。
((GenericRecord)((GenericRecord)((GenericRecord)response.Message.Value["Product"])["FamilyTree"])["Class"])["Code"]
您好,我在 C# 中有以下模型。这些是 Avro 文件。
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"
};
}
}
如果我尝试访问以下值,它会起作用。
object ageGrading = ((GenericRecord)response.Message.Value["Product"])["AgeGrading"];
如果我尝试访问以下值,则会抛出
object familyTree = ((GenericRecord)response.Message.Value["Product"])["FamilyTree"]["Class"]["Code"];
它抛出错误 Cannot apply indexing with [] to an expression of type object
谁能帮我找出这个错误?任何帮助,将不胜感激。谢谢。
您正在将此对象转换为 GenericRecord ((GenericRecord)response.Message.Value["Product"])
,但它 returns 此 ((GenericRecord)response.Message.Value["Product"])["FamilyTree"]
作为对象。您需要将每个级别转换为 GenericRecord 才能获取它的属性。
((GenericRecord)((GenericRecord)((GenericRecord)response.Message.Value["Product"])["FamilyTree"])["Class"])["Code"]