有没有办法使这个方法通用,所以我可以返回一个字符串,bool, int,或双精度?现在,它返回一个字符串,但如果它能够找到“true”或“false”作为配置值,我想返回一个bool值。

    public static string ConfigSetting(string settingName)
    {  
         return ConfigurationManager.AppSettings[settingName];
    }

当前回答

你需要让它成为一个泛型方法,就像这样:

public static T ConfigSetting<T>(string settingName)
{  
    return /* code to convert the setting to T... */
}

但是调用者必须指定他们所期望的类型。然后,您可能会使用Convert。ChangeType,假设所有相关类型都被支持:

public static T ConfigSetting<T>(string settingName)
{  
    object value = ConfigurationManager.AppSettings[settingName];
    return (T) Convert.ChangeType(value, typeof(T));
}

请注意,我并不完全相信这是个好主意……

其他回答

你可以使用Convert.ChangeType():

public static T ConfigSetting<T>(string settingName)
{
    return (T)Convert.ChangeType(ConfigurationManager.AppSettings[settingName], typeof(T));
}

你需要让它成为一个泛型方法,就像这样:

public static T ConfigSetting<T>(string settingName)
{  
    return /* code to convert the setting to T... */
}

但是调用者必须指定他们所期望的类型。然后,您可能会使用Convert。ChangeType,假设所有相关类型都被支持:

public static T ConfigSetting<T>(string settingName)
{  
    object value = ConfigurationManager.AppSettings[settingName];
    return (T) Convert.ChangeType(value, typeof(T));
}

请注意,我并不完全相信这是个好主意……

 private static T[] prepareArray<T>(T[] arrayToCopy, T value)
    {
        Array.Copy(arrayToCopy, 1, arrayToCopy, 0, arrayToCopy.Length - 1);
        arrayToCopy[arrayToCopy.Length - 1] = value;
        return (T[])arrayToCopy;
    }

I was performing this throughout my code and wanted a way to put it into a method. I wanted to share this here because I didn't have to use the Convert.ChangeType for my return value. This may not be a best practice but it worked for me. This method takes in an array of generic type and a value to add to the end of the array. The array is then copied with the first value stripped and the value taken into the method is added to the end of the array. The last thing is that I return the generic array.

请尝试以下代码:

public T? GetParsedOrDefaultValue<T>(string valueToParse) where T : struct, IComparable
{
 if(string.EmptyOrNull(valueToParse))return null;
  try
  {
     // return parsed value
     return (T) Convert.ChangeType(valueToParse, typeof(T));
  }
  catch(Exception)
  {
   //default as null value
   return null;
  }
 return null;
}

有很多方法可以做到这一点(按优先级列出,具体到OP的问题)

Option 1: Straight approach - Create multiple functions for each type you expect rather than having one generic function. public static bool ConfigSettingInt(string settingName) { return Convert.ToBoolean(ConfigurationManager.AppSettings[settingName]); } Option 2: When you don't want to use fancy methods of conversion - Cast the value to object and then to generic type. public static T ConfigSetting<T>(string settingName) { return (T)(object)ConfigurationManager.AppSettings[settingName]; } Note - This will throw an error if the cast is not valid(your case). I would not recommend doing this if you are not sure about the type casting, rather go for option 3. Option 3: Generic with type safety - Create a generic function to handle type conversion. public static T ConvertValue<T,U>(U value) where U : IConvertible { return (T)Convert.ChangeType(value, typeof(T)); } Note - T is the expected type, note the where constraint here(type of U must be IConvertible to save us from the errors)