sad lige oppe og brugte lidt tid på denne kode, jeg har dog lavet så den kan cache adresserne, dog ved jeg ikke om det er nødvendigt, da jeg ikke helt ved hvor meget DotNet selv cacher i reflection.
using System;
using System.Reflection;
namespace LibHookTest
{
public interface ILog
{
/* Test if a level is enabled for logging */
bool IsDebugEnabled { get; }
bool IsInfoEnabled { get; }
bool IsWarnEnabled { get; }
bool IsErrorEnabled { get; }
bool IsFatalEnabled { get; }
/* Log a message object */
void Debug(object message);
void Info(object message);
void Warn(object message);
void Error(object message);
void Fatal(object message);
/* Log a message object and exception */
void Debug(object message, Exception t);
void Info(object message, Exception t);
void Warn(object message, Exception t);
void Error(object message, Exception t);
void Fatal(object message, Exception t);
/* Log a message string using the System.String.Format syntax */
void DebugFormat(string format, params object[] args);
void InfoFormat(string format, params object[] args);
void WarnFormat(string format, params object[] args);
void ErrorFormat(string format, params object[] args);
void FatalFormat(string format, params object[] args);
/* Log a message string using the System.String.Format syntax */
void DebugFormat(IFormatProvider provider, string format, params object[] args);
void InfoFormat(IFormatProvider provider, string format, params object[] args);
void WarnFormat(IFormatProvider provider, string format, params object[] args);
void ErrorFormat(IFormatProvider provider, string format, params object[] args);
void FatalFormat(IFormatProvider provider, string format, params object[] args);
}
public class Log : ILog
{
private object logger;
private Type _thisType;
private Type ThisType
{
get
{
if (_thisType == null) _thisType = logger.GetType();
return _thisType;
}
}
private PropertyInfo _isDebugEnabledProperty;
private PropertyInfo _isErrorEnabledProperty;
private PropertyInfo _isFatalEnabledProperty;
private PropertyInfo _isInfoEnabledProperty;
private PropertyInfo _isWarnEnabledProperty;
private MethodInfo _DebugMethod1;
private MethodInfo _DebugMethod2;
private MethodInfo _DebugFormatMethod1;
private MethodInfo _DebugFormatMethod2;
private MethodInfo _ErrorMethod1;
private MethodInfo _ErrorMethod2;
private MethodInfo _ErrorFormatMethod1;
private MethodInfo _ErrorFormatMethod2;
private MethodInfo _FatalMethod1;
private MethodInfo _FatalMethod2;
private MethodInfo _FatalFormatMethod1;
private MethodInfo _FatalFormatMethod2;
private MethodInfo _InfoMethod1;
private MethodInfo _InfoMethod2;
private MethodInfo _InfoFormatMethod1;
private MethodInfo _InfoFormatMethod2;
private MethodInfo _WarnMethod1;
private MethodInfo _WarnMethod2;
private MethodInfo _WarnFormatMethod1;
private MethodInfo _WarnFormatMethod2;
public bool IsDebugEnabled
{
get
{
return ReflectSimplifierProperty<bool>(ref logger, ref _isDebugEnabledProperty, "IsDebugEnabled");
}
}
public bool IsErrorEnabled
{
get
{
return ReflectSimplifierProperty<bool>(ref logger, ref _isErrorEnabledProperty, "IsErrorEnabled");
}
}
public bool IsFatalEnabled
{
get
{
return ReflectSimplifierProperty<bool>(ref logger, ref _isFatalEnabledProperty, "IsFatalEnabled");
}
}
public bool IsInfoEnabled
{
get
{
return ReflectSimplifierProperty<bool>(ref logger, ref _isInfoEnabledProperty, "IsInfoEnabled");
}
}
public bool IsWarnEnabled
{
get
{
return ReflectSimplifierProperty<bool>(ref logger, ref _isWarnEnabledProperty, "IsWarnEnabled");
}
}
public void Debug(object message)
{
ReflectSimplifiedMethod(ref logger, ref _DebugMethod1, "Debug",new [] { typeof(object) }, message);
}
public void Debug(object message, Exception t)
{
ReflectSimplifiedMethod(ref logger, ref _DebugMethod2, "Debug", new[] { typeof(object), typeof(Exception) }, message,t);
}
public void DebugFormat(string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _DebugFormatMethod1, "DebugFormat", new[] { typeof(string), typeof(object[]) }, format, args);
}
public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _DebugFormatMethod2, "DebugFormat", new[] { typeof(IFormatProvider),typeof(string), typeof(object[]) }, provider,format, args);
}
public void Error(object message)
{
ReflectSimplifiedMethod(ref logger, ref _ErrorMethod1, "Error", new[] { typeof(object) }, message);
}
public void Error(object message, Exception t)
{
ReflectSimplifiedMethod(ref logger, ref _ErrorMethod2, "Error", new[] { typeof(object), typeof(Exception) }, message, t);
}
public void ErrorFormat(string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _ErrorFormatMethod1, "ErrorFormat", new[] { typeof(string), typeof(object[]) },format,args);
}
public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _ErrorFormatMethod2, "ErrorFormat", new[] { typeof(IFormatProvider),typeof(string), typeof(object[]) }, provider, format, args);
}
public void Fatal(object message)
{
ReflectSimplifiedMethod(ref logger, ref _FatalMethod1, "Fatal", new[] { typeof(object) }, message);
}
public void Fatal(object message, Exception t)
{
ReflectSimplifiedMethod(ref logger, ref _FatalMethod2, "Fatal", new[] { typeof(object),typeof(Exception) }, message,t);
}
public void FatalFormat(string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _FatalFormatMethod1, "FatalFormat", new[] { typeof(string), typeof(object[]) }, format, args);
}
public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _FatalFormatMethod2, "FatalFormat", new[] { typeof(IFormatProvider), typeof(string), typeof(object[]) }, provider, format, args);
}
public void Info(object message)
{
ReflectSimplifiedMethod(ref logger, ref _InfoMethod1, "Info", new[] { typeof(object) }, message);
}
public void Info(object message, Exception t)
{
ReflectSimplifiedMethod(ref logger, ref _InfoMethod2, "Info", new[] { typeof(object), typeof(Exception) }, message, t);
}
public void InfoFormat(string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _InfoFormatMethod1, "InfoFormat", new[] { typeof(string), typeof(object[]) }, format, args);
}
public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _InfoFormatMethod2, "InfoFormat", new[] { typeof(IFormatProvider), typeof(string), typeof(object[]) }, provider, format, args);
}
public void Warn(object message)
{
ReflectSimplifiedMethod(ref logger, ref _WarnMethod1, "Warn", new[] { typeof(object) }, message);
}
public void Warn(object message, Exception t)
{
ReflectSimplifiedMethod(ref logger, ref _WarnMethod2, "Warn", new[] { typeof(object), typeof(Exception) }, message, t);
}
public void WarnFormat(string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _WarnFormatMethod1, "WarnFormat", new[] { typeof(string), typeof(object[]) }, format, args);
}
public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
ReflectSimplifiedMethod(ref logger, ref _WarnFormatMethod2, "WarnFormat", new[] { typeof(IFormatProvider), typeof(string), typeof(object[]) }, provider, format, args);
}
public Log(object logger)
{
this.logger = logger;
if (ThisType.Namespace != "log4net.Core")
throw new InvalidCastException("object is not from the correct framework");
if (ThisType.Name != "LogImpl")
throw new InvalidCastException("object is not a LogImpl Type");
}
static private T ReflectSimplifierProperty<T>(ref object obj, ref PropertyInfo cacheobj, string name)
{
if (cacheobj == null) cacheobj = obj.GetType().GetProperty(name, typeof(T));
return (T)cacheobj.GetValue(obj);
}
static private void ReflectSimplifiedMethod(ref object obj, ref MethodInfo cacheobj, string name,Type[] types, params object[] args)
{
if (cacheobj == null) cacheobj = obj.GetType().GetMethod(name, types);
cacheobj.Invoke(obj, args);
}
}
}
for og loade et gyldigt objekt, brugte jeg denne kode
using System;
using System.Reflection;
namespace LibHookTest
{
class Program
{
static void Main(string[] args)
{
Assembly assembly = Assembly.Load("log4net");
Type BasicConfigurator = assembly.GetType("log4net.Config.BasicConfigurator");
MethodInfo ConfigureMethod = BasicConfigurator.GetMethod("Configure", Type.EmptyTypes);
ConfigureMethod.Invoke(null,new object[] { });
Type LogManager = assembly.GetType("log4net.LogManager");
MethodInfo GetLogger = LogManager.GetMethod("GetLogger", new[] { typeof(string) });
object log = GetLogger.Invoke(null, new [] { "test" });
ILog logger = new Log(log);
logger.Debug("Dette er en debug");
logger.Debug("Dette er en debug med en exeption", new Exception("Fejl!"));
logger.DebugFormat("Dette er en format debug: {0} {1}", logger.ToString(), logger.IsDebugEnabled);
logger.Info("Dette er en Info");
logger.Info("Dette er en Info med en exeption", new Exception("Fejl!"));
logger.InfoFormat("Dette er en format Info: {0} {1}", logger.ToString(), logger.IsInfoEnabled);
Console.ReadKey();
}
}
}