本文介绍了缩短C#中的DllImport数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
    }
}

推荐答案

要么将方法公开为COM方法,要么在它们周围创建C ++/CLI包装器.

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

这篇关于缩短C#中的DllImport数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 21:03