Connected properties only allow attaching data to carrier objects, but you may attach a delegate as data. It can then be used as a connected method.

This is similar to setting delegates on an ExpandoObject.

private const string LogMethod = Guid.NewGuid().ToString("N");

public void Log(object obj)
{
  var logMethodProperty = PropertyConnector.Default.Get(obj, LogMethod);
  Action logMethod;
  if (logMethodProperty.TryGet(out logMethod))
  {
    // This instance has a connected Log method.
    logMethod();
  }
  else
  {
    // This instance does not have a connected Log method.
    GenericLog();
  }
}

Note that connected methods are different than extension methods. Extension methods extend a type, and can be called for any instance of that type. Connected methods extend an instance, so they can be overridden on a per-instance basis.

Last edited Aug 30, 2013 at 3:49 AM by StephenCleary, version 5

Comments

No comments yet.