如何在访问静态成员之前确保私有构造函数加载?

时间:2017-09-12 18:50:26

标签: c# .net

当有人进行以下调用时,如何确保执行私有构造函数?

var rc = CrmSecureConfiguration.RestCRMClientConfiguration;

以下是实施:

public class CrmSecureConfiguration
{
    private CrmSecureConfiguration()
    {
        var configurationPackage = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config");

        RestCRMClientConfiguration.CRMOrganizationName = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMOrganizationName"].Value;
        RestCRMClientConfiguration.UserName = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServiceUserName"].Value;
        RestCRMClientConfiguration.Password = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServicePassword"].Value;
        RestCRMClientConfiguration.Domain = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServiceDomain"].Value;
        RestCRMClientConfiguration.CRMWebServiceBaseUrl = $"{configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServiceBaseUrl"].Value}/{RestCRMClientConfiguration.CRMOrganizationName}/api/data/v8.1/";

    }
    public static RestCRMClientConfiguration RestCRMClientConfiguration { get; private set; }
}

由于某种原因,当前行为导致私有构造函数根本不执行。

2 个答案:

答案 0 :(得分:4)

如果不创建实例,则无法做到这一点。也许您想要一个静态构造函数?

static CrmSecureConfiguration()
{
    var configurationPackage = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config");

    RestCRMClientConfiguration.CRMOrganizationName = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMOrganizationName"].Value;
    RestCRMClientConfiguration.UserName = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServiceUserName"].Value;
    RestCRMClientConfiguration.Password = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServicePassword"].Value;
    RestCRMClientConfiguration.Domain = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServiceDomain"].Value;
    RestCRMClientConfiguration.CRMWebServiceBaseUrl = $"{configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServiceBaseUrl"].Value}/{RestCRMClientConfiguration.CRMOrganizationName}/api/data/v8.1/";

}

答案 1 :(得分:0)

单身人士模式:

public class CrmSecureConfiguration
{
   private static CrmSecureConfigurationinstance;

   private CrmSecureConfiguration() {
      var configurationPackage = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config");

      RestCRMClientConfiguration.CRMOrganizationName = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMOrganizationName"].Value;
      RestCRMClientConfiguration.UserName = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServiceUserName"].Value;
      RestCRMClientConfiguration.Password = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServicePassword"].Value;
      RestCRMClientConfiguration.Domain = configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServiceDomain"].Value;
      RestCRMClientConfiguration.CRMWebServiceBaseUrl = $"{configurationPackage.Settings.Sections["CRMWebService"].Parameters["CRMWebServiceBaseUrl"].Value}/{RestCRMClientConfiguration.CRMOrganizationName}/api/data/v8.1/";
   }

   public static CrmSecureConfiguration Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new CrmSecureConfiguration();
         }
         return instance;
      }
   }

   public RestCRMClientConfiguration RestCRMClientConfiguration { get; private set; }
}

然后客户:

var rc = CrmSecureConfiguration.Instance.RestCRMClientConfiguration;