Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用不可变的集合或减少字段的可访问性&x27;创建ContactMapping';_C#_Visual Studio_Sonarqube_Immutability - Fatal编程技术网

C#使用不可变的集合或减少字段的可访问性&x27;创建ContactMapping';

C#使用不可变的集合或减少字段的可访问性&x27;创建ContactMapping';,c#,visual-studio,sonarqube,immutability,C#,Visual Studio,Sonarqube,Immutability,我在一个旧项目中遇到了一个类问题,我不知道如何重构这个部分,我从sonarcube得到了以下错误消息: Use an immutable collection or reduce the accessibility of the field(s) 'CreateContactMapping'.Why is this an issue? 这是一段代码 public static readonly Dictionary<string, Func<UpdateContactService

我在一个旧项目中遇到了一个类问题,我不知道如何重构这个部分,我从sonarcube得到了以下错误消息:

Use an immutable collection or reduce the accessibility of the field(s) 'CreateContactMapping'.Why is this an issue?
这是一段代码

public static readonly Dictionary<string, Func<UpdateContactServiceRequest, object>> UpdateContactMapping = new Dictionary<string, Func<UpdateContactServiceRequest, object>>
        {
            { "firstname", req => req.FirstName },
            { "lastname", req => req.LastName },
            { "full_name", req => req.FullName },
            { "email", req => req.Email },
            { "phone", req => req.Phone },
            { "date_of_birth", req => req.BirthDate },
            { "job_title", req => req.JobTitle },
            { "occupation", req => req.Occupation },
            { "renda", req => req.MonthlyIncome },
            { "lifecyclestage", req => req.LifeCycleStage },
            { "hs_lead_status", req => req.LeadStatus },
            { "ali_email_validated", req => req.EmailValidated },
            { "ali_sms_token_validated", req => req.CellphoneValidated },
            { "id_da_proposta_atual", req => req.CurrentProposalId },
            { "contact_type", req => req.ContactType }
        };
公共静态只读字典UpdateContactMapping=新字典 { {“firstname”,req=>req.firstname}, {“lastname”,req=>req.lastname}, {“全名”,req=>req.FullName}, {“email”,req=>req.email}, {“phone”,req=>req.phone}, {“出生日期”,req=>req.BirthDate}, {“job_title”,req=>req.JobTitle}, {“占领”,req=>req.occulation}, {“renda”,req=>req.MonthlyIncome}, {“LifecycleState”,req=>req.LifecycleState}, {“hs_lead_status”,req=>req.LeadStatus}, {“ali_email_validated”,req=>req.EmailValidated}, {“ali_sms_token_validated”,req=>req.CellphoneValidated}, {“id_da_proposta_atual”,req=>req.CurrentProposalId}, {“contact_type”,req=>req.ContactType} };
如何最好地解决此问题?

一种解决方案可以是将
字典
更改为
不可变字典
,然后在初始化字典后运行
.ToImmutableDictionary()

public static readonly ImmutableDictionary UpdateContactMapping=新字典
{
{“firstname”,req=>req.firstname},
{“lastname”,req=>req.lastname},
{“全名”,req=>req.FullName},
{“email”,req=>req.email},
{“phone”,req=>req.phone},
{“出生日期”,req=>req.BirthDate},
{“job_title”,req=>req.JobTitle},
{“占领”,req=>req.occulation},
{“renda”,req=>req.MonthlyIncome},
{“LifecycleState”,req=>req.LifecycleState},
{“hs_lead_status”,req=>req.LeadStatus},
{“ali_email_validated”,req=>req.EmailValidated},
{“ali_sms_token_validated”,req=>req.CellphoneValidated},
{“id_da_proposta_atual”,req=>req.CurrentProposalId},
{“contact_type”,req=>req.ContactType}
}.ToImmutableDictionary();

这解决了我的问题,非常感谢!