using System;
using System.Reflection;
using TargetMethodType = System.Reflection.MethodBase;
namespace com.bbbirder.injection
{
public struct InjectionInfo
{
///
/// indicate the method to be injected
///
public TargetMethodType InjectedMethod { get; private set; }
//TODO : FixingDelegate?
// use one of the following to fix target method
public MethodInfo FixingMethod { get; private set; }
public Delegate FixingDelegate { get; private set; }
public Type DryInjectAssmemble { get; private set; }
public string DryInjectMethodName { get; private set; }
public MethodInfo DryInjectMethod { get; private set; }
public Action OriginReceiver;
internal Action onStartFix;
public static InjectionInfo Create(TargetMethodType methodToReplace, MethodInfo methodToProvide, Action originMethodReceiver = null)
{
DebugHelper.IsNotNull(methodToReplace,"methodToReplace is null");
DebugHelper.IsNotNull(methodToProvide,"methodToProvide is null");
return new InjectionInfo()
{
InjectedMethod = methodToReplace,
FixingMethod = methodToProvide,
OriginReceiver = originMethodReceiver,
};
}
public static InjectionInfo Create(TargetMethodType methodToReplace, Delegate methodToProvide, Action originMethodReceiver = null)
{
DebugHelper.IsNotNull(methodToReplace,"methodToReplace is null");
DebugHelper.IsNotNull(methodToProvide,"methodToProvide is null");
return new InjectionInfo()
{
InjectedMethod = methodToReplace,
FixingDelegate = methodToProvide,
OriginReceiver = originMethodReceiver,
};
}
public static InjectionInfo Create(T methodToReplace, T methodToProvide, Action originMethodReceiver = null) where T : Delegate
{
DebugHelper.IsNotNull(methodToReplace, "methodToReplace is null");
DebugHelper.IsNotNull(methodToProvide, "methodToProvide is null");
return new InjectionInfo()
{
InjectedMethod = methodToReplace.Method,
FixingDelegate = methodToProvide,
OriginReceiver = f => originMethodReceiver?.Invoke((T)f),
};
}
public static InjectionInfo Create(Type assembleName, string filedName, MethodInfo methodToProvide)
{
DebugHelper.IsNotNull(methodToProvide, "methodToProvide is null");
return new InjectionInfo()
{
DryInjectAssmemble = assembleName,
DryInjectMethodName = filedName,
DryInjectMethod = methodToProvide
};
}
public static InjectionInfo Create(Action onStartFix){
return new InjectionInfo(){
onStartFix = onStartFix,
};
}
}
}