私は、最小/最大サイズの制約を持つコントロールを持っています。この制約は、2つのnull可能なプロパティを持つ構造体によって表されます(nullは無制限を意味します)。私の人生にとって、私はWinFormsデザイナーにそれを受け入れることはできません。Winformsデザイナーでnull可能な型を使用するにはどうすればよいですか?
私は(ネットからおよび.NET 4のソースからコピーされた)TypeConvertorを試してみましたが、私は
は「タイプ 『BoxRange』のコード生成が失敗します。エラーは、「キャストが有効ではありません指定されたました。」」
または
"タイプ 'BoxRangeはBoxRange' を型にキャストすることはできません ''"
そして、設定された値を忘れてしまいます。私はここで間違って何をしていますか?
この例はSize3DConverterの例ですが、.NET4のSizeConverterとほとんど違いはありません。私はサイズやポイント構造で "#、#"を分割することができない理由が分かりません。
[System.SerializableAttribute()]
[System.ComponentModel.TypeConverter(typeof(RKSHARP2.Designer.SizeRangeConvertor))]
public struct SizeRange
{
private System.Byte? Byte_Minimum;
private System.Byte? Byte_Maximum;
[System.ComponentModel.DefaultValue(typeof(System.Nullable<System.Byte>), null)]
public System.Byte? Minimum
{
get
{
return this.Byte_Minimum;
}
set
{
this.Byte_Minimum = value;
}
}
[System.ComponentModel.DefaultValue(typeof(System.Nullable<System.Byte>),null)]
public System.Byte? Maximum
{
get
{
return this.Byte_Maximum;
}
set
{
this.Byte_Maximum = value;
}
}
public SizeRange (System.Byte? Byte_Minimum, System.Byte? Byte_Maximum)
{
this.Byte_Minimum = Byte_Minimum;
this.Byte_Maximum = Byte_Maximum;
}
public static System.Boolean operator == (RKSHARP2.Controls.SizeRange Struct_Compare1, RKSHARP2.Controls.SizeRange Struct_Compare2)
{
return (Struct_Compare1.Minimum == Struct_Compare2.Minimum && Struct_Compare1.Maximum == Struct_Compare2.Maximum);
}
public static System.Boolean operator != (RKSHARP2.Controls.SizeRange Struct_Compare1, RKSHARP2.Controls.SizeRange Struct_Compare2)
{
return (Struct_Compare1.Minimum != Struct_Compare2.Minimum || Struct_Compare1.Maximum != Struct_Compare2.Maximum);
}
public override System.Boolean Equals (System.Object Object_Compare)
{
if ((Object_Compare is RKSHARP2.Controls.SizeRange) == false)
{
return false;
}
else
{
return ((((RKSHARP2.Controls.SizeRange)(Object_Compare)).Minimum == this.Minimum) && (((RKSHARP2.Controls.SizeRange)(Object_Compare)).Maximum == this.Maximum));
}
}
public override System.Int32 GetHashCode()
{
return this.Byte_Minimum.GetValueOrDefault()^this.Byte_Maximum.GetValueOrDefault();
}
public override System.String ToString()
{
return RKSHARP2.Convertor.ToString(this.Minimum, "?") + "," + RKSHARP2.Convertor.ToString(this.Maximum, "?");
}
}
public SizeRangeConvertor()
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType)
{
if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
if (culture == null)
culture = System.Globalization.CultureInfo.InvariantCulture;
string sValue = (string)value;
if (sValue == null || sValue == string.Empty)
sValue = "?,?";
string[] numbers = sValue.Split(new char[] { ',' });
object[] values = null;
System.Type[] types = null;
if (numbers.Length == 2)
{
values = new object[numbers.Length];
types = new System.Type[numbers.Length];
for (int i = 0; i < numbers.Length; i++)
{
values[1] = RKSHARP2.Convertor.ToByte(numbers[i]);
types[i] = typeof(byte?);
}
}
if (values != null)
{
System.Type type = GetSize3DType(context);
System.Reflection.ConstructorInfo constructorInfo = type.GetConstructor(types);
return constructorInfo.Invoke(values);
}
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
{
System.Type type = value.GetType();
byte? width = RKSHARP2.Convertor.ToByte(type.GetProperty("Minimum").GetValue(value, null));
byte? height = RKSHARP2.Convertor.ToByte(type.GetProperty("Maximum").GetValue(value, null));
if (destinationType == typeof(string))
{
if (culture == null)
culture = System.Globalization.CultureInfo.InvariantCulture;
return string.Format("{0},{1}", width, height);
}
else if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
{
System.Reflection.ConstructorInfo constructorInfo = type.GetConstructor(new System.Type[] { typeof(byte?), typeof(byte?) });
if (constructorInfo != null)
return new System.ComponentModel.Design.Serialization.InstanceDescriptor(constructorInfo, new object[] { width, height });
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override object CreateInstance(System.ComponentModel.ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
System.Type type = GetSize3DType(context);
System.Reflection.ConstructorInfo constructorInfo = type.GetConstructor(new System.Type[] { typeof(byte?), typeof(byte?) });
return constructorInfo.Invoke(new object[] {
propertyValues["Minimum"],
propertyValues["Maximum"]});
}
public override bool GetCreateInstanceSupported(System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}
public override System.ComponentModel.PropertyDescriptorCollection GetProperties(System.ComponentModel.ITypeDescriptorContext context, object value, System.Attribute[] attributes)
{
System.Type type = GetSize3DType(context);
return System.ComponentModel.TypeDescriptor.GetProperties(type, attributes).Sort(new string[] { "Minimum", "Maximum" });
}
public override bool GetPropertiesSupported(System.ComponentModel.ITypeDescriptorContext context)
{
return true;
}
private System.Type GetSize3DType(System.ComponentModel.ITypeDescriptorContext context)
{
if (context == null)
return typeof(RKSHARP2.Controls.SizeRange);
return context.PropertyDescriptor.PropertyType;
}
}
デザイナーはテキストのみを表示できるため、TypeConverterはnull値を空の文字列に変換できる必要があります。あなたのエラーは、そうしていないことを示唆しています。 TypeConverterコードを含めるように質問を編集してみてください。 – LarsTech