Sunday, February 13, 2011

Help with using TypeConverters for enums in an Excel PIA assembly

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?

  • 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.

    GalleySlave : The type converter is in the same dll, My suspicion is that it is probably down to the way Excel / (Office) loads PIAs.
  • 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
    

0 comments:

Post a Comment