Shorten amount of DllImport in C#?

允我心安 提交于 2020-02-07 08:26:15

问题


Here is a sample of my C# code. Is there a way to decrease the amount of DllImport attributes?

namespace CSLib
{
    class Program
    {
        static void Main(string[] args)
        {
            CLib.test();
            CLib.test2(3);
            A a = new A() { a = 9, b = 5 };
            CLib.test3(ref a);
        }
    }
    class CLib
    {
        [DllImport("path/to/CDLL", CallingConvention = CallingConvention.Cdecl)]
        public static extern void test();

        [DllImport("path/to/CDLL", CallingConvention = CallingConvention.Cdecl)]
        public static extern void test2(int a);

        [DllImport("path/to/CDLL", CallingConvention = CallingConvention.Cdecl)]
        public static extern void test3(ref A a);
    }

    [StructLayout(LayoutKind.Sequential)]
    struct A
    {
        [MarshalAs(UnmanagedType.I4)]
        public int a, b;
    }
}

回答1:


Either expose the methods as COM methods, or create a C++/CLI wrapper around them.



来源:https://stackoverflow.com/questions/15328712/shorten-amount-of-dllimport-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!