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

.NET调用Google Workspace API时CustomSchemas报CS0266错误

问题原因及解决方案

你的编译错误是因为CustomSchemas的类型不匹配:

  • Google Workspace API的User.CustomSchemas属性要求的类型是IDictionary<string, IDictionary<string, object>>
  • 你声明的是Dictionary<string, object>,且内层字典是Dictionary<string, string>,完全不符合类型要求

修改后的代码

userGoogleUserWithData = new User()
{
    PrimaryEmail = userEmail,
    Password = generatedPassword,
    Name = new UserName
    {
        GivenName = givenName,
        FamilyName = familyName
    },
    ChangePasswordAtNextLogin = true,
    OrgUnitPath = "/",
    Suspended = false,
    Relations = new List<UserRelation>
    {
        new UserRelation { Type = "manager", Value = googleData.manager } // Set the manager's email
    },
    Addresses = new List<UserAddress>
    {
        new UserAddress
        {
            Type = "work",
            Formatted = googleData.locationAddress,
            Locality = myBranch.branchCity,
            Region = myBranch.branchState,
            PostalCode = myBranch.branchZip,
            Country = myBranch.branchCountry
        }
    },
    Organizations = new List<UserOrganization>
    {
        new UserOrganization
        {
            Title = googleData.jobTitle,
            Primary = true,
            Department = googleData.department,
            CostCenter = googleData.costCenter,
            Description = "user",
            Name = "Test Company"
        }
    },
    // 核心修改:外层字典类型改为Dictionary<string, IDictionary<string, object>>
    CustomSchemas = new Dictionary<string, IDictionary<string, object>>
    {
        ["Custom Field Testing"] = new Dictionary<string, object>
        {
            ["blah text"] = "Test Value" // string可隐式转换为object,无需额外强制转换
        }
    }
};
service.Users.Insert(userGoogleUserWithData).Execute();

额外注意事项

  1. 确保你在Google Workspace管理控制台已经创建了名为Custom Field Testing的自定义架构,且其中包含blah text这个字段
  2. 如果自定义字段是其他类型(如数字、布尔值),内层字典的value也要对应使用对应类型的object(比如["age"] = 30)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 08:00:13