Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
以C#为单位缩短DllImport的数量?_C#_.net_Dllimport_Interopservices - Fatal编程技术网

以C#为单位缩短DllImport的数量?

以C#为单位缩短DllImport的数量?,c#,.net,dllimport,interopservices,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);

这是我的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;
    }
}

要么将这些方法公开为COM方法,要么围绕它们创建一个C++/CLI包装。

您可以导入
LoadLibrary
GetProcAddress
并动态加载导入内容。@Blorgbeard:我有点困惑。不是同样数量的代码吗?是的,少一个
[DllImport]
。。也许我也带你去了一点literally@Blorgbeard:我希望我可以做一些事情,比如在类上导入,然后它将采用名称。但是所有这些代码都会生成,所以我不介意。但是会有500多个函数和200多个结构,因此它可能会变得非常大-1:标题似乎完全是任意应用的(即,不是一个真正的问题)。虽然这并没有真正降低复杂性,但它只是将其从调用方移动到DLL中。如果使用ATL创建COM服务器,大部分代码都是为您生成的,您只需编写与DLL导出方法等效的内容。但是没有必要像DllImport那样编写编组指令。