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; }

我需要获取它的类型转换器实例。我试过:

        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。

有什么帮助吗?

编辑:

我正在尝试做的是以下内容。我需要通过属性网格设置类中的 2 个属性。

“Conexion”属性是一个依赖于其他属性的值列表。

此依赖项以这种方式运行良好:

当其他属性发生变化,然后我展开“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();
    }

返回的列表存储在转换器的私有(private)变量中,因此这完美地显示了依赖于其他属性的列表。

到目前为止一切顺利。当对象在其属性中已经具有值时,就会出现问题。由于“Conexion”列表是排他性的,因此当我将其分配给属性网格时,它的属性值显示为空。

这是显而易见的,因为依赖列表仅在调用 GetStandardValuesSupported 时才会填充,并且仅在我尝试编辑属性时才会发生。

现在我需要我在问题中提出的问题。我需要在对象构造函数中显式调用 GetStandardValuesSupported,以便在分配“Conexion”属性之前强制加载依赖列表。这样一来,我确信该属性将显示为已初始化,因为列表将具有其值。

我认为您的解决方案应该可行,但 GetConverter 返回 null。问题仅减少为调用我的自定义类型转换器的 GetStandardValuesSupported(),因此我也可以使用 Activator.CreateInstance,但问题是转换器的类型为 null 而不是 DevicesTypeConverter 的类型。

最佳答案

您必须跳过几层才能获得 TypeConverterAttribute ( LINQPad demo ) 中指定的 TypeConverter 实例:

//Get the type you are interested in.
var type = typeof(MyClass); 

//Get information about the property you are interested in on the type.
var prop = type.GetProperty("Conexion"); 

//Pull off the TypeConverterAttribute.
var attr = prop.GetCustomAttribute<TypeConverterAttribute>();

//The attribute only stores the name of the TypeConverter as a string.
var converterTypeName = attr.ConverterTypeName; 

// Get the actual Type of the TypeConverter from the string.
var converterType = Type.GetType(converterTypeName); 

//Create an instance of the TypeConverter.
var converter = (TypeConverter) Activator.CreateInstance(converterType); 

既然您拥有以下类型,您就可以使用现有的方法获取转换器:

var converter = TypeDescriptor.GetConverter(converterType);

关于c# - 如何获取属性的TypeConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49203910/

相关文章:

c# - 最短公共(public)前缀的 LINQ 表达式

javascript - 将 JSON 转换为 ViewModel 并将 token 传递给 Controller

c# - 即使在释放资源后也无法关闭 excel

c# - 我无法在 C# 中将 list<object[]> 转换为 list <T[]>

c++ - 如何将字符串转换为 long double?

C# - 从 XML 文档中的特定标记获取值

c# - 如何使 C# 应用程序充当服务?

c# - TypeConverter 将 byte[] 转换为 Bitmap

c# - 来自 dll 的类型转换器

c# - 在 .NET 中实现枚举数组到字符串数组类型转换器