C# 如何获取属性的TypeConverter
我有这个财产:C# 如何获取属性的TypeConverter,c#,typeconverter,C#,Typeconverter,我有这个财产: [DisplayName("Conexión")] [TypeConverter(typeof(Converters.DevicesTypeConverter))] [Description("Conexión con el dispositivo a usar.")] [Required] public string Conexion { get; set; } 我需要得到它的类型转换器实例。我试过: PropertyD
[DisplayName("Conexión")]
[TypeConverter(typeof(Converters.DevicesTypeConverter))]
[Description("Conexión con el dispositivo a usar.")]
[Required]
public string Conexion { get; set; }
我需要得到它的类型转换器实例。我试过:
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this);
PropertyDescriptor property = properties.Find("Conexion", false);
var converter = TypeDescriptor.GetConverter(property);
即使:
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this);
PropertyDescriptor property = properties.Find("Conexion", false);
var converter = TypeDescriptor.GetConverter(property.PropertyType);
这样,我只能得到属性类型的转换器,也就是字符串类型的转换器,而不是实际的属性转换器DevicesTypeConverter
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this);
PropertyDescriptor propertyDescriptor = properties.Find("Conexion", false);
propertyDescriptor.Converter // <- This is what you want
需要帮忙吗
编辑:
我想做的是以下几点。我需要通过属性网格设置类中的两个属性
“Conexion”属性是依赖于其他属性的值列表
这种依赖关系通过以下方式运行良好:
public List<object> GetDevicesList()
{
if (this.Driver == null)
return null;
var devices = this.Driver.GetList();
if (devices == null)
return null;
return devices.Select(l => (object)l.Value).ToList();
}
当其他属性发生更改,然后我展开“Conexion”属性时,将调用“Conexion”上的GetStandardValuesSupported方法。在该方法中,我使用“context.Instance”调用对象实例上的方法,该方法通过以下方式检索列表:
public List<object> GetDevicesList()
{
if (this.Driver == null)
return null;
var devices = this.Driver.GetList();
if (devices == null)
return null;
return devices.Select(l => (object)l.Value).ToList();
}
公共列表getDeviceList()
{
if(this.Driver==null)
返回null;
var devices=this.Driver.GetList();
如果(设备==null)
返回null;
return devices.Select(l=>(object)l.Value.ToList();
}
返回的列表存储在转换器的私有变量中,因此这完美地显示了依赖于其他属性的列表
到目前为止还不错。当对象的属性中已经有值时,就会出现问题。因为“Conexion”列表是独占的,所以当我将其指定给属性网格时,它的属性值显示为空
这是显而易见的,因为只有在调用GetStandardValuesSupported时才会填充依赖列表,并且只有在尝试编辑属性时才会发生这种情况
现在我需要我在问题中所问的。我需要在对象构造函数中显式调用GetStandardValuesSupported,以便在分配“Conexion”属性之前强制加载依赖列表。这样,我确信属性将被初始化,因为列表将有它的值
我认为您的解决方案应该可以工作,但GetConverter返回null。该问题仅限于调用GetStandardValuesSupported()函数我的自定义类型转换器,因此我也可以使用Activator.CreateInstance,但问题是转换器的类型为null,而不是DevicesTypeConverter的类型。您必须跳转多个层才能获得
TypeConverter
中指定的TypeConverter
():
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this);
PropertyDescriptor propertyDescriptor = properties.Find("Conexion", false);
propertyDescriptor.Converter // <- This is what you want
您必须跳过多个层才能获得
TypeConverterAttribute
()中指定的TypeConverter
的实例:
你很接近。为感兴趣的属性创建PropertyDescriptor后,使用Converter属性获取转换器
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this);
PropertyDescriptor propertyDescriptor = properties.Find("Conexion", false);
propertyDescriptor.Converter // <- This is what you want
PropertyDescriptorCollection properties=TypeDescriptor.GetProperties(this);
PropertyDescriptor PropertyDescriptor=properties.Find(“Conexion”,false);
propertyDescriptor.Converter/您很接近。为感兴趣的属性创建PropertyDescriptor后,使用Converter属性获取转换器
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(this);
PropertyDescriptor propertyDescriptor = properties.Find("Conexion", false);
propertyDescriptor.Converter // <- This is what you want
PropertyDescriptorCollection properties=TypeDescriptor.GetProperties(this);
PropertyDescriptor PropertyDescriptor=properties.Find(“Conexion”,false);
propertyDescriptor.Converter//我被你所做的事情弄糊涂了。属性datya类型为字符串,但您正在尝试分配其他转换器?是。该转换器显示选项列表。每个选项都是一个字符串。我对你所做的感到困惑。属性datya类型为字符串,但您正在尝试分配其他转换器?是。该转换器显示选项列表。每个选项都是一个字符串,我已经考虑过了。然而,我认为通过创建这样的TypeConverter实例,我将释放转换器的私有变量,不是吗?当我调用转换器时,我将分配一个内部变量。当我试图编辑属性时,我需要该变量保持其值。此外,我认为如果我使用静态变量是一个丑陋的解决方案。如果你需要这种功能,你应该将它添加到你的问题中。我更新了答案,给出了一种不同的方法,一旦你知道了转换器的类型,就可以不获取它。我尝试了,但是type.GetType(converterTypeName);返回null。即使converterTypeName是“Configuracion.Converters.DevicesTypeConverter,Configuracion,Version=1.0.0,Culture=neutral,PublicKeyToken=null”,我也更新了问题,告诉您整个场景。我认为您的解决方案可以工作,但是Type.GetType返回null:-(我已经考虑过了。但是,我认为通过创建这样的TypeConverter实例,我会释放转换器的私有变量,不是吗?当我调用转换器时,我会分配一个内部变量。我需要该变量在我试图编辑属性时保持其值。此外,我认为如果我使用静态变量是一个丑陋的解决方案。如果如果您需要此类功能,则应将其添加到问题中。我更新了答案,以便在您知道转换器的类型后提供另一种获取转换器的方法。我尝试了该方法,但type.GetType(converterTypeName);返回空值。即使converterTypeName为“Configuracion.Converters.DevicesTypeConverter,Configuracion,Version=1.0.0,Culture=neutral,PublicKeyToken=null”我已经更新了这个问题,告诉您整个场景。我认为您的解决方案可以工作,但是Type.GetType返回null:-(Agree--PropertyDescriptor.Converter
将从TypeConverterAttribute
实例化类型转换器,如果不存在属性,则返回对属性类型调用TypeDescriptor.GetConverter
。Agree--PropertyDescriptor.Converter
将从TypeCon>实例化类型转换器。)verterAttribute
,如果不存在属性,则返回对属性类型调用TypeDescriptor.GetConverter
。