يجب أن تكون وسيطة السمة تعبيرًا مستمرًا هل هناك طريقة للتغلب على هذا في C#؟

StackOverflow https://stackoverflow.com/questions/2321103

  •  22-09-2019
  •  | 
  •  

سؤال

أنا أعمل على رمز (WinForms C# Net 3.5) يتضمن استخدام UNRAR.

    [DllImport("UNRAR64.DLL")]
    private static extern IntPtr RAROpenArchive(ref RAROpenArchiveData archiveData);
    [DllImport("UNRAR64.DLL")]
    private static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx archiveData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARCloseArchive(IntPtr hArcData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARReadHeader(IntPtr hArcData, ref RARHeaderData headerData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARReadHeaderEx(IntPtr hArcData, ref RARHeaderDataEx headerData);
    [DllImport("UNRAR64.DLL")]
    private static extern int RARProcessFile(IntPtr hArcData, int operation, [MarshalAs(UnmanagedType.LPStr)] string destPath, [MarshalAs(UnmanagedType.LPStr)] string destName);
    [DllImport("UNRAR64.DLL")]
    private static extern void RARSetCallback(IntPtr hArcData, UNRARCallback callback, int userData);
    [DllImport("UNRAR64.DLL")]
    private static extern void RARSetPassword(IntPtr hArcData, [MarshalAs(UnmanagedType.LPStr)] string password);

نظرًا لأنني أريد أن يعمل الرمز على كل من 32bit و 64bit ، أردت تعيين unrar64.dll أو unar.dll عبر سلسلة unrardll اعتمادًا على التحقق من نظام النظام.

    private void DllChoice() {
        if (SystemIs64Bit()) {
            sevenZipDll = "7z-x64.dll";
            unrarDll = "unrar.dll";
        } else {
            sevenZipDll = "7x-x32.dll";
            unrarDll = "unrar64.dll";
        }
    } 
    private static bool SystemIs64Bit() {
        return (IntPtr.Size == 8);
    }

تم إلقاء الخطأ:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

هل هناك طريقة سهلة حول هذا؟ ماذا ستكون الطريقة الصحيحة للقيام بذلك؟

هل كانت مفيدة؟

المحلول

لا :-) إنه جزء من المواصفات ... يجب أن يكون لديك مبنيان منفصلان لكل منصة (x86/x64). ما يمكنك فعله هو تحديد توجيه ما قبل المعالج ، ثم القيام بشيء مثل

#if x64
// ... define all x64 imports here
#else
// ... define all x86 imports here
#endif

نصائح أخرى

قم بإنشاء واجهة لواردات UNRAR وتنفيذ إصدارات 32 بت و 64 بت بشكل منفصل. إذا كانت 32 بت ، قم بتثبيت ضجة 32 بت ، وإلا قم بتثبيت ضجة 64 بت.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top