缩短C#中的DllImport数量?

时间:2013-03-10 22:50:43

标签: c# .net dllimport interopservices

以下是我的C#代码示例。有没有办法减少DllImport属性的数量?

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 个答案:

答案 0 :(得分:2)

将方法公开为COM方法,或者围绕它们创建C ++ / CLI包装。