|
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using ThirdPartyHR;
using DynamicDecoratorAOP.Mef;
using System.ComponentModel.Composition.Primitives;
using System.ComponentModel;
using NCT;
using DynamicDecoratorAOP;
using System.Threading;
using System.Security.Principal;
using DynamicDecoratorAOP.Concerns;
namespace ConsoleUtil
{
class LocalConcerns
{
static LocalConcerns()
{
ConcernsContainer.runtimeAspects.Add("ConsoleUtil.LocalConcerns.NotifyChange", new Decoration(LocalConcerns.NotifyChange, null));
ConcernsContainer.runtimeAspects.Add("ConsoleUtil.LocalConcerns.SecurityCheck", new Decoration(LocalConcerns.SecurityCheck, null));
}
public static void NotifyChange(AspectContext ctx, object[] parameters)
{
((Employee)ctx.Target).NotifyPropertyChanged(ctx.CallCtx.MethodName);
}
public static void SecurityCheck(AspectContext ctx, object[] parameters)
{
Exception exInner = null;
try
{
if (parameters != null && parameters[0] is WindowsPrincipal && ((WindowsPrincipal)parameters[0]).IsInRole("BUILTIN\\" + "Administrators"))
{
return;
}
}
catch ( Exception ex)
{
exInner = ex;
}
throw new Exception("No right to call!", exInner);
}
}
class Program
{
static void Main(string[] args)
{
AggregateCatalog catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.LoadFrom("Employee.dll")));
Employee target = null;
IEmployee emp = null;
var container = new AOPCompositeContainer(catalog);
emp = container.GetExportProxy<Employee, IEmployee>();
emp.EmployeeID = 1;
emp.FirstName = "John";
emp.LastName = "Smith";
emp.DateOfBirth = new DateTime(1990, 4, 1);
emp.DepartmentID = 1;
emp.DetailsByLevel(2);
IEmployee emp1 = container.GetExportProxy<Employee, IEmployee>("set_EmployeeID");
try
{
//Commenting out this line will throw out an exception
//Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
Decoration dec = ConcernsContainer.runtimeAspects["ConsoleUtil.LocalConcerns.SecurityCheck"];
dec.Parameters = new object[] { Thread.CurrentPrincipal };
target = container.GetTarget<Employee>();
target.PropertyChanged += new PropertyChangedEventHandler(PropertyChanged_Listener);
emp1.EmployeeID = 2;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
container.Dispose();
Console.ReadLine();
}
static void PropertyChanged_Listener(object sender, PropertyChangedEventArgs e)
{
Console.WriteLine(e.PropertyName.ToString() + " has changed.");
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.