问题的本质:我在 c# 中有数据,这些数据是通过某些算法计算得到的。接下来,我需要通过网络发送到 nodejs 服务器,JS 对象将如下所示:
const test = {a: 1, b: 2, c: "string"}
在 C# 中,我是这样创建的。
new { Id = 1, b = 2, Body = "string" },
假设我创建了这样一个类型。
当我将它传递给函数时,我将其键入为对象,然后调用它:
然后我将它传递给发送函数,我在其中访问其中一个字段,但出现错误。如何描述这样的对象?
Send(object a) {
a.Body;
}
我得到:
error CS1061: 'object' does not contain a definition for 'Body' and no accessible extension method 'Body' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
如何决定?
为什么要使用匿名类型?C# 是一种强类型语言,类描述在这里是常态。
实际上,我是如何应对类似任务的。您通过将其强制转换为 来发出条件请求
List<a'>
,然后传入您的变量