.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();
额外注意事项
- 确保你在Google Workspace管理控制台已经创建了名为Custom Field Testing的自定义架构,且其中包含blah text这个字段
- 如果自定义字段是其他类型(如数字、布尔值),内层字典的value也要对应使用对应类型的object(比如
["age"] = 30)
内容的提问来源于stack exchange,提问作者Joey
相关产品推荐
相关产品推荐

