static void AboutDynamic() { dynamic d = new ExClass(); try { //CLR在这里就不会去捕捉错误并重新抛出, 于是原错误正常向上传递 d.ErrorMethod(); } catch (Exception e) { Console.WriteLine("\r\nDynamic call: catch: " + e.Message); //throw; } }
static void LogInnerException() { object o = new ExClass(); try { System.Reflection.MethodInfo m = o.GetType().GetMethod("ErrorMethod"); //CLR在内部捕捉这个调用的所有错误,并转为TargetInvocation重抛 //相当于真正的原错误没能顺利往上层传递 m.Invoke(o, null); } catch (System.Reflection.TargetInvocationException e) { Console.WriteLine("CLR reflection:\r\n\r\ncatch: " + e.Message + "\r\n\r\nactual: " + e.InnerException.Message); //throw e.InnerException; } }
static void Main(string[] args) { string str = string.Empty; string str2 = "hello"; Console.Write("문자열을 입력하세요 : "); str = Console.ReadLine(); Console.WriteLine(str.ToReverse(str.Length)); //1. string형식의 내장메소드를 확장해서 ToReverse()함수를 정의했으며 매개변수는 뒤집을 문자열의 길이 Console.WriteLine(str2.ToReverse(str2.Length)); string num = string.Empty; Console.Write("숫자를 입력하세요 : "); num = Console.ReadLine(); Console.WriteLine(Convert.ToInt32(num).ToFactorial()); //2. int형식의 메소드 확장(ToFactorial()을 정의) => 입력 숫자.메소드명() : 팩토리얼 결과 출력 ExClass ex = new ExClass(); Console.WriteLine(); Console.WriteLine(ex.ShowName("성명훈")); }
static void Main(string[] args) { var oldEx = new ExClass[10, 10]; var newEx = oldEx.init <ExClass>(); }
public static string ShowName(this ExClass myClass, string name) { myClass.name = name; return(myClass.name); //ExClass의 이름을 반환 }