You could make the method accept a generic type, and do the casting inside the method, for example like this:
private static T ReadSubSetting<T>(T variable, String def, String line)
{
if (variable is bool)
{
if (line.StartsWith(def))
{
line = line.Substring(def.Length, line.Length - def.Length);
System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));
return (T)converter.ConvertFrom(line);
}
}
return default(T);
}
And then you can call it as following:
Pref p = new Pref();
p.ShowHiddenItems = ReadSubSetting(p.ShowHiddenItems, "show=", "show=true");
This is assuming the type of the variable is the same as the return type of the method.