Mam metodę użytkową, która zwraca silnie typami wartości od starej .INI
konfiguracji pliku typu, z podpisemRzuć string.Empty to (generic) T w C#?
internal static T GetIniSetting<T>(string config, string key, T defVal = default(T))
chcę ciągi być specjalne, w które chciałbym wartość domyślną dla defaultValue być string.Empty
, a nie default(string)
(tj. null), w przypadku, gdy koder nie określił wartości domyślnej.
if (cantFindValueInIniFile == true)
{
if ((typeof(T) == typeof(string)) && (defaultValue == null))
{
// *** Code needed here - Cannot convert string to <T>***
return (T)string.Empty;
}
return defaultValue;
}
Próbowałem twardego odlewu, a kluczowe as
, bezskutecznie.
Wywoływanie uprawnienia LSP nie wydaje się bardzo przystoi tym konkretnym przypadku ... –