我有一个静态类:
public static class ConfigurationDetails
{
public static string SharepointServer { get; set; }
public static string Username { get; set; }
public static string Password { get; set; }
public static string SharepointDomain { get; set; }
public static string ServiceLibrary { get; set; }
public static string SqlServer { get; set; }
public static string SQLUsername { get; set; }
public static string SQLPassword { get; set; }
public static string SQLDomain { get; set; }
}
需要从字典的值更新。如果代码如下,则使用:
var configValues = ReadConfiguration();
if (configValues.ContainsKey("SharepointServer"))
{
ConfigurationDetails.SharepointServer = configValues["SharepointServer"];
}
if (configValues.ContainsKey("ServiceLibrary"))
{
ConfigurationDetails.ServiceLibrary = configValues["ServiceLibrary"];
}
if (configValues.ContainsKey("Username"))
{
ConfigurationDetails.Username = configValues["Username"];
}
if (configValues.ContainsKey("Password"))
{
ConfigurationDetails.SharepointServer = configValues["Password"];
}
现在这将完成这项工作,但会有很多 if 条件。我尝试使用 LINQ ,但这是我得到的最接近的。
configValues.Where(kv => kv.Key == "SharepointServer").ToList().ForEach(kv => ConfigurationDetails.SharepointServer = kv.Value);
它没有任何好处。请有任何其他想法。