public static int Main(string[] args) { TestClass13 objTestClass13 = new TestClass13(); objTestClass13.Method0(); return(100); }
public static int Main(string[] args) { try { TestClass13 objTestClass13 = new TestClass13(); objTestClass13.Method0(); } catch (Exception) { // ignore exceptions } return(100); }
/// <summary> /// 对 <see cref="Cyjb.TypeExt.IsImplicitFrom"/> 方法进行测试的辅助方法。 /// </summary> public void TestImplicitChangeTypeHelper(Func <object, Type, object> func) { object value = new object(); #region 6.1.1 标识转换 Assert.AreEqual(value, func(value, typeof(object))); value = true; Assert.AreEqual(value, func(value, typeof(bool))); value = 'A'; Assert.AreEqual(value, func(value, typeof(char))); value = (sbyte)10; Assert.AreEqual(value, func(value, typeof(sbyte))); value = (short)11; Assert.AreEqual(value, func(value, typeof(short))); value = (ushort)12; Assert.AreEqual(value, func(value, typeof(ushort))); value = (int)13; Assert.AreEqual(value, func(value, typeof(int))); value = (uint)14; Assert.AreEqual(value, func(value, typeof(uint))); value = (long)15; Assert.AreEqual(value, func(value, typeof(long))); value = (ulong)16; Assert.AreEqual(value, func(value, typeof(ulong))); value = (float)17; Assert.AreEqual(value, func(value, typeof(float))); value = (double)18; Assert.AreEqual(value, func(value, typeof(double))); value = (decimal)19; Assert.AreEqual(value, func(value, typeof(decimal))); value = new TestClass(); Assert.AreEqual(value, func(value, typeof(TestClass))); // 这里还有一点是 dynamic 和 object 是等效的,但由于在运行时 // dynamic 和 object 没有区别(参见规范 4.7 节), // 因此在判断类型转换时完全不用考虑它。 #endregion // 6.1.1 标识转换 #region 6.1.2 隐式数值转换 Assert.AreEqual((short)10, func((sbyte)10, typeof(short))); Assert.AreEqual((int)11, func((sbyte)11, typeof(int))); Assert.AreEqual((long)12, func((sbyte)12, typeof(long))); Assert.AreEqual((float)13, func((sbyte)13, typeof(float))); Assert.AreEqual((double)14, func((sbyte)14, typeof(double))); Assert.AreEqual((decimal)15, func((sbyte)15, typeof(decimal))); Assert.AreEqual((short)10, func((byte)10, typeof(short))); Assert.AreEqual((ushort)11, func((byte)11, typeof(ushort))); Assert.AreEqual((int)12, func((byte)12, typeof(int))); Assert.AreEqual((uint)13, func((byte)13, typeof(uint))); Assert.AreEqual((long)14, func((byte)14, typeof(long))); Assert.AreEqual((ulong)15, func((byte)15, typeof(ulong))); Assert.AreEqual((float)16, func((byte)16, typeof(float))); Assert.AreEqual((double)17, func((byte)17, typeof(double))); Assert.AreEqual((decimal)18, func((byte)18, typeof(decimal))); Assert.AreEqual((int)12, func((short)12, typeof(int))); Assert.AreEqual((long)14, func((short)14, typeof(long))); Assert.AreEqual((float)16, func((short)16, typeof(float))); Assert.AreEqual((double)17, func((short)17, typeof(double))); Assert.AreEqual((decimal)18, func((short)18, typeof(decimal))); Assert.AreEqual((int)12, func((ushort)12, typeof(int))); Assert.AreEqual((uint)13, func((ushort)13, typeof(uint))); Assert.AreEqual((long)14, func((ushort)14, typeof(long))); Assert.AreEqual((ulong)15, func((ushort)15, typeof(ulong))); Assert.AreEqual((float)16, func((ushort)16, typeof(float))); Assert.AreEqual((double)17, func((ushort)17, typeof(double))); Assert.AreEqual((decimal)18, func((ushort)18, typeof(decimal))); Assert.AreEqual((long)14, func((int)14, typeof(long))); Assert.AreEqual((float)16, func((int)16, typeof(float))); Assert.AreEqual((double)17, func((int)17, typeof(double))); Assert.AreEqual((decimal)18, func((int)18, typeof(decimal))); Assert.AreEqual((long)14, func((uint)14, typeof(long))); Assert.AreEqual((ulong)15, func((uint)15, typeof(ulong))); Assert.AreEqual((float)16, func((uint)16, typeof(float))); Assert.AreEqual((double)17, func((uint)17, typeof(double))); Assert.AreEqual((decimal)18, func((uint)18, typeof(decimal))); Assert.AreEqual((float)16, func((long)16, typeof(float))); Assert.AreEqual((double)17, func((long)17, typeof(double))); Assert.AreEqual((decimal)18, func((long)18, typeof(decimal))); Assert.AreEqual((float)16, func((ulong)16, typeof(float))); Assert.AreEqual((double)17, func((ulong)17, typeof(double))); Assert.AreEqual((decimal)18, func((ulong)18, typeof(decimal))); Assert.AreEqual((ushort)11, func((char)11, typeof(ushort))); Assert.AreEqual((int)12, func((char)12, typeof(int))); Assert.AreEqual((uint)13, func((char)13, typeof(uint))); Assert.AreEqual((long)14, func((char)14, typeof(long))); Assert.AreEqual((ulong)15, func((char)15, typeof(ulong))); Assert.AreEqual((float)16, func((char)16, typeof(float))); Assert.AreEqual((double)17, func((char)17, typeof(double))); Assert.AreEqual((decimal)18, func((char)18, typeof(decimal))); Assert.AreEqual((double)17, func((float)17, typeof(double))); #endregion // 6.1.2 隐式数值转换 // 6.1.3 隐式枚举转换,针对的是十进制数字文本 0,不在考虑范围内。 #region 6.1.4 可以为 null 的隐式转换 // 从 S 到 T? 的隐式转换。 Assert.AreEqual((int?)10, func((int)10, typeof(int?))); Assert.AreEqual((long?)10, func((int)10, typeof(long?))); // 从 S? 到 T? 的隐式转换。 Assert.AreEqual((int?)10, func((int?)10, typeof(int?))); Assert.AreEqual((long?)10, func((int?)10, typeof(long?))); Assert.AreEqual((long?)null, func((int?)null, typeof(long?))); #endregion // 6.1.4 可以为 null 的隐式转换 // 6.1.5 null 文本转换,不在考虑范围内。 #region 6.1.6 隐式引用转换 // 6.1.6.1 从任何 reference-type 到 object (和 dynamic)。 value = "abc"; Assert.AreEqual((object)value, func(value, typeof(object))); value = new TestClass(); Assert.AreEqual((object)value, func(value, typeof(object))); // 6.1.6.2 从任何 class-type S 到任何 class-type T(前提是 S 是从 T 派生的)。 value = new TestClass2(); Assert.AreEqual((TestClass)value, func(value, typeof(TestClass))); // 6.1.6.3 从任何 class-type S 到任何 interface-type T(前提是 S 实现了 T)。 value = new List <int>(); Assert.AreEqual((IList <int>)value, func(value, typeof(IList <int>))); // 6.1.6.4 从任何 interface-type S 到任何 interface-type T(前提是 S 是从 T 派生的)。 value = new List <int>(); Assert.AreEqual((IList)value, func(value, typeof(IList))); // 6.1.6.5 从元素类型为 SE 的 array-type S 到元素类型为 TE 的 array-type T(前提是以下所列条件均成立): // o S 和 T 只有元素类型不同。换言之,S 和 T 具有相同的维数。 // o SE 和 TE 都是 reference-type。 // o 存在从 SE 到 TE 的隐式引用转换。 value = new string[0]; Assert.AreEqual((object[])value, func(value, typeof(object[]))); value = new TestClass2[0]; Assert.AreEqual((TestClass[])value, func(value, typeof(TestClass[]))); value = new List <int> [0]; Assert.AreEqual((IList[])value, func(value, typeof(IList[]))); value = new List <string> [0]; Assert.AreEqual(value, func(value, typeof(IEnumerable <object>[]))); // 6.1.6.6 从任何 array-type 到 System.Array 及其实现的接口。 value = new int[0]; Assert.AreEqual((Array)value, func(value, typeof(Array))); value = new object[0]; Assert.AreEqual((Array)value, func(value, typeof(Array))); // 6.1.6.7 从一维数组类型 S[] 到 System.Collections.Generic.IList<T> 及其基接口 // (前提是存在从 S 到 T 的隐式标识或引用转换)。 value = new int[0]; Assert.AreEqual((IList <int>)value, func(value, typeof(IList <int>))); value = new TestClass[0][][]; Assert.AreEqual((IList <IList <TestClass>[]>)value, func(value, typeof(IList <IList <TestClass>[]>))); // 6.1.6.8 从任何 delegate-type 到 System.Delegate 及其实现的接口。 value = new Func <int>(TestMethod); Assert.AreEqual((Delegate)value, func((Delegate)value, typeof(Delegate))); // 6.1.6.9 从 null 文本到任何 reference-type,不在考虑范围内。 // 6.1.6.10 从任何 reference-type 到 reference-type T // (前提是它具有到 reference-type T0 的隐式标识或引用转换,且 T0 具有到 T 的标识转换)。 // 此条规则也可以不考虑。 // 6.1.6.11 从任何 reference-type 到接口或委托类型 T // (前提是它具有到接口或委托类型 T0 的隐式标识或引用转换,且 T0 可变化转换为T)。 // 这里的变化转换在规范的 13.1.3.2 节,就是泛型的协变和逆变。 // 协变。 value = Enumerable.Empty <string>(); Assert.AreEqual((IEnumerable <object>)value, func(value, typeof(IEnumerable <object>))); value = Enumerable.Empty <IEqualityComparer <object>[]>(); Assert.AreEqual((IEnumerable <IEqualityComparer <string>[]>)value, func(value, typeof(IEnumerable <IEqualityComparer <string>[]>))); value = Enumerable.Empty <object[][]>(); Assert.AreEqual((IEnumerable <IList <IList <object> > >)value, func(value, typeof(IEnumerable <IList <IList <object> > >))); // 逆变。 value = EqualityComparer <object> .Default; Assert.AreEqual((IEqualityComparer <string>)value, func(value, typeof(IEqualityComparer <string>))); value = EqualityComparer <IEnumerable <object>[]> .Default; Assert.AreEqual((IEqualityComparer <IEnumerable <string>[]>)value, func(value, typeof(IEqualityComparer <IEnumerable <string>[]>))); value = EqualityComparer <IList <TestClass[]> > .Default; Assert.AreEqual((IEqualityComparer <TestClass2[][]>)value, func(value, typeof(IEqualityComparer <TestClass2[][]>))); // 6.1.6.12 涉及已知为引用类型的类型参数的隐式转换。这个转换是针对类型参数 T 的,因此同样不做考虑。 #endregion // 6.1.6 隐式引用转换 #region 6.1.7 装箱转换 // 从 non-nullable-value-type 到 object。 value = (uint)10; Assert.AreEqual((object)value, func(value, typeof(object))); value = new TestStruct(); Assert.AreEqual((object)value, func(value, typeof(object))); // 从 non-nullable-value-type 到 System.ValueType。 value = 10; Assert.AreEqual((ValueType)value, func(value, typeof(ValueType))); value = new TestStruct2(); Assert.AreEqual((ValueType)value, func(value, typeof(ValueType))); // 从 non-nullable-value-type 到其实现的接口。 value = 10; Assert.AreEqual((IComparable <int>)value, func(value, typeof(IComparable <int>))); value = new TestStruct4(); Assert.AreEqual((IEnumerable <string>)value, func(value, typeof(IEnumerable <string>))); // 从 enum-type 转换为 System.Enum 类型。 value = Tristate.True; Assert.AreEqual((Enum)value, func(value, typeof(Enum))); // 从 nullable-type 到引用类型的装箱转换,如果存在从对应的 non-nullable-value-type 到该引用类型的装箱转换。 value = (uint?)10; Assert.AreEqual((object)value, func(value, typeof(object))); value = (int?)11; Assert.AreEqual((ValueType)value, func(value, typeof(ValueType))); value = (int?)null; Assert.AreEqual((ValueType)value, func(value, typeof(ValueType))); value = (int?)12; Assert.AreEqual((IComparable <int>)value, func(value, typeof(IComparable <int>))); value = (Tristate?)Tristate.True; Assert.AreEqual((Enum)value, func(value, typeof(Enum))); // 如果值类型具有到接口或委托类型 I0 的装箱转换,且 I0 变化转换为接口类型 I,则值类型具有到 I 的装箱转换。 value = new TestStruct4(); Assert.AreEqual((IEnumerable <object>)value, func(value, typeof(IEnumerable <object>))); value = (TestStruct4?)new TestStruct4(); Assert.AreEqual((IEnumerable <object>)value, func(value, typeof(IEnumerable <object>))); value = (TestStruct4?)null; Assert.AreEqual((IEnumerable <object>)value, func(value, typeof(IEnumerable <object>))); #endregion // 6.1.7 装箱转换 // 6.1.8 隐式动态转换、6.1.9 隐式常量表达式转换、6.1.10 涉及类型形参的隐式转换,不在考虑范围内。 #region 6.1.11 用户定义的隐式转换 value = new TestStruct2(); object expectedValue = (TestStruct) new TestStruct2(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct))); value = new TestStruct2(); expectedValue = (TestStruct?)new TestStruct2(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct?))); value = (TestStruct2?)new TestStruct2(); expectedValue = (TestStruct?)(TestStruct2?)new TestStruct2(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct?))); value = (TestStruct2?)null; Assert.AreEqual((TestStruct?)value, func(value, typeof(TestStruct?))); value = new TestStruct3(); expectedValue = (TestStruct) new TestStruct3(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct))); value = new TestStruct3(); expectedValue = (TestStruct?)new TestStruct3(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct?))); value = (TestStruct3?)new TestStruct3(); expectedValue = (TestStruct?)(TestStruct3?)new TestStruct3(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct?))); value = (TestStruct3?)null; Assert.AreEqual(value, func(value, typeof(TestStruct?))); value = new TestStruct5(); expectedValue = (TestStruct?)new TestStruct5(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct?))); value = new TestStruct6(); expectedValue = (TestStruct) new TestStruct6(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct))); value = (TestStruct6?)new TestStruct6(); expectedValue = (TestStruct)(TestStruct6?)new TestStruct6(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct))); value = new TestStruct6(); expectedValue = (TestStruct?)new TestStruct6(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct?))); value = (TestStruct6?)new TestStruct6(); expectedValue = (TestStruct?)(TestStruct6?)new TestStruct6(); Assert.AreEqual(expectedValue, func(value, typeof(TestStruct?))); value = (TestStruct6?)null; Assert.AreEqual(value, func(value, typeof(TestStruct?))); value = new TestClass(); expectedValue = (int)new TestClass(); Assert.AreEqual(expectedValue, func(value, typeof(int))); value = new TestClass2(); expectedValue = (int)new TestClass2(); Assert.AreEqual(expectedValue, func(value, typeof(int))); value = new TestClass2(); expectedValue = (long)new TestClass2(); Assert.AreEqual(expectedValue, func(value, typeof(long))); value = new TestClass2(); expectedValue = (bool)new TestClass2(); Assert.AreEqual(expectedValue, func(value, typeof(bool))); value = new TestClass13(); expectedValue = (Enum) new TestClass13(); Assert.AreEqual(expectedValue, func(value, typeof(Enum))); value = new TestClass12(); expectedValue = (int?)new TestClass12(); Assert.AreEqual(expectedValue, func(value, typeof(int?))); value = new TestClass8(); expectedValue = (TestClass6) new TestClass8(); Assert.AreEqual(expectedValue, func(value, typeof(TestClass6))); value = new TestClass6(); expectedValue = (TestClass7) new TestClass6(); Assert.AreEqual(expectedValue, func(value, typeof(TestClass7))); value = 10; expectedValue = (TestClass6)10; Assert.AreEqual(expectedValue, func(value, typeof(TestClass6))); value = new TestClass(); expectedValue = (long)new TestClass(); Assert.AreEqual(expectedValue, func(value, typeof(long))); value = new TestClass(); expectedValue = (decimal) new TestClass(); Assert.AreEqual(expectedValue, func(value, typeof(decimal))); value = new TestClass2(); expectedValue = (decimal) new TestClass2(); Assert.AreEqual(expectedValue, func(value, typeof(decimal))); value = (short)10; expectedValue = (TestClass6)(short)10; Assert.AreEqual(expectedValue, func(value, typeof(TestClass6))); value = new TestClass6(); expectedValue = (long)new TestClass6(); Assert.AreEqual(expectedValue, func(value, typeof(long))); #endregion // 6.1.11 用户定义的隐式转换 }