将几个变量从C#DLL传递给调用C#应用程序 [英] Pass several variables from C# DLL to calling C# application

查看:399
本文介绍了将几个变量从C#DLL传递给调用C#应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先尝试使用公共属性,数组,并从调用应用程序调用该数组。找不到办法做到这一点。传递dll中生成的多个值的最简单方法是什么?我知道我可以在dll中生成一个字符串,变量用逗号分隔,然后在调用程序中从中创建一个数组。有没有更直接的方法?



我尝试过:



我试图在dll中使用数组属性但没有成功。

I first tried to use a public property, array, and call the array from the calling app. Can't find a way to do that. What is the simplest method to achieve passing a multiple of values that are generated in the dll? I know I can generated a string in the dll with the variables separated by comma, then create an array from this in the calling program. Is there any more direct method?

What I have tried:

I have tried to use an array property in the dll without success.

推荐答案

有很多方法可以做到这一点,但你遗漏了所有重要的细节你的描述所以不可能说什么更合适。



你可以返回一个数组,一个ArrayList,一个List,一个List< t>,一个用来携带的结构一堆不同的类型,或者包含属性值的类实例。

您可以将值返回给调用方法的变量并通过引用传入变量。



有很多选项,但是因为我们可以看不到你的代码(你没有发布任何片段),我们无法告诉你你做错了什么。
There's a variety of ways to do this, but you left out all the important details in your description so it's impossible to say what's more appropriate.

You can return an array, an ArrayList, a List, a List<t>, a structure designed to carry a bunch of different types at once, or a class instance that holds the values in properties.
You can return values to variable that call the method and pass in variables by reference.

There's lots of options, but since we can't see you're code (you didn't post any snippets), we can't tell you what you're doing wrong.


它是一个DLL的事实是无关紧要的 - 无论包含数据的类在哪里,程序都是相同的。

停止并考虑您想要传递的数据,以及如果它在同一个程序集中,您将如何处理它。弄清楚你将如何做到这一点,它与DLL中的类相同 - 你需要做的就是将DLL的引用添加到项目中,并使用 $ c>声明到源文件以访问它。

然后它完全相同:静态数据通过类名访问,实例数据通过您创建的类的实例访问:

The fact that it's a DLL is irrelevant - the procedure is the same regardless of where the class containing the data is.
Stop and think about the data you want to pass, and how you would handle it if it was in the same assembly. Work out how you would do it there, and it's the same solution for a class in a DLL - all you need to do is add the reference to the DLL to your project, and a using statement to the source file to access it.
Then it's exactly the same: static data is accessed via the class name, instance data via the instance of the class you created:
public class InTheDLL
    {
    public static string[] StaticArrayOfStrings { get { ... } set { } }
    public List<MyClass> Items { get{ ...} set { } }
    }







string[] strings = InTheDLL.StaticArrayOfStrings;
InTheDLL instance = new InTheDLL();
List<MyClass> collection = instance.Items;

您可以返回任何内容,包括多个属性,元组,无论您的数据需要什么。

You can return anything, including multiple properties, tuples, whatever your data requires.


这篇关于将几个变量从C#DLL传递给调用C#应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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