如何在 WinForms C# 应用程序中 return 创建类型 (Type) 的类型 属性?
How to return a create a Type property of type (Type) in WinForms C# application?
This question is self-answered. If you have better answer post it.
Specially when dealing with NULL value in TypeConverters if you have better
approach post it
创建字符串、日期时间等属性时
看起来不错,连载了。
但我需要创建一个 Type
类型的 属性。
实际上正在创建 Type
属性 但它是不可修改的。
是否有转换器或此类任务允许这样做?
public class MyClass
{
public Type MyType{get;set;}
}
来自图片 变灰了。并且是只读的。
首先,非常感谢 @Reza Aghaei 的出色回答 感谢
他很多。
TypeConverter 实现如下:
public class DataTypeConverter : TypeConverter
{
private const string NullString = "<NULL>";
private Type[] types = new Type[] { null, typeof(byte), typeof(short), typeof(int), typeof(long), typeof(decimal), typeof(string), typeof(DateTime), typeof(bool) };
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value.ToString() == NullString) return null;
var result = types.FirstOrDefault(x => x?.ToString() == value.ToString());
if (result != null)
return result;
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value == null)
return NullString;
return value.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => new StandardValuesCollection(types);
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true;
}
结果
This question is self-answered. If you have better answer post it. Specially when dealing with NULL value in TypeConverters if you have better approach post it
创建字符串、日期时间等属性时
看起来不错,连载了。
但我需要创建一个 Type
类型的 属性。
实际上正在创建 Type
属性 但它是不可修改的。
是否有转换器或此类任务允许这样做?
public class MyClass
{
public Type MyType{get;set;}
}
来自图片 变灰了。并且是只读的。
首先,非常感谢 @Reza Aghaei 的出色回答 感谢 他很多。
TypeConverter 实现如下:
public class DataTypeConverter : TypeConverter
{
private const string NullString = "<NULL>";
private Type[] types = new Type[] { null, typeof(byte), typeof(short), typeof(int), typeof(long), typeof(decimal), typeof(string), typeof(DateTime), typeof(bool) };
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value.ToString() == NullString) return null;
var result = types.FirstOrDefault(x => x?.ToString() == value.ToString());
if (result != null)
return result;
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value == null)
return NullString;
return value.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => new StandardValuesCollection(types);
public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true;
}
结果