帮助在 Excel PIA 程序集中使用 TypeConverters 进行枚举

发布于 2024-07-07 04:29:59 字数 623 浏览 6 评论 0原文

我想使用 TypeCoverter 来区域化程序集中枚举的输出,该程序集是加载到 Excel 中的 PIA。

我可以运行它,并且它可以在我使用显式引用的程序集创建的测试项目中的程序集上运行,但是当运行已构建为 Excel PIA 的项目时。 如果我尝试: _ 公共枚举 MyEnum 项目A 项目B 结束枚举

并在代码中 myE = MyEnum.ItemA 昏暗转换器 As System.ComponentModel.TypeConverter = TypeDescriptor.GetConverter(myE)

在立即窗口中 ? converter.ToString() 规定 “System.ComponentModel.EnumConverter”

,而在我的其他项目(也是一个强签名的程序集,但直接从新创建的存根窗口表单项目引用)中,我得到

? 转换器.ToString “ClassLibrary1.LocalizedEnumConverter”

所以看起来 LocalizedEnumConverter 没有绑​​定到枚举 - 有什么想法吗? 这是因为 Excel 加载程序集的方式造成的吗?有没有办法解决这个问题?

I would like to use a TypeCoverter to regionalise output for enums in an assembly that is a PIA loaded into Excel.

I can run this and it works on an assembly in a test project I created with an explicitly referenceed assembly, however when running a project that has been built as an Excel PIA. If I try:
_
public enum MyEnum
ItemA
ItemB
end enum

and in code
myE = MyEnum.ItemA
Dim converter As System.ComponentModel.TypeConverter = TypeDescriptor.GetConverter(myE)

In the immediate window
? converter.ToString() goves
"System.ComponentModel.EnumConverter"

whereas in my other project (also a strongly signed assembly, but referenced directly from a newly created stub windows form project), I get

? converter.ToString
"ClassLibrary1.LocalizedEnumConverter"

so it look like the LocalizedEnumConverter is not being bound to the enum - any ideas? Is this because of the way Excel loads the assembly, and is there a way arounfd this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哽咽笑 2024-07-14 04:29:59

类型转换器与枚举位于同一个 dll 中吗? TypeDescriptor 必须能够解析类型转换器、UI 类型编辑器等 - 否则它会使用简单的默认值。

我还没有对 Excel 进行任何具体的尝试,但我想知道它是否不像程序集解析那么简单...

如果有帮助,您可以使用 AppDomain.AssemblyResolve 事件来自定义程序集正在加载 - 但这是一个复杂的区域......除非绝对必要,否则我会避免它。

Is the type converter in the same dll as the enum? TypeDescriptor must be able to resolve the type-converters, UI-type-editors, etc - otherwise it uses the simple defaults.

I haven't tried anything sepcifically with Excel, but I'm wondering if it isn't as simple as assembly resolution...

If it helps, you can use the AppDomain.AssemblyResolve event to customize assembly loading - but this is a complex area... I'd avoid it unless absolutely necessary.

笑饮青盏花 2024-07-14 04:29:59

经过大量的挖掘后,我找到了解决方案,这适用于无法解析的程序集:

添加程序集解析的处理程序:

    Dim currentDomain As AppDomain = AppDomain.CurrentDomain
    AddHandler currentDomain.AssemblyResolve, AddressOf AssemblyResolve_EventHandler

然后处理解析:

Private Function AssemblyResolve_EventHandler(ByVal sender As Object, ByVal e As System.ResolveEventArgs) As System.Reflection.Assembly
    Dim asm() As System.Reflection.Assembly = System.AppDomain.CurrentDomain.GetAssemblies()

    For i As Integer = 0 To asm.Length
        If asm(i).FullName = e.Name Then
            Return asm(i)
        End If
    Next
    Return Nothing
End Function

After a fair amount of digging around, I have found the solution, this will work for an assembly that is loaded by cannot be resolved:

Add a handler for the Assembly Resolution:

    Dim currentDomain As AppDomain = AppDomain.CurrentDomain
    AddHandler currentDomain.AssemblyResolve, AddressOf AssemblyResolve_EventHandler

then to handle the resolution:

Private Function AssemblyResolve_EventHandler(ByVal sender As Object, ByVal e As System.ResolveEventArgs) As System.Reflection.Assembly
    Dim asm() As System.Reflection.Assembly = System.AppDomain.CurrentDomain.GetAssemblies()

    For i As Integer = 0 To asm.Length
        If asm(i).FullName = e.Name Then
            Return asm(i)
        End If
    Next
    Return Nothing
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文