using System; using System.Reflection; using System.Runtime; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; namespace Test { // [DefaultMemberAttribute("Item")] public class SimpleTest { [MyAttribute()] public delegate string myMethodDelegate(int myInt); [SerializableAttribute()] [StructLayoutAttribute(LayoutKind.Sequential, CharSet=CharSet.Unicode)] protected internal class NestedType { private int pData; private NestedType(int num) { } } private const bool yes = true; private const byte b = byte.MaxValue; private const int ONE = 1; private const string JOHN = "John"; [MyAttribute(true)] private const SimpleTest ST = null; protected int which; private readonly int pData = 1000; private readonly string LUO = "luo"; [MarshalAs(UnmanagedType.LPStr)] public string lpstr; [MarshalAs(UnmanagedType.ByValArray, SizeConst=20)] private int[] arr; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)] public string f2; private byte[] days = new byte[10]; private int count = 100; [MyAttribute()] public virtual int Which { get { return which; } [MyAttribute(false, Kind=1000)] set { which = value; } } public byte this[long i] { get { return days[(int)i]; } set { days[(int)i] = value; } } [MyAttribute()] protected static event ChangedEventHandler Changed; protected virtual void OnChanged(EventArgs e) { if (Changed != null) { Changed(this, e); } } private SimpleTest() { } protected SimpleTest(int w) { which = w; } protected SimpleTest(int w, string other) : this(w) { Console.WriteLine(other); } public static void Main(string[] argv) { new SimpleTest(100); Console.WriteLine("JOHN"); Console.WriteLine("\u0003Hello World\n\n"); test(); long l = 1000; double d = 12.3; testLong(new object[]{l, d}); new SimpleTest(100).testDupX(); testForLoop(); testWhileLoop(5); testLabelledStatement((long)6); arrayTest(); testSwitch(4); testException(true); testException(false); testFinally(true); testFinally(false); SimpleTest simpleTest = new SimpleTest(100); simpleTest.testLock(); simpleTest = new ChildClass(1000); simpleTest.testLock(); Console.WriteLine(simpleTest.testConstructor().which); simpleTest.foreachTest(); simpleTest.testDelegate(); simpleTest.Which = 1000; Console.WriteLine("Testing property Which = {0}", simpleTest.Which); Console.WriteLine("Testing indexer ..."); simpleTest[(long)0] = 100; Console.WriteLine(simpleTest[(long)0]); } private static int testLong(params object[] args) { return 0; } private void testDupX() { int i = 0; int j = count += 10; Console.WriteLine(String.Concat("You should see 110 here : ", j)); Console.WriteLine(String.Concat("You should see 110 here : ", (i = count))); } private static void arrayTest() { int[] nums1 = new int[10]; string[] strs = new string[10]; for (int i = 0; i < (int)nums1.Length; i++) { nums1[i] = i; strs[i] = "Christina Zhang"; } Console.WriteLine(nums1[3]); Console.WriteLine(strs[3]); int[,] nums2 = new int[10, 10]; int[,] nums3 = new int[6, 20]; } private static void test() { int i = 100; if (i < 100) { i++; } else { i--; } bool flag1 = false; bool flag2 = flag1 == false; Console.WriteLine(i); } private static void testForLoop() { int i = 0; for (int j = 0; (long)j < (long)1000; j++) { i++; } Console.WriteLine(i); } private static void testWhileLoop(int limit) { for (int i = 0; i < limit; i++) { Console.WriteLine(i); } } private static void testContinueStatement(long l) { l++; for (int i = 0; i < 5; i++) { if (i != 2) { l++; } } Console.WriteLine(l); } private static void testBreakStatement(long l) { int i; l++; for (i = 0; i < 5 && i != 2; i++) { l++; } Console.WriteLine(l); } private static void testLabelledStatement(long l) { l++; int i = 0; int j = 0; for (i = 0; i < 5; i++) { for (j++; (long)j < l; j++) { if (j == 4) { goto IL_000a; } } l++; } Console.WriteLine((int)l); } private int loopTest(int limit) { int i; for (i = 0; i < limit; i++) { if (i != 0) { i++; i += 5; if (i == 10) { break; } } } int j = i; return j; } private static void testSwitch(int i) { string str = null; switch (i) { case 1: str = "Jan"; break; case 2: str = "Feb"; break; case 3: str = "Mar"; break; case 4: str = "Apr"; break; case 5: str = "May"; break; case 6: str = "Jun"; break; case 7: str = "Jul"; break; case 8: str = "Aug"; break; case 12: str = "Dec"; break; default: str = "Default"; break; } Console.WriteLine(str); } private static void testSwitch_1(int i) { string str = null; switch (i) { case 1: str = "Jan"; goto IL_0066; case 2: str = "Feb"; break; case 3: str = "Mar"; break; case 4: str = "Apr"; break; case 5: str = "May"; break; case 6: str = "Jun"; break; case 7: str = "Jul"; break; case 8: str = "Aug"; break; case 12: str = "Dec"; break; default: str = "Default"; break; } Console.WriteLine(str); } private static void testSwitch_2(int i) { string str = null; switch (i) { case 1: if (str != null) { goto IL_006d; } str = "Jan"; break; case 2: str = "Feb"; break; case 3: str = "Mar"; break; case 4: str = "Apr"; break; case 5: str = "May"; break; case 6: str = "Jun"; break; case 7: str = "Jul"; break; case 8: str = "Aug"; break; case 12: str = "Dec"; break; default: str = "Default"; break; } Console.WriteLine(str); } private static void testException(bool throwit) { try { throwException(throwit); } catch (SimpleException e) { Console.WriteLine(e.msg); } } private static void testFinally(bool throwit) { try { throwException(throwit); } catch (SimpleException e) { Console.WriteLine(e.msg); } finally { Console.WriteLine("Finally clause called"); } } private static void throwException(bool throwit) { if (throwit) { throw new SimpleException(); } else { return; } } internal virtual void testLock() { SimpleTest simpleTest; Console.WriteLine("SimpleTest.testLock() called"); int i = 0; Monitor.Enter(simpleTest = this); try { i++; } finally { Monitor.Exit(simpleTest); } } internal void testLock1() { } internal ChildClass testConstructor() { Console.WriteLine("testConstructor() ..."); Type type = typeof(ChildClass); Type[] types = new Type[]{typeof(int)}; ConstructorInfo constructorInfo = type.GetConstructor(52, null, types, null); object[] locals = new object[]{100}; return (ChildClass)constructorInfo.Invoke(locals); } private void foreachTest() { int[] nums2 = new int[]{4, 5, 6, 1, 2, 3, -2, -1, 0}; for (int j = 0; j < (int)nums2.Length; j++) { Console.WriteLine(nums2[j]); } } private static string myStringMethod(int myInt) { string str; if (myInt > 0) { str = "positive"; } else if (myInt < 0) { str = "negative"; } else { str = "zero"; } return str; } private void testDelegate() { myMethodDelegate MyMethodDelegate = new myMethodDelegate(null, myStringMethod); Console.WriteLine("{0} is {1};", 5, MyMethodDelegate(5)); } private bool testOutParam(out int age) { age = 34; return true; } private bool testRefParam(ref int age) { age = 34; return true; } private void testIfStatement(int num) { int j; if (num != 0) { int i = 50; j = 200; } } [DllImportAttribute("HelloCS.dll", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)] private static extern void pinvokeTest(); [DllImportAttribute("HelloCS.dll", EntryPoint="writeMessage")] private static extern void pinvokeTest1(); [DllImportAttribute("HelloCS.dll", ExactSpelling=true, CharSet=CharSet.Ansi)] private static extern void pinvokeTest2(); [DllImportAttribute("HelloCS.dll", ExactSpelling=true, CharSet=CharSet.Ansi, PreserveSig=false)] private static extern void pinvokeTest3(); private void marshalTest([MarshalAs(UnmanagedType.LPArray, SizeConst=40)] int[] a) { } [return: MarshalAs(UnmanagedType.IDispatch)] public object marshalTest([MarshalAs(UnmanagedType.LPStr)] string s, int len, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] int[] a) { return null; } public void marshalTest1([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_PTR)] int[] a) { } public void marshalTest2([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_UI8 | VarEnum.VT_VECTOR)] int[] a) { } public void marshalTest3([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_I8 | VarEnum.VT_BYREF)] int[] a) { } public void marshalTest4([MarshalAs(UnmanagedType.CustomMarshaler, MarshalType="SimpleMarshaler")] int[] a) { } [MyAttribute("Christina", "Zhang")] private void customAttributeTest([MyAttribute(true)] int age) { } [MyAttribute(typeof(String), "Zhang")] [return: MyAttribute(true)] private int customAttributeTest1() { return 100; } [MyAttribute(new string[]{"Christina", "Zhang", "124"})] private void customAttributeTest2() { } [MyAttribute(TypeAttributes.Public, false)] private void customAttributeTest3() { } [MyAttribute(true)] private void customAttributeTest4() { } [MyAttribute(SimpleEnum.third)] private void customAttributeTest5() { } [MyAttribute(false, name="NamedField")] private void customAttributeTest6() { } [MyAttribute(Kind=100)] private void customAttributeTest7() { } [MyAttribute(TypeAttributes.Public)] private void customAttributeTest8() { } [MyAttribute(null, 123)] private void customAttributeTest9() { } [MyAttribute(ByteEnum.third)] private void customAttributeTest10() { } [MyAttribute(123, 'C')] private void customAttributeTest11() { } [MyAttribute(new uint[]{123, 456})] private void customAttributeTest12() { } private int tempLocalTest() { int i = 100; Console.WriteLine("tempLocalTest()"); int j = 10; return i; } } [MyAttribute()] public delegate void ChangedEventHandler(object sender, EventArgs e); public class SimpleException : Exception { internal string msg = "This is a simple exception"; } enum SimpleEnum { first = 1, second = 2, third = 3, } class MyAttribute : Attribute { public string name; private int kind; public int Kind { get { return kind; } set { kind = value; } } public MyAttribute() { } public MyAttribute(string s, int i) { } public MyAttribute(bool flag) { } public MyAttribute(string value1, string value2) { } public MyAttribute(string[] values) { } public MyAttribute(Type value1, string value2) { } public MyAttribute(TypeAttributes value1, bool value2) { } public MyAttribute(SimpleEnum value1) { } public MyAttribute(ByteEnum value1) { } public MyAttribute(ushort value1, char c) { } public MyAttribute(object o) { } } [GuidAttribute("B5713A00-BEB3-11D5-81ED-00C04F2725D9")] [ComImportAttribute()] class ExternalClass { } class ChildClass : SimpleTest { internal ChildClass(int i) : base(i) { } internal ChildClass(int i, int j) : base(i) { } internal override void testLock() { Console.WriteLine("ChildClass.testLock() called"); base.testLock(); } internal void testLock1() { } internal virtual void testLock2() { } } public enum ByteEnum : byte { first = 1, second = 2, third = 3, } }