diff --git a/phirSOFT.ContextProperties/ContextProperty.cs b/phirSOFT.ContextProperties/ContextProperty.cs index bce64ce..233a5d1 100644 --- a/phirSOFT.ContextProperties/ContextProperty.cs +++ b/phirSOFT.ContextProperties/ContextProperty.cs @@ -17,7 +17,7 @@ public ContextProperty(IContextPool, TValue> contextPoo public IContextPool, TValue> ContextPool { get; } - public IContextProvider, TValue> DefaultContext { get; set; } + public IContextProvider, TValue> DefaultContext { get; set; } public TValue this[object target, IContextProvider, TValue> context] => ContextPool[context] .First(c => c.OverridesValue(target, this)).GetValue(target, this); diff --git a/phirSOFT.ContextProperties/IContextProperty.cs b/phirSOFT.ContextProperties/IContextProperty.cs new file mode 100644 index 0000000..d57ccab --- /dev/null +++ b/phirSOFT.ContextProperties/IContextProperty.cs @@ -0,0 +1,9 @@ +namespace phirSOFT.ContextProperties +{ + public interface IContextProperty + { + TValue this[object instance, IContextProvider, TValue> context] { get; } + + IContextProvider, TValue> DefaultContext { get; } + } +} \ No newline at end of file diff --git a/phirSOFT.ContextProperties/IContextProvider.cs b/phirSOFT.ContextProperties/IContextProvider.cs index e71235f..4c31dc0 100644 --- a/phirSOFT.ContextProperties/IContextProvider.cs +++ b/phirSOFT.ContextProperties/IContextProvider.cs @@ -7,8 +7,4 @@ public interface IContextProvider where TProperty : IC TValue GetValue(object targetObject, TProperty targetProperty); bool OverridesValue(object targetObject, TProperty targetProperty); } - - public interface IContextProperty - { - } } \ No newline at end of file diff --git a/phirSOFT.ContextProperties/IMemberContextProperty.cs b/phirSOFT.ContextProperties/IMemberContextProperty.cs new file mode 100644 index 0000000..dd1605b --- /dev/null +++ b/phirSOFT.ContextProperties/IMemberContextProperty.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using System.Text; + +namespace phirSOFT.ContextProperties +{ + /// + /// Provides a way to define a context property in an interface. + /// + /// The type of the property in the interface. + public interface IMemberContextProperty + { + TValue this[IContextProvider, TValue> context] { get; } + + TValue Value { get; } + } +} diff --git a/phirSOFT.ContextProperties/MemberContextProperty.cs b/phirSOFT.ContextProperties/MemberContextProperty.cs new file mode 100644 index 0000000..ec753ea --- /dev/null +++ b/phirSOFT.ContextProperties/MemberContextProperty.cs @@ -0,0 +1,25 @@ +using System; + +namespace phirSOFT.ContextProperties +{ + /// + /// Implements the . This implementation does not keep the object alive. + /// + /// + public class MemberContextProperty : IMemberContextProperty + { + private readonly IContextProperty _contextProperty; + private readonly WeakReference _instance; + + public MemberContextProperty(IContextProperty contextProperty, object instance) + { + _contextProperty = contextProperty; + _instance = new WeakReference(instance); + } + + public TValue this[IContextProvider, TValue> context] => + _contextProperty[_instance, context]; + + public TValue Value => _contextProperty[_instance.Target, _contextProperty.DefaultContext]; + } +} \ No newline at end of file