C # Learning Notes IV (Attribute, Reflection, thread, thread Syn

Source: Internet
Author: User
Tags foreach join reflection sleep thread
Atributes:
Can add metadata in program, IT store with the program
Using ILDasm to check atributes

Usage
[Codereviewattribute ("08/08/2005", "Allan Zhang"),
Comment = "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 ' is reviewed ' {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" 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'll 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 would block the other thread is 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);
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.