在 C# 中将值从程序集传递到应用程序

发布于 2024-12-19 23:05:17 字数 728 浏览 0 评论 0原文

如果我有一个程序集,其中包含一个操作字符串然后返回该字符串的函数,然后我有一个单独的 C# 应用程序,它从程序集中调用该函数...

如何将操作的字符串从程序集传递到应用程序中?

例如,如果我有这个程序集...

using System;

namespace test
{

    public class Class1
    {
        string inputString = "hello";
        string outputString;

        public static string Convert(ref inputString, ref outputString)
        {
            outputString = inputString.ToUpper();
            return outputString;
        }
    }
}

并且我有一个调用程序集中的 Convert 函数的应用程序...

using System;
using test;

public class Class2
{
    public static void Main()
    {
        Class1.Convert();
    }
}

如何将返回的 outputString 获取到 Class2 应用程序中? 我无法在 Main() 函数中引用它,那么如何传递它呢?

If I have an assembly which contains a function that manipulates a string and then returns that string, and I then have a separate C# application which calls the function from the assembly...

How can I pass the manipulated string from the assembly into the application?

For example, If I have this assembly...

using System;

namespace test
{

    public class Class1
    {
        string inputString = "hello";
        string outputString;

        public static string Convert(ref inputString, ref outputString)
        {
            outputString = inputString.ToUpper();
            return outputString;
        }
    }
}

And I have this application which calls the Convert function within the assembly...

using System;
using test;

public class Class2
{
    public static void Main()
    {
        Class1.Convert();
    }
}

How can I get the returned outputString into the Class2 application?
I can't reference it in the Main() function so how can I pass it in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

镜花水月 2024-12-26 23:05:17

看起来你想传入一个有效的值,然后返回另一个值?然后去掉 Class1 上的属性,它们不是必需的。只需将输入字符串作为 Convert 函数的参数,然后返回输出即可。

using System;

namespace test
{

    public class Class1
    {
        public static string Convert(string inputString)
        {
            string outputString = inputString.ToUpper();
            return outputString;
        }
    }
}

和:

using System;
using test;

public class Class2
{
    public static void Main()
    {
        string thisIsMyReturnedString = Class1.Convert("whatever the input value should be");
    }
}

It looks like you want to pass in a valid and then return another value? Then get rid of the of properties on your Class1, they are not necessary. Just make the input string the parameter of the Convert function, and then return the output.

using System;

namespace test
{

    public class Class1
    {
        public static string Convert(string inputString)
        {
            string outputString = inputString.ToUpper();
            return outputString;
        }
    }
}

and:

using System;
using test;

public class Class2
{
    public static void Main()
    {
        string thisIsMyReturnedString = Class1.Convert("whatever the input value should be");
    }
}
ぶ宁プ宁ぶ 2024-12-26 23:05:17

编辑:

您的 Convert() 函数只需要接受一个参数,因为您要返回另一个值。除非您要在函数中修改参数,否则不应将参数标记为 ref


替换:

Class1.Convert();

string inputValue = "old value";
string returnValue = Class1.Convert(ref inputValue);

EDIT:

Your Convert() function only needs to take in one parameter, since you're returning the other value. The parameter shouldn't be marked as ref unless you're going to modify it within your function.


Replace:

Class1.Convert();

with

string inputValue = "old value";
string returnValue = Class1.Convert(ref inputValue);
明月夜 2024-12-26 23:05:17

您需要在主项目中添加对 dll 的引用:

在主项目中,右键单击“引用 -->”添加参考
完成后,您可以像这样使用 Convert:

string result = Class1.Convert(ref myInputString, ref myOutputString);

顺便说一下,您的返回是无用的,您已经在使用参数作为参考。
你的方法的签名应该为空:

public static void Convert(ref inputString, ref outputString)
{
    outputString = inputString.ToUpper();
}

然后你可以这样调用它:

string input = "My InPuT";
string output = "";
Class1.Convert(ref input, ref output);
//Here, output = "MY INPUT"

You need to add reference to your dll in the main project:

In the main project, right click References --> Add Reference
Once that is done, you can use Convert like this:

string result = Class1.Convert(ref myInputString, ref myOutputString);

On a sidenote, your return is useless, you already are using the parameters as reference.
Your method's signature should be void:

public static void Convert(ref inputString, ref outputString)
{
    outputString = inputString.ToUpper();
}

Then you'd call it like this:

string input = "My InPuT";
string output = "";
Class1.Convert(ref input, ref output);
//Here, output = "MY INPUT"
栖竹 2024-12-26 23:05:17

如果同一解决方案中有两个不同的项目,请添加对该项目的引用。如果是两个不同的解决方案,请添加对 dll 的引用。

If it's two different projects inside the same solution add a reference to the project. If it's two different solutions, add a reference to the dll.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文