一、创建程序集
1、打开vs创建一个类库,实现代码如下
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Wrox.ProCSharp.Assemblies
{
public class ShareDemo
{
private string[] quotes;
private Random random;
public ShareDemo(string fileName)
{
quotes = File.ReadAllLines(fileName);
random = new Random();
}
public string GetQuoteOfTheDay()
{
int index = random.Next(1, quotes.Length);
return quotes[index];
}
}
}
2、选中项目右键属性打开签名页签,选中为程序集签名并且生成密钥文件,完成后生成解决方案

二、安装共享程序集
1、打开VS开发人员命令工具

2、输入命令后回车完成程序集安装
gacutil /i dll路径 /f
三、引用程序集
1、创建一个客户端项目,点击引用,然后右键添加引用,在弹出的窗口中点击浏览找到安装后的程序集文件,程序集文件一般可以在C:\Windows\Microsoft.NET\assembly\GAC_MSIL\目录下搜索

2、引用完成后在调用是首先引用命名空间,然后便可以通过编程调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wrox.ProCSharp.Assemblies;
namespace Client
{
internal class Program
{
static void Main(string[] args)
{
var quotes = new ShareDemo("Quotes.txt");
for (int i = 0; i < 3; i++)
{
Console.WriteLine(quotes.GetQuoteOfTheDay());
Console.WriteLine();
}
Console.ReadKey();
}
}
}
本文详细介绍了如何使用VisualStudio创建C#程序集,包括设置签名、生成密钥文件,以及如何安装到全局assemblycache(GAC)和在客户端项目中引用并调用共享代码。
1299

被折叠的 条评论
为什么被折叠?



