C# 从.dll类调用函数并将其与自定义类C连接#

C# 从.dll类调用函数并将其与自定义类C连接#,c#,C#,我有一个库(不是我的),用于C#中的网格,这个库是在.dll中编译的。所以我无法访问内部代码 然而,我编写了自定义静态类,该类中充满了该库的帮助函数 我注意到每个函数都是这样写的: public static class MeshUtilities{ public static functionA (Mesh mesh, some other variable) { ... } public static functionB (Mesh mesh, some other variabl

我有一个库(不是我的),用于C#中的网格,这个库是在.dll中编译的。所以我无法访问内部代码

然而,我编写了自定义静态类,该类中充满了该库的帮助函数

我注意到每个函数都是这样写的:

public static class MeshUtilities{

 public static functionA (Mesh mesh, some other variable)
 { ... }

 public static functionB (Mesh mesh, some other variable)
 { ... }

 public static functionC (Mesh mesh, some other variable)
 { ... }

}
Mesh ourMesh = new Mesh();
ourMesh.functionC(someVariable);
在我的应用程序中,我将这些函数称为MeshUtilities.function(mesh,其他变量)

但正如您所看到的,每个函数都是从类型Mesh开始的


有没有什么方法可以让我编写这个helper类,这样我就只需要从MyCustomHelperClass(一些其他变量)编写mesh.FunctionFromMyCustomHelperClass了?

,除非我误解了你的最后一段。在第一个参数的类型之前添加此:

public static class MeshUtilities{

     public static void functionA (this Mesh mesh, object someOtherVariable)
     {
         mesh.SomeOtherMethod(baz.foobar());
         //  etc.
     }
这样使用:

var mesh = new Mesh();

mesh.functionA(3);

你为什么不把它变成一个扩展类呢

public static class MeshUtilities{

 public static functionA (this Mesh mesh, some other variable)
 { ... }

 public static functionB (this Mesh mesh, some other variable)
 { ... }

 public static functionC (this Mesh mesh, some other variable)
 { ... }

}
这样称呼它:

public static class MeshUtilities{

 public static functionA (Mesh mesh, some other variable)
 { ... }

 public static functionB (Mesh mesh, some other variable)
 { ... }

 public static functionC (Mesh mesh, some other variable)
 { ... }

}
Mesh ourMesh = new Mesh();
ourMesh.functionC(someVariable);