C#控制台程序调用DLL
C#控制台程序调用DLL
C#控制台程序调用DLL这个问题困扰我好几天了,今天竟然解决了,于是就想记下来,即为了防止以后忘记,也给遭受这个困扰的朋友一个参考。
本例为整形数组排序,即把排序法做成一个DLL,然后在控制台中调用
1.首先建立一个DLL程序:整形数组排序,并编译生成DLL文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace整形数组排序
{
public class Class1
{
public int[] IntegeArraySort(int[] array)
{
Console.WriteLine("请选择排序方式,A-升序,D-降序");
string input = Console.ReadLine();
int temp = 0;
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = i + 1; j < array.Length; j++)
{
if (input == "A")
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
else if (input == "D")
{
if (array[i] < array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
else
{
Console.WriteLine("输入错误");
}
}
}
return array;
}
}
}
2.建立一个控制台程序:DLL测试,首先点击【引用】-【添加引用】,然后点浏
览,找到刚才编译生成的整形数组排序.dll,然后点确定就好了。
这时候我们看到引用里多出了一项,然后添加一下命名空
间:using整形数组排序; 然后就可以开始测试了
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using 整形数组排序;
namespace DLL测试
{
class Program
{
static void Main(string[] args)
{
Class1 cl = new Class1();
int[] i = new int[] { 1,3,9,5,4,8,7,6,2};
cl.IntegeArraySort(i);
foreach (int a in i)
{
Console.Write(a + " ");
}
Console.ReadKey();
}
}
}
现在就可以进行测试运行了。
到这里,本例就结束了,希望能给正在困惑中的朋友以帮助。