在C#中将类或结构传递给DLL函数 [英] Passing classes or structs to a DLL function in C#

查看:184
本文介绍了在C#中将类或结构传递给DLL函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#工作,我创建了一个包含各种数学函数库的DLL。

我还有一个应用程序,它生成需要传递给DLL的各种数学值。

而不是将它们一个一个地传递给DLL,这很容易做到,我宁愿在应用程序中填充结构或类,然后将结构或类传递给DLL。一个对象。换句话说,应用程序创建值,将值放在结构或类中,然后将对象作为一个对象传递给DLL,该对象可用于填充DLL中的本地类或结构。

有人可以帮忙吗?提前致谢。



我的尝试:



我有尝试拨打电话:

I am working in C#, and I have created a DLL to contain a library of various math functions.
I also have an app that generates various math values that need to be passed to the DLL.
Rather than passing them one by one to the DLL, which is easy to do, Id rather populate a structure, or a class, in the app and then pass the struct or class to the DLL as one object. In other words, the app creates the values, places the values in the struct or class and then passes the object to the DLL as one object which could be used to populate a local class or struct in the DLL.
Can anyone help? Thanks in advance.

What I have tried:

I have tried in calling program:

public  class Paramiters
{

    public  double A1;
}




Paramiters Parms = new Paramiters();
Parms.A1 = 2.0;







			DllInfo dllinfo = new DllInfo();

			dllinfo.InitializeInfo(ref Paramiters Parms);

Generates Errors	CS0118	'Form1.Paramiters' is a type but is used like a variable	
Error	CS1003	Syntax error, ',' expected


$这个DLL中的b $ b








in the DLL...

public  class DLLInfo
{
    public  double A;
}

DLLInfo dllinfo = new DLLInfo();
public void InitializeInfo(ref DLLInfo dllinfo)
{
             No Errors generated
}

推荐答案

dllinfo.InitializeInfo(ref Parms);



当你声明一个方法时,你只需要指定参数类型,但是当你调用时不需要。


You only have to specify the parameter type when you declare a method, but not when you are calling it.


InitializeInfo 的定义需要一个 DLLInfo 对象作为参数,但是你试图传递一个 Paramiters 对象。还不清楚你期望它如何工作,除非你期望每个方法都采用完全相同的参数集。
The definition of InitializeInfo requires a DLLInfo object as its parameter, but you are trying to pass a Paramiters object. Also it is not clear how you expect this to work, unless you are expecting every method to take exactly the same set of parameters.


Quote:

默认情况下,所有参数都按C#中的值传递。如果明确包含out或ref修饰符,则参数仅通过引用传递。但是,您需要注意,当参数的类型是引用类型时,您将传递引用而不是实际对象。有关更多信息,请参阅我关于参数传递和C#程序员参考的文章。



[作者:Jon Skeet]

By default, all parameters are passed by value in C#. Parameters are only passed by reference if you explicitly include an out or ref modifier. However, you need to be aware that when the type of the parameter is a reference type, you’re passing a reference rather than an actual object. For more information, see my article on parameter passing and the C# Programmer’s Reference.

[Author: Jon Skeet]





必读。



使用接口设计C#软件 - 简单对话 [ ^ ]


这篇关于在C#中将类或结构传递给DLL函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆