You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# 对接 Firebase 时如何访问获取到的 Object 类型数据

解决方案

1. 重新定义强类型Activity类

你不需要保留object类型的属性定义,直接将属性调整为和Firebase数据结构匹配的强类型即可,你使用的CrossCloudFirestore库原生支持GeoPoint类型的序列化与反序列化:

public class Activity
{
    // 根据实际存储的时长类型调整为long/int/TimeSpan均可
    public long elapsedTime { get; set; }
    // 直接定义为GeoPoint集合类型,对应Firebase中的GeoPoint数组
    public List<Plugin.CloudFirestore.GeoPoint> positions { get; set; }
    // 其余属性同理替换为对应强类型即可
}

2. 调整查询代码,转换为强类型对象使用

查询到文档后调用库提供的ToObject<T>方法直接转换为强类型实例,即可直接遍历集合:

var documents = await CrossCloudFirestore.Current
                .Instance
                .Collection("Activity")
                .GetAsync();

foreach (var doc in documents.Documents)
{
    // 转换为强类型Activity实例
    var activity = doc.ToObject<Activity>();
    // 直接遍历positions坐标集合
    foreach (var geoPoint in activity.positions)
    {
        double lat = geoPoint.Latitude;
        double lng = geoPoint.Longitude;
        // 按需处理经纬度数据
    }
}

补充说明

如果你不想依赖CrossCloudFirestore库的GeoPoint类型,也可以自定义坐标类后手动映射:

// 自定义坐标类
public class MyGeoPoint
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

// 映射代码示例
var firestorePoints = doc.Get<List<Plugin.CloudFirestore.GeoPoint>>("positions");
var myPoints = firestorePoints.Select(p => new MyGeoPoint{ Latitude = p.Latitude, Longitude = p.Longitude }).ToList();

内容的提问来源于stack exchange,提问作者Quentin RF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 07:45:03