private void ConfigureConditionality() { var conditionName = ControllableAttribute.ConditionalPropertyName; if (string.IsNullOrEmpty(conditionName)) { return; } // Check for raw string value of True or False if (String.Compare(conditionName, "true", true, CultureInfo.InvariantCulture) == 0 || String.Compare(conditionName, "false", true, CultureInfo.InvariantCulture) == 0) { ReflectCondition(bool.Parse(conditionName)); return; } // Check for property by that name var flags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic; var conditionProperty = PropertyResolutionObject.GetType().GetProperty(conditionName, flags); if (conditionProperty == null) { throw new MissingMemberException("Could not find condition property named " + conditionName); } if (conditionProperty.PropertyType == typeof(ControllableCondition)) { ControllableCondition = (ControllableCondition)conditionProperty.GetValue(PropertyResolutionObject, null); ControllableCondition.Changed += HandleConditionChanged; ReflectCondition(ControllableCondition.ConditionValid); } else if (conditionProperty.PropertyType == typeof(bool)) { var staticConditionValue = (bool)conditionProperty.GetValue(PropertyResolutionObject, null); ReflectCondition(staticConditionValue); } else { throw new MemberAccessException("Property found with name, but not a type or value which can specify conditionality."); } }
} // A formality of reflective event binding... private void ConfigureObservation() { if (string.IsNullOrEmpty(ControllableAttribute.ObservableEventName)) { return; } ObservingEvent = PropertyResolutionObject.GetType().GetEvent(ControllableAttribute.ObservableEventName); if (ObservingEvent == null) { throw new MissingMemberException("Could not find change-observing event named " + ControllableAttribute.ObservableEventName); } ChangeHandlerDelegate = Delegate.CreateDelegate(ObservingEvent.EventHandlerType, this, ((Action)HandleObservedPropertyChanged).Method); ObservingEvent.AddEventHandler(PropertyResolutionObject, ChangeHandlerDelegate); }