Andrey Bushman
Andrey Bushman

Reputation: 12516

Is exists any way to calculate the parameter of attribute dynamically?

Exists some imported function. It is used in the AutoCAD 2009 plugin. But for newer AutoCAD versions it is to use acdb18.dll, acdb19.dll and acdb20.dll. Of course, I can add the similar import record for each AutoCAD version, but maybe exist more convenient way with dynamic replacing 18, 19, or 20 instead of 17? I think it is not possible, but I ask my question for to be sure.

[DllImport("acdb17.dll", CallingConvention = CallingConvention.Cdecl, 
EntryPoint = "?acdbSetDbmod@@YAJPAVAcDbDatabase@@J@Z")]
private static extern Int32 acdbSetDbmod17x86(IntPtr db, Int32 newDbMod);

Upvotes: 1

Views: 128

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391634

No, you cannot calculate the attribute parameters, these are embedded in a different way than for normal executing code and has to be constant.

Instead you should create 4 distinct methods, one for each such library, and have the surrounding code figure out which one to call.

ie. something like this:

[DllImport("acdb17.dll", CallingConvention = CallingConvention.Cdecl, 
EntryPoint = "?acdbSetDbmod@@YAJPAVAcDbDatabase@@J@Z")]
private static extern Int32 v17_acdbSetDbmod17x86(IntPtr db, Int32 newDbMod);

[DllImport("acdb18.dll", CallingConvention = CallingConvention.Cdecl, 
EntryPoint = "?acdbSetDbmod@@YAJPAVAcDbDatabase@@J@Z")]
private static extern Int32 v18_acdbSetDbmod17x86(IntPtr db, Int32 newDbMod);

switch (version)
{
    case 17: v17_acdbSetDbmod17x86(...);
    case 18: v18_acdbSetDbmod17x86(...);
             ^-+^
               |
               +-- notice the prefix to the methods

Or, you should create 4 distinct classes all implementing the same interface and pick the right class at startup:

public class AutoCADAPI17 : IAutoCADAPI
{
    ....

public class AutoCADAPI18 : IAutoCADAPI
{
    ....

This way you would just pick the right implementation once and go to the right methods each time without having to switch.

Upvotes: 2

Related Questions