//////////////////////////////////////////////// //// PropertyDelegate //// //// Version 1.0.0 12.07.04 //// //// www.danielgrunwald.de //// //////////////////////////////////////////////// // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // using System; namespace Grunwald.PropertyDelegates { #region Base Class /// /// Abstract class for delegates pointing to properties. /// public abstract class PropertyDelegate { /// /// Creates a new PropertyDelegate pointing to the target property. /// protected PropertyDelegate(object target, string propertyName) { if (target == null) throw new ArgumentNullException("target"); if (propertyName == null) throw new ArgumentNullException("propertyName"); this.target = target; this.propertyName = propertyName; try { MakeGet(); writeOnly = false; } catch (ArgumentException) { writeOnly = true; } try { MakeSet(); readOnly = false; } catch { readOnly = true; } if (readOnly && writeOnly) throw new ArgumentException("Error binding to target property. Check" + "if the property exists and if the return "+ "type is correct."); } bool readOnly, writeOnly; /// /// Is used internally to create the get-delegate. /// protected abstract void MakeGet(); /// /// Is used internally to create the set-delegate. /// protected abstract void MakeSet(); object target; string propertyName; /// /// Gets the target object. /// public object Target { get { return target; } } /// /// Gets the name of the property. /// public string PropertyName { get { return propertyName; } } /// /// Gets if the target property is read-only. /// public bool ReadOnly { get { return readOnly; } } /// /// Gets if the target property is write-only. /// public bool WriteOnly { get { return writeOnly; } } } #endregion #region String /// /// A delegate pointing to properties. /// public sealed class StringPropertyDelegate : PropertyDelegate { delegate string DelegateGet(); delegate void DelegateSet(string value); DelegateGet getDelegate; DelegateSet setDelegate; /// /// Gets or sets the value of the target property. /// public string Value { get { if (WriteOnly) throw new InvalidOperationException("This delegate is write-only."); return getDelegate(); } set { if (ReadOnly) throw new InvalidOperationException("This delegate is read-only."); setDelegate(value); } } /// /// Creates a new PropertyDelegate pointing to the specified property. /// public StringPropertyDelegate(object target, string propertyName) : base(target, propertyName) { } /// /// Is used internally to create the get-delegate. /// protected override void MakeGet() { getDelegate = (DelegateGet)Delegate.CreateDelegate(typeof(DelegateGet), Target, "get_" + PropertyName); } /// /// Is used internally to create the set-delegate. /// protected override void MakeSet() { setDelegate = (DelegateSet)Delegate.CreateDelegate(typeof(DelegateSet), Target, "set_" + PropertyName); } } #endregion // continue this for every type you need... // or use generics if you have .NET 2.0! }