diff --git a/Stylet.sln.DotSettings b/Stylet.sln.DotSettings new file mode 100644 index 0000000..a2cf130 --- /dev/null +++ b/Stylet.sln.DotSettings @@ -0,0 +1,13 @@ + + HINT + HINT + HINT + WARNING + HINT + HINT + HINT + HINT + HINT + UI + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb"><ExtraRule Prefix="_" Suffix="" Style="aaBb" /></Policy> + <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb"><ExtraRule Prefix="_" Suffix="" Style="aaBb" /></Policy> \ No newline at end of file diff --git a/Stylet/BindableCollection.cs b/Stylet/BindableCollection.cs index f1293c1..706a678 100644 --- a/Stylet/BindableCollection.cs +++ b/Stylet/BindableCollection.cs @@ -29,7 +29,7 @@ namespace Stylet /// Interface encapsulating IReadOnlyList and INotifyCollectionChanged /// /// The type of elements in the collection - public interface IReadOnlyObservableCollection : IReadOnlyList, INotifyCollectionChanged, INotifyCollectionChanging + public interface IReadOnlyObservableCollection : IReadOnlyList, INotifyCollectionChanged, INotifyCollectionChanging { } /// @@ -112,7 +112,7 @@ namespace Stylet var previousNotificationSetting = this.isNotifying; this.isNotifying = false; - var index = Count; + var index = this.Count; foreach (var item in items) { base.InsertItem(index, item); @@ -140,7 +140,7 @@ namespace Stylet this.isNotifying = false; foreach (var item in items) { - var index = IndexOf(item); + var index = this.IndexOf(item); if (index >= 0) base.RemoveItem(index); } diff --git a/Stylet/BootstrapperBase.cs b/Stylet/BootstrapperBase.cs index 365b207..0731086 100644 --- a/Stylet/BootstrapperBase.cs +++ b/Stylet/BootstrapperBase.cs @@ -34,7 +34,7 @@ namespace Stylet /// /// Initialises a new instance of the class /// - public BootstrapperBase() + protected BootstrapperBase() { this.Assemblies = new List() { typeof(BootstrapperBase<>).Assembly, this.GetType().Assembly }; } diff --git a/Stylet/EventAggregator.cs b/Stylet/EventAggregator.cs index d636134..57a42dc 100644 --- a/Stylet/EventAggregator.cs +++ b/Stylet/EventAggregator.cs @@ -17,7 +17,7 @@ namespace Stylet /// Implement this to handle a particular message type /// /// Message type to handle. Can be a base class of the messsage type(s) to handle - public interface IHandle : IHandle + public interface IHandle : IHandle { /// /// Called whenever a message of type TMessageType is posted @@ -137,7 +137,7 @@ namespace Stylet } if (channels.Length == 0) - channels = new[] { EventAggregator.DefaultChannel }; + channels = new[] { DefaultChannel }; this.SubscribeToChannels(channels); } @@ -167,7 +167,7 @@ namespace Stylet return false; if (channels.Length == 0) - channels = new[] { EventAggregator.DefaultChannel }; + channels = new[] { DefaultChannel }; // We're not subscribed to any of the channels if (!channels.All(x => this.channels.Contains(x))) diff --git a/Stylet/Execute.cs b/Stylet/Execute.cs index a0d3d6a..3000de3 100644 --- a/Stylet/Execute.cs +++ b/Stylet/Execute.cs @@ -2,7 +2,6 @@ using System.ComponentModel; using System.Threading.Tasks; using System.Windows; -using System.Windows.Threading; namespace Stylet { diff --git a/Stylet/ExpressionExtensions.cs b/Stylet/ExpressionExtensions.cs index 9ac7ac9..da09c99 100644 --- a/Stylet/ExpressionExtensions.cs +++ b/Stylet/ExpressionExtensions.cs @@ -17,8 +17,9 @@ namespace Stylet public static string NameForProperty(this Expression propertyExpression) { Expression body; - if (propertyExpression.Body is UnaryExpression) - body = ((UnaryExpression)propertyExpression.Body).Operand; + var expression = propertyExpression.Body as UnaryExpression; + if (expression != null) + body = expression.Operand; else body = propertyExpression.Body; diff --git a/Stylet/IBootstrapper.cs b/Stylet/IBootstrapper.cs index cdbb23a..b779718 100644 --- a/Stylet/IBootstrapper.cs +++ b/Stylet/IBootstrapper.cs @@ -1,6 +1,5 @@ using System; using System.Windows; -using System.Windows.Threading; namespace Stylet { diff --git a/Stylet/IConductor.cs b/Stylet/IConductor.cs index 84813af..9bf05e3 100644 --- a/Stylet/IConductor.cs +++ b/Stylet/IConductor.cs @@ -46,6 +46,8 @@ namespace Stylet /// Thing which owns one or more children, and can manage their lifecycles accordingly /// /// Type of child being conducted + // ReSharper disable once TypeParameterCanBeVariant + // Not sure whether this might change in future... public interface IConductor { /// diff --git a/Stylet/IDispatcher.cs b/Stylet/IDispatcher.cs index 4944fdb..bab53a5 100644 --- a/Stylet/IDispatcher.cs +++ b/Stylet/IDispatcher.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows.Threading; namespace Stylet diff --git a/Stylet/INotifyCollectionChanging.cs b/Stylet/INotifyCollectionChanging.cs index 8220ca5..da8d164 100644 --- a/Stylet/INotifyCollectionChanging.cs +++ b/Stylet/INotifyCollectionChanging.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; using System.Collections.Specialized; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace Stylet { @@ -15,6 +11,6 @@ namespace Stylet /// /// Occurs when the collection will change /// - event NotifyCollectionChangedEventHandler CollectionChanged; + event NotifyCollectionChangedEventHandler CollectionChanging; } } diff --git a/Stylet/IValidationAdapter.cs b/Stylet/IValidationAdapter.cs index b76b271..053b07c 100644 --- a/Stylet/IValidationAdapter.cs +++ b/Stylet/IValidationAdapter.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using System.Threading.Tasks; namespace Stylet @@ -15,6 +14,7 @@ namespace Stylet /// and request a new IModelValidator{MyViewModelType} in your ViewModel's constructor. /// /// Type of model being validated + // ReSharper disable once UnusedTypeParameter public interface IModelValidator : IModelValidator { } diff --git a/Stylet/MessageBoxView.xaml b/Stylet/MessageBoxView.xaml index 432798b..2f4d1d8 100644 --- a/Stylet/MessageBoxView.xaml +++ b/Stylet/MessageBoxView.xaml @@ -4,14 +4,16 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:s="clr-namespace:Stylet.Xaml" + xmlns:stylet="clr-namespace:Stylet" mc:Ignorable="d" + d:DataContext="{d:DesignInstance stylet:MessageBoxViewModel}" ResizeMode="NoResize" SizeToContent="WidthAndHeight" FlowDirection="{Binding FlowDirection}" WindowStartupLocation="CenterScreen" ShowInTaskbar="False" MinWidth="160" MaxWidth="485" - x:Name="rootObject"> + x:Name="RootObject"> @@ -31,7 +33,7 @@ - @@ -45,13 +47,13 @@ - + - + diff --git a/Stylet/PropertyChangedBase.cs b/Stylet/PropertyChangedBase.cs index c6a9ed5..a9acdbb 100644 --- a/Stylet/PropertyChangedBase.cs +++ b/Stylet/PropertyChangedBase.cs @@ -87,7 +87,7 @@ namespace Stylet if (!EqualityComparer.Default.Equals(field, value)) { field = value; - this.NotifyOfPropertyChange(propertyName); + this.NotifyOfPropertyChange(propertyName: propertyName); } } } diff --git a/Stylet/PropertyChangedExtensions.cs b/Stylet/PropertyChangedExtensions.cs index 2335a37..3ba3caa 100644 --- a/Stylet/PropertyChangedExtensions.cs +++ b/Stylet/PropertyChangedExtensions.cs @@ -48,8 +48,8 @@ namespace Stylet { internal class StrongPropertyChangedBinding : IEventBinding { - private WeakReference inpc; - private PropertyChangedEventHandler handler; + private readonly WeakReference inpc; + private readonly PropertyChangedEventHandler handler; public StrongPropertyChangedBinding(INotifyPropertyChanged inpc, PropertyChangedEventHandler handler) { @@ -68,9 +68,9 @@ namespace Stylet internal class WeakPropertyChangedHandler : IEventBinding where TSource : class, INotifyPropertyChanged { private readonly WeakReference source; - private EventHandler> handler; - private string propertyName; - private Func valueSelector; + private readonly EventHandler> handler; + private readonly string propertyName; + private readonly Func valueSelector; public WeakPropertyChangedHandler(TSource source, Expression> selector, EventHandler> handler) { @@ -104,7 +104,7 @@ namespace Stylet internal class WeakPropertyChangedBinding : IEventBinding { - private WeakReference wrappedBinding; + private readonly WeakReference wrappedBinding; public WeakPropertyChangedBinding(IEventBinding wrappedBinding) { diff --git a/Stylet/Screen.cs b/Stylet/Screen.cs index 86c1394..cf63ee4 100644 --- a/Stylet/Screen.cs +++ b/Stylet/Screen.cs @@ -134,7 +134,7 @@ namespace Stylet #region IClose - private bool isClosed = false; + private bool isClosed; /// /// Called whenever this Screen is closed diff --git a/Stylet/ScreenExtensions.cs b/Stylet/ScreenExtensions.cs index 2b32993..3a8ab87 100644 --- a/Stylet/ScreenExtensions.cs +++ b/Stylet/ScreenExtensions.cs @@ -76,7 +76,7 @@ namespace Stylet public static void CloseWith(this IClose child, IClose parent) { // Using TryCloseAndDispose ensures that Dispose is called if necessary - WeakEventManager.AddHandler(parent, "Closed", (o, e) => ScreenExtensions.TryCloseAndDispose(child)); + WeakEventManager.AddHandler(parent, "Closed", (o, e) => TryCloseAndDispose(child)); } /// diff --git a/Stylet/StyletIoC/Internal/Builders/BuilderBindingBase.cs b/Stylet/StyletIoC/Internal/Builders/BuilderBindingBase.cs index 9170f6f..7aec17b 100644 --- a/Stylet/StyletIoC/Internal/Builders/BuilderBindingBase.cs +++ b/Stylet/StyletIoC/Internal/Builders/BuilderBindingBase.cs @@ -13,7 +13,7 @@ namespace StyletIoC.Internal.Builders public string Key { get; protected set; } public bool IsWeak { get; protected set; } - public BuilderBindingBase(Type serviceType) + protected BuilderBindingBase(Type serviceType) { this.ServiceType = serviceType; diff --git a/Stylet/StyletIoC/Internal/Builders/BuilderInstanceBinding.cs b/Stylet/StyletIoC/Internal/Builders/BuilderInstanceBinding.cs index a0979bf..7a50569 100644 --- a/Stylet/StyletIoC/Internal/Builders/BuilderInstanceBinding.cs +++ b/Stylet/StyletIoC/Internal/Builders/BuilderInstanceBinding.cs @@ -1,9 +1,5 @@ -using StyletIoC.Internal.Creators; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System; +using StyletIoC.Internal.Creators; namespace StyletIoC.Internal.Builders { diff --git a/Stylet/StyletIoC/Internal/Builders/BuilderToAllImplementationsBinding.cs b/Stylet/StyletIoC/Internal/Builders/BuilderToAllImplementationsBinding.cs index 2323b87..1856a52 100644 --- a/Stylet/StyletIoC/Internal/Builders/BuilderToAllImplementationsBinding.cs +++ b/Stylet/StyletIoC/Internal/Builders/BuilderToAllImplementationsBinding.cs @@ -8,7 +8,7 @@ namespace StyletIoC.Internal.Builders { internal class BuilderToAllImplementationsBinding : BuilderBindingBase { - private IEnumerable assemblies; + private readonly IEnumerable assemblies; public BuilderToAllImplementationsBinding(Type serviceType, IEnumerable assemblies) : base(serviceType) diff --git a/Stylet/StyletIoC/Internal/Builders/BuilderTypeBinding.cs b/Stylet/StyletIoC/Internal/Builders/BuilderTypeBinding.cs index ed5c597..427a232 100644 --- a/Stylet/StyletIoC/Internal/Builders/BuilderTypeBinding.cs +++ b/Stylet/StyletIoC/Internal/Builders/BuilderTypeBinding.cs @@ -4,7 +4,7 @@ namespace StyletIoC.Internal.Builders { internal class BuilderTypeBinding : BuilderBindingBase { - private Type implementationType; + private readonly Type implementationType; public BuilderTypeBinding(Type serviceType, Type implementationType) : base(serviceType) diff --git a/Stylet/StyletIoC/Internal/Container.cs b/Stylet/StyletIoC/Internal/Container.cs index 1715484..b795eed 100644 --- a/Stylet/StyletIoC/Internal/Container.cs +++ b/Stylet/StyletIoC/Internal/Container.cs @@ -7,7 +7,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; -using System.Linq.Expressions; using System.Reflection; using System.Reflection.Emit; @@ -16,6 +15,7 @@ namespace StyletIoC.Internal [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1611:ElementParametersMustBeDocumented", Justification = "Internal class, but some documentation added for readability. StyleCop ignores 'Internal only' setting if some documentation exists on member")] [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1615:ElementReturnValueMustBeDocumented", Justification = "Internal class, but some documentation added for readability. StyleCop ignores 'Internal only' setting if some documentation exists on member")] [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1618:GenericTypeParametersMustBeDocumented", Justification = "Internal class, but some documentation added for readability. StyleCop ignores 'Internal only' setting if some documentation exists on member")] + // ReSharper disable once RedundantExtendsListEntry internal class Container : IContainer, IRegistrationContext { /// @@ -51,7 +51,7 @@ namespace StyletIoC.Internal /// public event EventHandler Disposing; - private bool disposed = false; + private bool disposed; /// /// Compile all known bindings (which would otherwise be compiled when needed), checking the dependency graph for consistency @@ -401,7 +401,7 @@ namespace StyletIoC.Internal // { // this.registrationContext = registrationContext; // } - var ctorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { typeof(IRegistrationContext) }); + var ctorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new[] { typeof(IRegistrationContext) }); var ilGenerator = ctorBuilder.GetILGenerator(); // Load 'this' and the registration context onto the stack ilGenerator.Emit(OpCodes.Ldarg_0); @@ -413,7 +413,7 @@ namespace StyletIoC.Internal // These are needed by all methods, so get them now // IRegistrationContext.GetTypeOrAll(Type, string) // IRegistrationContext extends ICreator, and it's ICreator that actually implements this - var containerGetMethod = typeof(IContainer).GetMethod("GetTypeOrAll", new Type[] { typeof(Type), typeof(string) }); + var containerGetMethod = typeof(IContainer).GetMethod("GetTypeOrAll", new[] { typeof(Type), typeof(string) }); // Type.GetTypeFromHandler(RuntimeTypeHandle) var typeFromHandleMethod = typeof(Type).GetMethod("GetTypeFromHandle"); diff --git a/Stylet/StyletIoC/Internal/Creators/AbstractFactoryCreator.cs b/Stylet/StyletIoC/Internal/Creators/AbstractFactoryCreator.cs index eb630e2..999871c 100644 --- a/Stylet/StyletIoC/Internal/Creators/AbstractFactoryCreator.cs +++ b/Stylet/StyletIoC/Internal/Creators/AbstractFactoryCreator.cs @@ -4,6 +4,8 @@ using System.Linq.Expressions; namespace StyletIoC.Internal.Creators { + using System.Diagnostics; + /// /// Knows how to create an instance of an abstract factory (generated by Container.GetFactoryForType) /// @@ -23,6 +25,7 @@ namespace StyletIoC.Internal.Creators public Expression GetInstanceExpression(ParameterExpression registrationContext) { var ctor = this.abstractFactoryType.GetConstructor(new[] { typeof(IRegistrationContext) }); + Debug.Assert(ctor != null); var construction = Expression.New(ctor, registrationContext); return construction; } diff --git a/Stylet/StyletIoC/Internal/Creators/CreatorBase.cs b/Stylet/StyletIoC/Internal/Creators/CreatorBase.cs index 8d07337..14cfba7 100644 --- a/Stylet/StyletIoC/Internal/Creators/CreatorBase.cs +++ b/Stylet/StyletIoC/Internal/Creators/CreatorBase.cs @@ -13,7 +13,7 @@ namespace StyletIoC.Internal.Creators public virtual Type Type { get; protected set; } protected IRegistrationContext ParentContext { get; set; } - public CreatorBase(IRegistrationContext parentContext) + protected CreatorBase(IRegistrationContext parentContext) { this.ParentContext = parentContext; } diff --git a/Stylet/StyletIoC/Internal/Creators/InstanceCreator.cs b/Stylet/StyletIoC/Internal/Creators/InstanceCreator.cs index 2239dbd..b96e8ea 100644 --- a/Stylet/StyletIoC/Internal/Creators/InstanceCreator.cs +++ b/Stylet/StyletIoC/Internal/Creators/InstanceCreator.cs @@ -1,10 +1,6 @@ using StyletIoC.Creation; using System; -using System.Collections.Generic; -using System.Linq; using System.Linq.Expressions; -using System.Text; -using System.Threading.Tasks; namespace StyletIoC.Internal.Creators { diff --git a/Stylet/StyletIoC/Internal/Creators/TypeCreator.cs b/Stylet/StyletIoC/Internal/Creators/TypeCreator.cs index ebabb29..263defb 100644 --- a/Stylet/StyletIoC/Internal/Creators/TypeCreator.cs +++ b/Stylet/StyletIoC/Internal/Creators/TypeCreator.cs @@ -55,7 +55,7 @@ namespace StyletIoC.Internal.Creators { ctor = ctorsWithAttribute[0]; var key = ctorsWithAttribute[0].GetCustomAttribute(true).Key; - var cantResolve = ctor.GetParameters().Where(p => !this.ParentContext.CanResolve(p.ParameterType, key) && !p.HasDefaultValue).FirstOrDefault(); + var cantResolve = ctor.GetParameters().FirstOrDefault(p => !this.ParentContext.CanResolve(p.ParameterType, key) && !p.HasDefaultValue); if (cantResolve != null) throw new StyletIoCFindConstructorException(String.Format("Found a constructor with [Inject] on type {0}, but can't resolve parameter '{1}' (of type {2}, and doesn't have a default value).", this.Type.GetDescription(), cantResolve.Name, cantResolve.ParameterType.GetDescription())); } diff --git a/Stylet/StyletIoC/Internal/RegistrationCollections/EmptyRegistrationCollection.cs b/Stylet/StyletIoC/Internal/RegistrationCollections/EmptyRegistrationCollection.cs index 7a16d8e..3c83107 100644 --- a/Stylet/StyletIoC/Internal/RegistrationCollections/EmptyRegistrationCollection.cs +++ b/Stylet/StyletIoC/Internal/RegistrationCollections/EmptyRegistrationCollection.cs @@ -1,9 +1,6 @@ using StyletIoC.Creation; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace StyletIoC.Internal.RegistrationCollections { diff --git a/Stylet/StyletIoC/Internal/RegistrationCollections/RegistrationCollection.cs b/Stylet/StyletIoC/Internal/RegistrationCollections/RegistrationCollection.cs index ac6f2d6..66ba98d 100644 --- a/Stylet/StyletIoC/Internal/RegistrationCollections/RegistrationCollection.cs +++ b/Stylet/StyletIoC/Internal/RegistrationCollections/RegistrationCollection.cs @@ -33,7 +33,7 @@ namespace StyletIoC.Internal.RegistrationCollections lock (this.registrationsLock) { // Should have been caught by SingleRegistration.AddRegistration - Debug.Assert(!this.registrations.Any(x => x.Type == registration.Type)); + Debug.Assert(this.registrations.All(x => x.Type != registration.Type)); this.registrations.Add(registration); return this; } diff --git a/Stylet/StyletIoC/Internal/Registrations/FuncNoKeyRegistration.cs b/Stylet/StyletIoC/Internal/Registrations/FuncNoKeyRegistration.cs index 3253226..982d812 100644 --- a/Stylet/StyletIoC/Internal/Registrations/FuncNoKeyRegistration.cs +++ b/Stylet/StyletIoC/Internal/Registrations/FuncNoKeyRegistration.cs @@ -12,7 +12,7 @@ namespace StyletIoC.Internal.Registrations { private readonly Type funcType; private readonly Func generator; - private IRegistration delegateRegistration; + private readonly IRegistration delegateRegistration; public Type Type { diff --git a/Stylet/StyletIoC/Internal/Registrations/GetAllRegistration.cs b/Stylet/StyletIoC/Internal/Registrations/GetAllRegistration.cs index 03b3e0b..d308cb5 100644 --- a/Stylet/StyletIoC/Internal/Registrations/GetAllRegistration.cs +++ b/Stylet/StyletIoC/Internal/Registrations/GetAllRegistration.cs @@ -6,6 +6,8 @@ using System.Threading; namespace StyletIoC.Internal.Registrations { + using System.Diagnostics; + /// /// Knows how to generate an IEnumerable{T}, which contains all implementations of T /// @@ -54,6 +56,7 @@ namespace StyletIoC.Internal.Registrations var instanceExpressions = this.parentContext.GetAllRegistrations(this.Type.GenericTypeArguments[0], this.Key, false).Select(x => x.GetInstanceExpression(registrationContext)).ToArray(); var listCtor = this.Type.GetConstructor(new[] { typeof(int) }); // ctor which takes capacity + Debug.Assert(listCtor != null); var listNew = Expression.New(listCtor, Expression.Constant(instanceExpressions.Length)); Expression list = instanceExpressions.Any() ? (Expression)Expression.ListInit(listNew, instanceExpressions) : listNew; diff --git a/Stylet/StyletIoC/Internal/Registrations/RegistrationBase.cs b/Stylet/StyletIoC/Internal/Registrations/RegistrationBase.cs index 2445695..21e26c2 100644 --- a/Stylet/StyletIoC/Internal/Registrations/RegistrationBase.cs +++ b/Stylet/StyletIoC/Internal/Registrations/RegistrationBase.cs @@ -15,7 +15,7 @@ namespace StyletIoC.Internal.Registrations private readonly object generatorLock = new object(); protected Func Generator { get; set; } - public RegistrationBase(ICreator creator) + protected RegistrationBase(ICreator creator) { this.Creator = creator; } diff --git a/Stylet/StyletIoC/StyletIoCModule.cs b/Stylet/StyletIoC/StyletIoCModule.cs index b7908d5..b73cf95 100644 --- a/Stylet/StyletIoC/StyletIoCModule.cs +++ b/Stylet/StyletIoC/StyletIoCModule.cs @@ -1,9 +1,6 @@ using StyletIoC.Internal.Builders; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace StyletIoC { diff --git a/Stylet/ValidatingModelBase.cs b/Stylet/ValidatingModelBase.cs index 341a7d7..c426c8e 100644 --- a/Stylet/ValidatingModelBase.cs +++ b/Stylet/ValidatingModelBase.cs @@ -190,6 +190,9 @@ namespace Stylet if (this.Validator == null) throw new InvalidOperationException("Can't run validation if a validator hasn't been set"); + if (propertyName == null) + propertyName = String.Empty; + // To allow synchronous calling of this method, we need to resume on the ThreadPool. // Therefore, we might resume on any thread, hence the need for a lock var newErrorsRaw = await this.Validator.ValidatePropertyAsync(propertyName).ConfigureAwait(false); diff --git a/Stylet/WindowManager.cs b/Stylet/WindowManager.cs index 7c5d1b5..f03bfe4 100644 --- a/Stylet/WindowManager.cs +++ b/Stylet/WindowManager.cs @@ -161,6 +161,7 @@ namespace Stylet } // This gets itself retained by the window, by registering events + // ReSharper disable once ObjectCreationAsStatement new WindowConductor(window, viewModel); return window; @@ -171,8 +172,8 @@ namespace Stylet if (Application.Current == null) return null; - var active = Application.Current.Windows.OfType().Where(x => x.IsActive).FirstOrDefault() ?? Application.Current.MainWindow; - return active == window ? null : active; + var active = Application.Current.Windows.OfType().FirstOrDefault(x => x.IsActive) ?? Application.Current.MainWindow; + return ReferenceEquals(active, window) ? null : active; } private class WindowConductor : IChildDelegate diff --git a/Stylet/Xaml/ActionExtension.cs b/Stylet/Xaml/ActionExtension.cs index 1e34660..38be647 100644 --- a/Stylet/Xaml/ActionExtension.cs +++ b/Stylet/Xaml/ActionExtension.cs @@ -84,12 +84,11 @@ namespace Stylet.Xaml return new CommandAction((DependencyObject)valueService.TargetObject, this.Method, nullTarget, actionNotFound); } - var propertyAsEventInfo = valueService.TargetProperty as EventInfo; - if (propertyAsEventInfo != null) + if (valueService.TargetProperty is EventInfo) { var nullTarget = this.NullTarget == ActionUnavailableBehaviour.Default ? ActionUnavailableBehaviour.Enable : this.NullTarget; var actionNotFound = this.ActionNotFound == ActionUnavailableBehaviour.Default ? ActionUnavailableBehaviour.Throw : this.ActionNotFound; - var ec = new EventAction((DependencyObject)valueService.TargetObject, propertyAsEventInfo, this.Method, nullTarget, actionNotFound); + var ec = new EventAction((DependencyObject)valueService.TargetObject, this.Method, nullTarget, actionNotFound); return ec.GetDelegate(); } diff --git a/Stylet/Xaml/BoolToVisibilityConverter.cs b/Stylet/Xaml/BoolToVisibilityConverter.cs index 7b026a8..23bbee9 100644 --- a/Stylet/Xaml/BoolToVisibilityConverter.cs +++ b/Stylet/Xaml/BoolToVisibilityConverter.cs @@ -22,8 +22,8 @@ namespace Stylet.Xaml /// public Visibility TrueVisibility { - get { return (Visibility)GetValue(TrueVisibilityProperty); } - set { SetValue(TrueVisibilityProperty, value); } + get { return (Visibility)this.GetValue(TrueVisibilityProperty); } + set { this.SetValue(TrueVisibilityProperty, value); } } /// @@ -37,8 +37,8 @@ namespace Stylet.Xaml /// public Visibility FalseVisibility { - get { return (Visibility)GetValue(FalseVisibilityProperty); } - set { SetValue(FalseVisibilityProperty, value); } + get { return (Visibility)this.GetValue(FalseVisibilityProperty); } + set { this.SetValue(FalseVisibilityProperty, value); } } /// @@ -66,6 +66,7 @@ namespace Stylet.Xaml { result = (bool)value; } + // ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull else if (value is IEnumerable) { result = ((IEnumerable)value).GetEnumerator().MoveNext(); @@ -82,7 +83,7 @@ namespace Stylet.Xaml // Easiest is just to try it and see try { - result = !value.Equals(System.Convert.ChangeType((object)0, value.GetType())); + result = !value.Equals(System.Convert.ChangeType(0, value.GetType())); } catch { diff --git a/Stylet/Xaml/DebugConverter.cs b/Stylet/Xaml/DebugConverter.cs index 15b17b5..bbba19e 100644 --- a/Stylet/Xaml/DebugConverter.cs +++ b/Stylet/Xaml/DebugConverter.cs @@ -22,8 +22,8 @@ namespace Stylet.Xaml /// public string Name { - get { return (string)GetValue(NameProperty); } - set { SetValue(NameProperty, value); } + get { return (string)this.GetValue(NameProperty); } + set { this.SetValue(NameProperty, value); } } /// @@ -37,8 +37,8 @@ namespace Stylet.Xaml /// public Action Logger { - get { return (Action)GetValue(LoggerProperty); } - set { SetValue(LoggerProperty, value); } + get { return (Action)this.GetValue(LoggerProperty); } + set { this.SetValue(LoggerProperty, value); } } /// diff --git a/Stylet/Xaml/EqualityConverter.cs b/Stylet/Xaml/EqualityConverter.cs index 549e39a..de682e7 100644 --- a/Stylet/Xaml/EqualityConverter.cs +++ b/Stylet/Xaml/EqualityConverter.cs @@ -22,8 +22,8 @@ namespace Stylet.Xaml /// public bool Invert { - get { return (bool)GetValue(InvertProperty); } - set { SetValue(InvertProperty, value); } + get { return (bool)this.GetValue(InvertProperty); } + set { this.SetValue(InvertProperty, value); } } /// @@ -49,7 +49,7 @@ namespace Stylet.Xaml if (values == null || values.Length == 0) return null; var first = values.FirstOrDefault(); - var result = values.Skip(1).All(x => first.Equals(x)); + var result = values.Skip(1).All(x => x.Equals(first)); return this.Invert ? !result : result; } diff --git a/Stylet/Xaml/EventAction.cs b/Stylet/Xaml/EventAction.cs index d037010..a2eccb5 100644 --- a/Stylet/Xaml/EventAction.cs +++ b/Stylet/Xaml/EventAction.cs @@ -19,17 +19,12 @@ namespace Stylet.Xaml /// /// View whose View.ActionTarget we watch /// - private DependencyObject subject; - - /// - /// Property on the WPF element we're returning a delegate for - /// - private EventInfo targetProperty; + private readonly DependencyObject subject; /// /// The MyMethod in {s:Action MyMethod}, this is what we call when the event's fired /// - private string methodName; + private readonly string methodName; /// /// MethodInfo for the method to call. This has to exist, or we throw a wobbly @@ -42,11 +37,10 @@ namespace Stylet.Xaml /// Initialises a new instance of the class /// /// View whose View.ActionTarget we watch - /// Property on the WPF element we're returning a delegate for /// The MyMethod in {s:Action MyMethod}, this is what we call when the event's fired /// Behaviour for it the relevant View.ActionTarget is null /// Behaviour for if the action doesn't exist on the View.ActionTarget - public EventAction(DependencyObject subject, EventInfo targetProperty, string methodName, ActionUnavailableBehaviour targetNullBehaviour, ActionUnavailableBehaviour actionNonExistentBehaviour) + public EventAction(DependencyObject subject, string methodName, ActionUnavailableBehaviour targetNullBehaviour, ActionUnavailableBehaviour actionNonExistentBehaviour) { if (targetNullBehaviour == ActionUnavailableBehaviour.Disable) throw new ArgumentException("Setting NullTarget = Disable is unsupported when used on an Event"); @@ -54,7 +48,6 @@ namespace Stylet.Xaml throw new ArgumentException("Setting ActionNotFound = Disable is unsupported when used on an Event"); this.subject = subject; - this.targetProperty = targetProperty; this.methodName = methodName; this.targetNullBehaviour = targetNullBehaviour; this.actionNonExistentBehaviour = actionNonExistentBehaviour; diff --git a/Stylet/Xaml/View.cs b/Stylet/Xaml/View.cs index 1e65494..c15d70c 100644 --- a/Stylet/Xaml/View.cs +++ b/Stylet/Xaml/View.cs @@ -82,7 +82,7 @@ namespace Stylet.Xaml { if (Execute.InDesignMode) { - var bindingExpression = BindingOperations.GetBindingExpression(d, View.ModelProperty); + var bindingExpression = BindingOperations.GetBindingExpression(d, ModelProperty); string text; if (bindingExpression == null) text = "View for [Broken Binding]"; @@ -90,7 +90,7 @@ namespace Stylet.Xaml text = String.Format("View for child ViewModel on {0}", bindingExpression.DataItem.GetType().Name); else text = String.Format("View for {0}.{1}", bindingExpression.DataItem.GetType().Name, bindingExpression.ResolvedSourcePropertyName); - View.SetContentProperty(d, new System.Windows.Controls.TextBlock() { Text = text }); + SetContentProperty(d, new System.Windows.Controls.TextBlock() { Text = text }); } else { diff --git a/StyletUnitTests/EventActionTests.cs b/StyletUnitTests/EventActionTests.cs index 5f1f32b..34fbc98 100644 --- a/StyletUnitTests/EventActionTests.cs +++ b/StyletUnitTests/EventActionTests.cs @@ -44,59 +44,57 @@ namespace StyletUnitTests private DependencyObject subject; private Target target; - private EventInfo eventInfo; [SetUp] public void SetUp() { this.target = new Target(); this.subject = new Button(); - this.eventInfo = typeof(Button).GetEvent("Click"); View.SetActionTarget(this.subject, this.target); } [Test] public void ThrowsIfNullTargetBehaviourIsDisable() { - Assert.Throws(() => new EventAction(this.subject, this.eventInfo, "DoSomething", ActionUnavailableBehaviour.Disable, ActionUnavailableBehaviour.Enable)); + Assert.Throws(() => new EventAction(this.subject, "DoSomething", ActionUnavailableBehaviour.Disable, ActionUnavailableBehaviour.Enable)); } [Test] public void ThrowsIfNonExistentActionBehaviourIsDisable() { - Assert.Throws(() => new EventAction(this.subject, this.eventInfo, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Disable)); + Assert.Throws(() => new EventAction(this.subject, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Disable)); } [Test] public void ThrowsIfTargetNullBehaviourIsThrowAndTargetBecomesNull() { - var cmd = new EventAction(this.subject, this.eventInfo, "DoSomething", ActionUnavailableBehaviour.Throw, ActionUnavailableBehaviour.Enable); + var cmd = new EventAction(this.subject, "DoSomething", ActionUnavailableBehaviour.Throw, ActionUnavailableBehaviour.Enable); Assert.Throws(() => View.SetActionTarget(this.subject, null)); } [Test] public void ThrowsIfActionNonExistentBehaviourIsThrowAndActionIsNonExistent() { - var cmd = new EventAction(this.subject, this.eventInfo, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Throw); + var cmd = new EventAction(this.subject, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Throw); Assert.Throws(() => View.SetActionTarget(this.subject, new Target2())); } [Test] public void ThrowsIfMethodHasTooManyArguments() { - Assert.Throws(() => new EventAction(this.subject, this.eventInfo, "DoSomethingWithTooManyArgs", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable)); + Assert.Throws(() => new EventAction(this.subject, "DoSomethingWithTooManyArgs", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable)); } [Test] public void ThrowsIfMethodHasBadParameter() { - Assert.Throws(() => new EventAction(this.subject, this.eventInfo, "DoSomethingWithBadArgument", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable)); + Assert.Throws(() => new EventAction(this.subject, "DoSomethingWithBadArgument", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable)); } [Test] public void InvokingCommandDoesNothingIfTargetIsNull() { - var cmd = new EventAction(this.subject, this.eventInfo, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable); + var cmd = new EventAction(this.subject, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable); View.SetActionTarget(this.subject, null); cmd.GetDelegate().DynamicInvoke(null, null); } @@ -104,7 +102,7 @@ namespace StyletUnitTests [Test] public void InvokingCommandDoesNothingIfActionIsNonExistent() { - var cmd = new EventAction(this.subject, this.eventInfo, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable); + var cmd = new EventAction(this.subject, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable); View.SetActionTarget(this.subject, new Target2()); cmd.GetDelegate().DynamicInvoke(null, null); } @@ -112,7 +110,7 @@ namespace StyletUnitTests [Test] public void InvokingCommandCallsMethod() { - var cmd = new EventAction(this.subject, this.eventInfo, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable); + var cmd = new EventAction(this.subject, "DoSomething", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable); cmd.GetDelegate().DynamicInvoke(null, null); Assert.True(this.target.DoSomethingCalled); } @@ -120,7 +118,7 @@ namespace StyletUnitTests [Test] public void InvokingCommandCallsMethodWithEventArgs() { - var cmd = new EventAction(this.subject, this.eventInfo, "DoSomethingWithEventArgs", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable); + var cmd = new EventAction(this.subject, "DoSomethingWithEventArgs", ActionUnavailableBehaviour.Enable, ActionUnavailableBehaviour.Enable); var arg = new RoutedEventArgs(); cmd.GetDelegate().DynamicInvoke(null, arg); Assert.AreEqual(arg, this.target.EventArgs);