国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#學習筆記之四(Attribute, Reflection, Thread, Thread Syn

2024-07-21 02:19:43
字體:
來源:轉載
供稿:網友
atributes:
//can add metadata in program, it store with the program
//using ildasm to check atributes

//usage
[codereviewattribute("08/08/2005", "allan zhang",
comment = "that is a attribute test")]
class employee {}

//define target and atr
[attributeusage(attributetargets.class |
attributetargets.constructor |
attributetargets.interface |
attributetargets.method |
attributetargets.field |
attributetargets.property,
allowmultiple=true)]
class codereviewatrribute : system.attribute
{
private string reviewdate;
private string reviewername;
private string reviewcomments;
...
}

reflections: //look the metadata in program, discory, late binding

//display attribute in code
system.reflection.memberinfo inf = typeof(employee);
object[] atributes = inf.getcustomattributes(false);
foreach (object attribute in attributes)
{
codereviewattribute attr = (codereviewattribute) attribute;
console.writeline("this is code was reviewed on {0} by {1} /n comments {2}",
attr.date,
attr.name,
attr.comment )
}

//discovery
assembly asm = assembly.load("mscorlib.dll");
type[] types = asm.gettypes();
foreach(type type in types)
{
console.writeline("type is{0}, type);
console.writeline("/n{0} types found." types.length);
}

//latebinding
type mathtype =type.gettype("system.math");
object obj = activator.createrinstance(mathtype);

type[] parametertypes = new type[2];
parametertypes[0] = type.gettype("system.double");
parametertypes[1] = type.gettype("system.double");
methodinfo powinfo = mathtype.getmethod("pow", parametertypes);

object[] parameters = new object[2];
parameters[0] = 5;
parameters[1] = 3;
object returnvalue = powinfo.invoke(obj, parameters);
console.writeline("5 to the 3rd power is {0}, returnvalue);

thread:
//create implicit threads

using system.threading
thread threadone = new thread(new threadstart(countup));
thread threadtwo = new thread(new threadstart(countup));
threadone.name = "thread one";
threadtwo.name = "thread two";
threadone.start();
threadtwo.start();

//joining & killing threads
//join two threads in current threa
threadone.start();
threadtwo.start();
threadone.join();
threadtwo.join();
//this will print, after two above two threads ends
console.writeline("the other threads done");
//sleep(millisenconds)
threadone.sleep(1);
threadone.abort();

synchronization:
//1. no conflict between threads
interlocked.increment(ref synchvalue);
//2. lock method, this will block other thread when it enter into it
lock(this)
{
int temp = synchvalue;
temp++;
thread.sleep(2);
synchvalue = temp;
synchvalue = temp;
console.writeline("{0} count up: {1}",
thread.currentthread.name,
synchvalue);
}




發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和龙市| 六枝特区| 二连浩特市| 莲花县| 本溪| 宝坻区| 龙游县| 蒙自县| 铜山县| 五家渠市| 兴国县| 呼图壁县| 凌源市| 东阳市| 锡林郭勒盟| 郸城县| 合山市| 峨眉山市| 贵定县| 贵州省| 雷波县| 永丰县| 台北县| 康定县| 富源县| 岱山县| 墨竹工卡县| 门头沟区| 会泽县| 乌拉特中旗| 株洲县| 军事| 静乐县| 九江市| 中阳县| 盐津县| 碌曲县| 黎川县| 嵩明县| 育儿| 镇安县|