Here is my problem: in the PCL library, I am going to call unmanaged code from the C ++ DLL. This DLL comes in two versions (x86 and x64) and, for performance reasons, the correct library should be referenced depending on the platform in which the PCL library is built.
Since an attribute [DllImport]requires a constant string as the name of the library, this very convenient method is useless because the correct library must be determined at runtime. There are several โmanlyโ ways to load functions manually ( LoadLibrary, GetProcaddressand GetDelegateForFunctionPointer), but Im will make it more convenient for the programmer.
So, declaring an external function is not a problem. Well, the C # compiler detects the external and is worried that with the missing attribute the [DllImport]external may not be allowed when loading the type. Ok, I defined the attribute [MyImport]and put it in an external declaration and bingo, at least the compiler was happy.
At runtime, of course, I get a TypeLoadException becauce, my external one is really not resolved. This raises two questions:
1) Why is the compiler satisfied with any attribute? Is there any magic from the bootloader regarding using this attribute to resolve a pending external? This could be easily done by providing an interface that will be implemented by the attribute. Thus, the runtime will look for external search attributes for those who implement the โmagicโ interface.
2) How can I catch a TypeLoadException so that I can implement my own loader? This loader will iterate over all external data from the given type, read the [MyImport] attribute and allow the appearance in this way.
Any idea if one of the two ideas can be implemented or is there any other solution to the mentioned problem?
I appreciate English lessons, but in fact this is not what I ask :-))
Christian.