using System;
using System.Reflection;
using System.Windows.Forms;
using System.Collections.Generic;
using System.IO;
using System.ComponentModel;
using System.Collections;
using System.Text;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;
using BrainTechLLC.ThreadSafeObjects;
using LinFu.AOP;
using LinFu.AOP.Interfaces;
namespace BrainTechLLC
{
[Serializable, AttributeUsage(AttributeTargets.Method)]
public class InvokeIfThenElseAttribute : IfCaseAttribute, IReadableAttribute
{
public InvokeIfThenElseAttribute(string ifMethod, string thenMethod, string elseMethod,
string ifMethodAfter, string thenMethodAfter, string elseMethodAfter)
: base(ifMethod, thenMethod, elseMethod, ifMethodAfter, thenMethodAfter, elseMethodAfter) { }
}
[Serializable, AttributeUsage(AttributeTargets.Method)]
public class IfCaseAttribute : Attribute, IReadableAttribute
{
public string IfMethodName { get; set; }
public string ThenMethodName { get; set; }
public string ElseMethodName { get; set; }
public string IfMethodNameAfter { get; set; }
public string ThenMethodNameAfter { get; set; }
public string ElseMethodNameAfter { get; set; }
public object Value { get { return IfMethodName; } }
public IfCaseAttribute(string ifMethod, string thenMethod, string elseMethod,
string ifMethodAfter, string thenMethodAfter, string elseMethodAfter)
{
IfMethodName = ifMethod;
ThenMethodName = thenMethod;
ElseMethodName = elseMethod;
IfMethodNameAfter = ifMethodAfter;
ThenMethodNameAfter = thenMethodAfter;
ElseMethodNameAfter = elseMethodAfter;
}
protected void GetIfThenElseMethodsBefore(Type t, out MethodInfo ifDel, out MethodInfo thenDel, out MethodInfo elseDel)
{
ifDel = Lookups.IfLookup[t.Name + "." + IfMethodName];
thenDel = null;
elseDel = null;
if (ifDel == null)
{
ifDel = t.GetMethod(IfMethodName);
Lookups.IfLookup.AddOrSet(t.Name + "." + IfMethodName, ifDel);
}
if (!string.IsNullOrEmpty(ThenMethodName))
{
thenDel = Lookups.ThenLookup[t.Name + "." + ThenMethodName];
if (thenDel == null)
{
thenDel = t.GetMethod(ThenMethodName);
Lookups.ThenLookup.AddOrSet(t.Name + "." + ThenMethodName, thenDel);
}
}
if (!string.IsNullOrEmpty(ElseMethodName))
{
elseDel = Lookups.ElseLookup[t.Name + "." + ElseMethodName];
if (elseDel == null)
{
elseDel = t.GetMethod(ElseMethodName);
Lookups.ElseLookup.AddOrSet(t.Name + "." + ElseMethodName, elseDel);
}
}
}
protected void GetIfThenElseMethodsAfter(Type t, out MethodInfo ifDel, out MethodInfo thenDel, out MethodInfo elseDel)
{
ifDel = Lookups.IfLookup[t.Name + "." + IfMethodNameAfter];
thenDel = null;
elseDel = null;
if (!string.IsNullOrEmpty(ThenMethodNameAfter))
{
thenDel = Lookups.ThenLookup[t.Name + "." + ThenMethodNameAfter];
if (thenDel == null)
{
thenDel = t.GetMethod(ThenMethodNameAfter);
if (thenDel != null)
Lookups.ThenLookup.AddOrSet(t.Name + "." + ThenMethodNameAfter, thenDel);
}
}
if (!string.IsNullOrEmpty(ElseMethodNameAfter))
{
elseDel = Lookups.ElseLookup[t.Name + "." + ElseMethodNameAfter];
if (elseDel == null)
{
elseDel = t.GetMethod(ElseMethodNameAfter);
if (elseDel != null)
Lookups.ElseLookup.AddOrSet(t.Name + "." + ElseMethodNameAfter, elseDel);
}
}
if (ifDel == null)
{
ifDel = t.GetMethod(IfMethodNameAfter);
if (ifDel != null)
Lookups.IfLookup.AddOrSet(t.Name + "." + IfMethodNameAfter, ifDel);
}
}
public void DoWorkBefore(IInvocationContext context, object thisObject)
{
Type t = thisObject.GetType();
MethodInfo ifDel;
MethodInfo thenDel;
MethodInfo elseDel;
GetIfThenElseMethodsBefore(t, out ifDel, out thenDel, out elseDel);
if (ifDel == null)
{
if (thenDel != null)
thenDel.Invoke(thisObject, new object[] { context });
}
else
{
object result = ifDel.Invoke(thisObject, new object[] { context });
if (Convert.ToBoolean(result) == true)
{
if (thenDel != null)
thenDel.Invoke(thisObject, new object[] { context });
}
else
{
if (elseDel != null)
elseDel.Invoke(thisObject, new object[] { context });
}
}
}
public void DoWorkAfter(IInvocationContext context, object thisObject)
{
Type t = thisObject.GetType();
MethodInfo ifDel;
MethodInfo thenDel;
MethodInfo elseDel;
GetIfThenElseMethodsAfter(t, out ifDel, out thenDel, out elseDel);
if (ifDel == null)
{
if (thenDel != null)
thenDel.Invoke(thisObject, new object[] { context });
}
else
{
object result = ifDel.Invoke(thisObject, new object[] { context });
if (Convert.ToBoolean(result) == true)
{
if (thenDel != null)
thenDel.Invoke(thisObject, new object[] { context });
}
else
{
if (elseDel != null)
elseDel.Invoke(thisObject, new object[] { context });
}
}
}
}
}