????2?????????
???????????????????????????AOP?????????????????????????????????AOP???????á???????????????????????AOP????????????????????????????IL ???????????????????????????????????Demo??????ɡ????????????????????????????????
?????????????????????????????(MS Enterprise Library)?????PIAB(Policy Injection Application Block)????????????
??????????????????????dll????????????????á?

?????????????Handler
public class User
{
public string Name { set; get; }
public string PassWord { set; get; }
}
#region 1????????????????
public class LogHandlerAttribute : HandlerAttribute
{
public string LogInfo { set; get; }
public int Order { get; set; }
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new LogHandler() { Order = this.Order?? LogInfo = this.LogInfo };
}
}
#endregion
#region 2???????????Handler????????
public class LogHandler : ICallHandler
{
public int Order { get; set; }
public string LogInfo { set; get; }
//???????????????????????漲????з???????????????
public IMethodReturn Invoke(IMethodInvocation input?? GetNextHandlerDelegate getNext)
{
Console.WriteLine("LogInfo????" + LogInfo);
//0.????????
var arrInputs = input.Inputs;
if (arrInputs.Count > 0)
{
var oUserTest1 = arrInputs[0] as User;
}
//1.??з???????????
Console.WriteLine("???????????????");
//2.??з???
var messagereturn = getNext()(input?? getNext);
//3.??з???????????
Console.WriteLine("??????к????????");
return messagereturn;
}
}
#endregion
#region 3???????????????
public interface IUserOperation
{
void Test(User oUser);
void Test2(User oUser?? User oUser2);
}
//????????????????MarshalByRefObject????????
public class UserOperation : MarshalByRefObject?? IUserOperation
{
private static UserOperation oUserOpertion = null;
public UserOperation()
{
//oUserOpertion = PolicyInjection.Create();
}
//???嵥??????PolicyInjection.Create()??????????????????????????????????д??Щ????
public static UserOperation GetInstance()
{
if (oUserOpertion == null)
oUserOpertion = PolicyInjection.Create();
return oUserOpertion;
}
//???????????????
public string Name { set; get; }
//[LogHandler]???????????????????????????????
[LogHandler(LogInfo = "Test??????aaaaa")]
public void Test(User oUser)
{
Console.WriteLine("Test?????????");
}
[LogHandler(LogInfo = "Test2??????bbbbb")]
public void Test2(User oUser?? User oUser2)
{
Console.WriteLine("Test2?????????");
}
}
#endregion
??????????????????????
static void Main(string[] args)
{
try
{
var oUserTest1 = new User() { Name = "test2222"?? PassWord = "yxj" };
var oUserTest2 = new User() { Name = "test3333"?? PassWord = "yxj" };
var oUser = UserOperation.GetInstance();
oUser.Test(oUserTest1);
oUser.Test2(oUserTest1??oUserTest2);
}
catch (Exception ex)
{
//throw;
}
}
?????????????£?

???????????????Test()??????Test2()???????????

????????Test()??Test2()???????????LogHander???????????????漲????AOP??Handler???????Test??Test2????????????????Invoke()???????檔???????AOP????????????????????ù????????????????????????????????ù???????ü??ɡ?