标签:c# 友元
internal:对友元程序集和内部可见.我们可以利用这一点,使我们开发的类库更加地安全.比如,你不喜欢你开发的ClassLib( DLL ),被其他公司或团队使用,就可以在使用internal , 当然还要加上友元程序集的相关知识.
上代码:
TestFrendsAbb 类库
ToolsBytes
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace TestFrendsAbb.com { internal class ToolsBytes { private static ToolsBytes ins; private static Object obj = 0; internal static ToolsBytes Instance { get { if (ins == null) { lock (obj) { if (ins == null) ins = new ToolsBytes(); } } return ins; } } private ToolsBytes() { } /// <summary> /// 组合 addArr 和 src 将addArr放在src的前面并返回 /// </summary> /// <param name="addArr">前</param> /// <param name="src">后</param> /// <returns></returns> internal Byte[] addTop(Byte[] addArr, Byte[] src) { if (addArr != null && addArr.Length > 0) { Byte[] bc = null; if (src != null) { bc = new Byte[addArr.Length + src.Length]; Buffer.BlockCopy(addArr, 0, bc, 0, addArr.Length); Buffer.BlockCopy(src, 0, bc, addArr.Length, src.Length); } else { bc = new Byte[addArr.Length]; Buffer.BlockCopy(addArr, 0, bc, 0, addArr.Length); } return bc; } else return src; } } }
Friend:
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using TestFrendsAbb.com; [assembly: InternalsVisibleTo("MCC")] namespace TestFrendsAbb { internal class Friend { //public Byte[] addTop(Byte[] add, Byte[] src) //{ // return ToolsBytes.Instance.addTop(add, src); //} internal Byte[] addTop2(Byte[] add, Byte[] src) { return ToolsBytes.Instance.addTop(add, src); } } }
MCC : 控制台应用程序
Program:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MCC { class Program { static void Main(string[] args) { TestFrendsAbb.Friend fr = new TestFrendsAbb.Friend(); Byte[] by1 = BitConverter.GetBytes(‘胡‘); Byte[] by2 = BitConverter.GetBytes(‘召‘); Byte[] bytes = fr.addTop2(by1, by2); StringBuilder sb = new StringBuilder(); sb.Append(BitConverter.ToChar(bytes, 0)); sb.Append(BitConverter.ToChar(bytes, 2)); Console.Write(sb); Console.Read(); } } }
Other:依然能调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MCC.com { class Other { //Friend fr = new Friend(); TestFrendsAbb.Friend fr = new TestFrendsAbb.Friend(); } }
本文出自 “Better_Power_Wisdom” 博客,请务必保留此出处http://aonaufly.blog.51cto.com/3554853/1834336
标签:c# 友元
原文地址:http://aonaufly.blog.51cto.com/3554853/1834336