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

首頁 > 開發 > 綜合 > 正文

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

2024-07-21 02:27:58
字體:
來源:轉載
供稿:網友

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);
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 呼伦贝尔市| 克拉玛依市| 广河县| 周至县| 合作市| 大港区| 三河市| 成都市| 海门市| 中方县| 门源| 金华市| 金乡县| 松滋市| 罗甸县| 井研县| 和平县| 乐安县| 宁河县| 尉氏县| 博野县| 荆门市| 贺兰县| 黄山市| 柳州市| 德昌县| 贡觉县| 白朗县| 株洲市| 双辽市| 汪清县| 高雄县| 建水县| 许昌市| 蛟河市| 雷州市| 泽州县| 高青县| 广宁县| 靖江市| 吉安市|