クラスを使用して接続文字列を格納しています。私のアプリは、設定クラスから設定を読み取り、それをインスタンス変数に割り当てます。これは、いくつかのコントロールにバインドされます。私の接続文字列クラスには、次の属性セットがあります。
[TypeConverter(typeof(ConnectionStringConverter))]
私の型コンバーターは以下のようになります。
問題は、設定ファイルの設定が空白の場合、設定クラスが null を返すことです。デフォルトのコンストラクターを使用した接続文字列クラスのインスタンスではありません。
誰かこのなぞなぞを解くのを手伝ってくれませんか。
ありがとう。
public class ConnectionStringConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
return (ConnectionString)(value as string);
else
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return (string)(value as ConnectionString);
else
return base.ConvertTo(context, culture, value, destinationType);
}
}