C#友元程序集
internal:对友元程序集和内部可见.我们可以利用这一点,使我们开发的类库更加地安全.比如,你不喜欢你开发的ClassLib( DLL ),被其他公司或团队使用,就可以在使用internal , 当然还要加上友元程序集的相关知识.
上代码:
TestFrendsAbb 类库
ToolsBytes
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceTestFrendsAbb.com{internalclassToolsBytes{privatestaticToolsBytesins;privatestaticObjectobj=0;internalstaticToolsBytesInstance{get{if(ins==null){lock(obj){if(ins==null)ins=newToolsBytes();}}returnins;}}privateToolsBytes(){}///<summary>///组合addArr和src将addArr放在src的前面并返回///</summary>///<paramname="addArr">前</param>///<paramname="src">后</param>///<returns></returns>internalByte[]addTop(Byte[]addArr,Byte[]src){if(addArr!=null&&addArr.Length>0){Byte[]bc=null;if(src!=null){bc=newByte[addArr.Length+src.Length];Buffer.BlockCopy(addArr,0,bc,0,addArr.Length);Buffer.BlockCopy(src,0,bc,addArr.Length,src.Length);}else{bc=newByte[addArr.Length];Buffer.BlockCopy(addArr,0,bc,0,addArr.Length);}returnbc;}elsereturnsrc;}}}
Friend:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Runtime.CompilerServices;usingSystem.Text;usingSystem.Threading.Tasks;usingTestFrendsAbb.com;[assembly:InternalsVisibleTo("MCC")]namespaceTestFrendsAbb{internalclassFriend{//publicByte[]addTop(Byte[]add,Byte[]src)//{//returnToolsBytes.Instance.addTop(add,src);//}internalByte[]addTop2(Byte[]add,Byte[]src){returnToolsBytes.Instance.addTop(add,src);}}}
MCC : 控制台应用程序
Program:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceMCC{classProgram{staticvoidMain(string[]args){TestFrendsAbb.Friendfr=newTestFrendsAbb.Friend();Byte[]by1=BitConverter.GetBytes('胡');Byte[]by2=BitConverter.GetBytes('召');Byte[]bytes=fr.addTop2(by1,by2);StringBuildersb=newStringBuilder();sb.Append(BitConverter.ToChar(bytes,0));sb.Append(BitConverter.ToChar(bytes,2));Console.Write(sb);Console.Read();}}}
Other:依然能调用
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceMCC.com{classOther{//Friendfr=newFriend();TestFrendsAbb.Friendfr=newTestFrendsAbb.Friend();}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。