Skip to content

Instantly share code, notes, and snippets.

@cairey
cairey / Literal.cs
Created October 19, 2012 09:05
Literal of an expression at runtime. Handy for property change notifications i.e Literal.Of(() => MyProperty)
public static class Literal
{
private static string GetMemberName(Expression expression)
{
switch (expression.NodeType)
{
case ExpressionType.MemberAccess:
var memberExpression = (MemberExpression)expression;
var supername = GetMemberName(memberExpression.Expression);
@cairey
cairey / LogInterceptor.cs
Created July 20, 2012 15:22
Aspect Oriented Programming with MEF using the interceptors. Compose an interceptor catalog in the example below
public class LogInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
if(invocation.TargetType.GetCustomAttributes(typeof(LogMethodCallsAttribute), true).Length > 0)
{
var loggerFacade = MefInstanceProvider.Container.GetExportedValue<ILoggerFacade>();
loggerFacade.Log(FormatMethodCall(invocation, ApplicationStates.Started), Category.Info, Priority.Low);
invocation.Proceed();
@cairey
cairey / CommandHandlerFactory.cs
Created July 20, 2012 15:13
Command pattern using MEF
[Export(typeof(ICommandHandlerFactory))]
public class CommandHandlerFactory : ICommandHandlerFactory
{
private readonly IEnumerable<ICommandHandler> _commandHandlers;
[ImportingConstructor]
public CommandHandlerFactory([ImportMany]IEnumerable<ICommandHandler> commandHandlers)
{
_commandHandlers = commandHandlers;
}
@cairey
cairey / DragOverControlToCommandBehavior.cs
Created July 20, 2012 15:07
Drag over control to command bahvior
public class DragOverControlToCommandBehavior : CommandBehaviorBase<Control>
{
public DragOverControlToCommandBehavior(Control element)
: base(element)
{
element.AllowDrop = true;
element.DragOver += ElementDragOver;
}
private void ElementDragOver(object sender, DragEventArgs e)
@cairey
cairey / MouseEnterIntent.cs
Created August 26, 2011 10:56
Just like Hover Intent in JQuery, this is Hover Intent Behavior for Silverlight. See "Read Me" below.
public class MouseEnterIntent
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(MouseEnterIntent), new PropertyMetadata(OnSetCommandCallback));
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(MouseEnterIntent), new PropertyMetadata(OnSetCommandParameterCallback));
public static readonly DependencyProperty MouseEnterIntentCommandBehaviorProperty =
DependencyProperty.RegisterAttached("HoverIntentCommandBehavior", typeof(MouseEnterIntentCommandBehavior), typeof(MouseEnterIntent), null);