using System; using System.Reflection; using System.Runtime.InteropServices; namespace Test { // A delegate type for hooking up change notifications. [MyAttribute()] public delegate void ChangedEventHandler(object sender, EventArgs e); public class SimpleTest { protected int which; const bool yes = true; const byte b = 255; const int ONE = 1; const System.String JOHN = "John"; [MyAttribute(true)] const SimpleTest ST = null; readonly int pData = 1000; 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; [MyAttribute()] // property public virtual int Which { get { return which; } [MyAttribute(false, Kind=1000)] set { which = value; } } byte[] days = new byte[10]; // indexer public byte this[long i] // long is a 64-bit integer { // Read one byte at offset index and return it. get { return days[i]; } set { days[i] = value; } } // An event that clients can use to be notified whenever the // elements of the list change. [MyAttribute()] static protected event ChangedEventHandler Changed; // Invoke the Changed event; called whenever list changes protected virtual void OnChanged(EventArgs e) { if (Changed != null) Changed(this, e); } 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); String s = "\u0003Hello World\n\n"; Console.WriteLine(s); test(); long l = 1000; double d = 12.3; testLong(l, d); new SimpleTest(100).testDupX(); testForLoop(); testWhileLoop(5); //testTypeCast(s); testLabelledStatement(6); arrayTest(); testSwitch(4); testException(true); testException(false); testFinally(true); testFinally(false); SimpleTest st = new SimpleTest(100); st.testLock(); st = new ChildClass(1000); st.testLock(); ChildClass cc = st.testConstructor(); Console.WriteLine(cc.which); st.foreachTest(); st.testDelegate(); st.Which = 1000; Console.WriteLine("Testing property Which = {0}", st.Which); Console.WriteLine("Testing indexer ..."); st[0] = 100; Console.WriteLine(st[0]); } static int testLong(params object[] args) { return 0; } int count = 100; /** JVM has dup_x1, dup_x2 instructions, but MSIL does not have such thing * This routine test if our translation works. */ void testDupX() { /* Console.WriteLine("You should see 100 here : " + count++); Console.WriteLine("You should see 101 here : " + count); count = 100; */ int j = 0; int val = count+=10; Console.WriteLine("You should see 110 here : " + val); Console.WriteLine("You should see 110 here : " + (j = count)); } static void arrayTest() { int[] intArray = new int[10]; String[] strArray = new String[10]; for (int i=0;i 0 ) return( "positive" ); if ( myInt < 0 ) return( "negative" ); return ( "zero" ); } void testDelegate() { myMethodDelegate myD1 = new myMethodDelegate( myStringMethod ); // Invoke the delegates. Console.WriteLine( "{0} is {1};", 5, myD1( 5 )); } bool testOutParam(out int age) { age = 34; return true; } bool testRefParam(ref int age) { age = 34; return true; } void testIfStatement(int num) { if (num == 0) { return; } else { int i = 50; } int j = 200; } [DllImport("HelloCS.dll", CharSet=CharSet.Unicode, CallingConvention=CallingConvention.StdCall)] extern static void pinvokeTest(); [DllImport("HelloCS.dll", EntryPoint="writeMessage")] extern static void pinvokeTest1(); [DllImport("HelloCS.dll", CharSet=CharSet.Ansi, ExactSpelling=true)] extern static void pinvokeTest2(); [DllImport("HelloCS.dll", CharSet=CharSet.Ansi, ExactSpelling=true, PreserveSig=false)] extern static void pinvokeTest3(); 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")] void customAttributeTest([MyAttribute(true)] int age) { } [MyAttribute(typeof(string), "Zhang")] [return: MyAttribute(true)] int customAttributeTest1() { return 100; } [MyAttribute(new string[]{"Christina", "Zhang", "124"})] void customAttributeTest2() { } [MyAttribute(TypeAttributes.Public, false)] void customAttributeTest3() { } [MyAttribute(true)] void customAttributeTest4() { } [MyAttribute(SimpleEnum.third)] void customAttributeTest5() { } [MyAttribute(false, name="NamedField")] void customAttributeTest6() { } [MyAttribute(Kind = 100)] void customAttributeTest7() { } [MyAttribute(System.Reflection.TypeAttributes.Public)] void customAttributeTest8() { } [MyAttribute(null, 123)] void customAttributeTest9() { } [MyAttribute(ByteEnum.third)] void customAttributeTest10() { } [MyAttribute(123, 'C')] void customAttributeTest11() { } [MyAttribute(new uint[]{123, 456})] void customAttributeTest12() { } int tempLocalTest() { int tmp = 100; Console.WriteLine("tempLocalTest()"); int j = 10; return tmp; } [Serializable()] [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] protected internal class NestedType { int pData; NestedType(int num) { } } } internal 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(); } new internal void testLock1() { } internal virtual void testLock2() { } } public class SimpleException : System.Exception { internal String msg = "This is a simple exception"; } 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) { } } enum SimpleEnum { first = 1, second = 2, third = 3 } public enum ByteEnum : byte { first = 1, second = 2, third = 3, } [ComImport()] [Guid("B5713A00-BEB3-11D5-81ED-00C04F2725D9")] class ExternalClass {} }