添加的TypeConverter属性在运行时枚举 [英] Add TypeConverter attribute to enum in runtime

查看:409
本文介绍了添加的TypeConverter属性在运行时枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个C#/ WPF应用程序,我为了显示本地化的文本,而不是枚举的文本添加的TypeConverter属性,我的一些枚举:

In a C#/WPF application I added a TypeConverter attribute to some of my enums in order to display a localized text instead of the text of the enum:

[TypeConverter(typeof(LocalizedEnumTypeConverter))]
public enum MyEnum
{
    EnumVal1 = 0,
    EnumVal2 = 1,
    EnumVal3 = 2,
}

我已经实现LocalizedEnumTypeConverter执行此任务。

I have implemented LocalizedEnumTypeConverter to perform this task.

在出现问题时,我尝试使用相同的方法与在另一个程序集中定义枚举,即有LocalizedEnumTypeConverter进不去,它是与其他应用程序(也就是,我不能一个参照集添加共享其中,LocalizedEnumTypeConverter定义)。

The problem arises when I try to use the same approach with an enum that is defined in another assembly, that has no access to LocalizedEnumTypeConverter, and it is shared with other applications (that is, I cannot add a reference to the assembly where LocalizedEnumTypeConverter is defined).

有没有一种方法添加的TypeConverter属性,在运行?这样我可以留在没有TypeConverter的属性其他组件的枚举,然后在我的应用程序中添加它的运行时间。

Is there a way to add the TypeConverter attribute in runtime? This way I can leave the enum in the other assembly without the TypeConverter attribute, and then add it in runtime in my application.

推荐答案

这可以通过使用TypeDescriptor类的https://msdn.microsoft.com/en-us/library/system.componentmodel.typedescriptor.aspx.请参阅下面的示例。

This can be done using TypeDescriptor class https://msdn.microsoft.com/en-us/library/system.componentmodel.typedescriptor.aspx. Refer the below sample.

    Attribute[] newAttributes = new Attribute[1];
    newAttributes[0] = new TypeConverterAttribute(typeof(LocalizedEnumTypeConverter));

    TypeDescriptor.AddAttributes(MyEnum, newAttributes);

这篇关于添加的TypeConverter属性在运行时枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆