using System;
using System.Text;
public static string FromASCIIByteArray(byte[] characters)
{
ASCIIEncoding encoding = new ASCIIEncoding( );
string constructedString = encoding.GetString(characters);
return (constructedString);
}
UTF-16으로 인코딩된 유니코드로 구성된 byte 배열을 문자열로 변환하기 위해 다음 메서드를 사용한다.
public static string FromUnicodeByteArray(byte[] characters)
{
UnicodeEncoding encoding = new UnicodeEncoding( );
string constructedString = encoding.GetString(characters);
return (constructedString);
}
using System;
using System.Reflection;
public class Reflect
{
public void ReflectionException( )
{
Type reflectedClass = typeof(Reflect);
try
{
MethodInfo methodToInvoke = reflectedClass.GetMethod("TestInvoke");
if (methodToInvoke != null)
{
object oInvoke = methodToInvoke.Invoke(null, null);
}
}
catch(Exception e)
{
Console.WriteLine("MESSAGE: " + e.Message);
Console.WriteLine("SOURCE: " + e.Source);
Console.WriteLine("TARGET: " + e.TargetSite);
Console.WriteLine("STACK: " + e.StackTrace + "\r\n");
if(e.InnerException != null)
{
Console.WriteLine( );
Console.WriteLine("\t**** INNEREXCEPTION START ****");
Console.WriteLine("\tTYPE THAT THREW EXCEPTION: " +
reflectedClass.ToString( ));
Console.WriteLine("\tINNEREXCEPTION MESSAGE: " +
e.InnerException.Message);
Console.WriteLine("\tINNEREXCEPTION SOURCE: " +
e.InnerException.Source);
Console.WriteLine("\tINNEREXCEPTION STACK: " +
e.InnerException.StackTrace);
Console.WriteLine("\tINNEREXCEPTION TARGETSITE: " +
e.InnerException.TargetSite);
Console.WriteLine("\t**** INNEREXCEPTION END ****");
}
Console.WriteLine( );
// Shows fusion log when assembly cannot be located
Console.WriteLine(e.ToString( ));
}
}
// Method to invoke via reflection
public static void TestInvoke( )
{
throw (new Exception("Thrown from invoked method."));
}
}
MESSAGE: Exception has been thrown by the target of an invocation.
SOURCE: mscorlib
TARGET: System.Object InternalInvoke(System.Object, System.Reflection.BindingFlags,
System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo,
Boolean, System.Reflection.Assembly, Boolean)
STACK: at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean isBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj,
BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr,
Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at Reflect.ReflectionException( ) in
c:\book cs cookbook\code\test.cs:line 22
**** INNEREXCEPTION START ****
TYPE THAT THREW EXCEPTION: ClassLibrary1.Reflect
INNEREXCEPTION MESSAGE: Thrown from invoked method.
INNEREXCEPTION SOURCE: ClassLibrary1
INNEREXCEPTION STACK: at ClassLibrary1.Reflect.TestInvoke( ) in
C:\BOOK CS CookBook\code\Test.cs:line 49
at ClassLibrary1.Reflect.TestInvoke( ) in
C:\BOOK CS CookBook\code\Test.cs:line 49
INNEREXCEPTION TARGETSITE: Void TestInvoke( )
**** INNEREXCEPTION END ****
System.Reflection.TargetInvocationException: Exception has been thrown by the target
of an invocation. ---> System.Exception: Thrown from invoked method.
at ClassLibrary1.Reflect.TestInvoke( ) in
C:\BOOK CS CookBook\code\Test.cs:line 49
at ClassLibrary1.Reflect.TestInvoke( ) in
C:\BOOK CS CookBook\code\Test.cs:line 49
--- End of inner exception stack trace ---
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean
isBinderDefault, Assembly caller, Boolean verifyAccess)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean
verifyAccess)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr,
Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
atReflect.ReflectionException( ) in c:\book cs cookbook
\code\test.cs:line 22
이전 글 : 임베디드 시스템을 위한 GNU dbm의 소개
다음 글 : 소설처럼 읽히는 프로그램 작성하기
최신 콘텐츠