using System; using System.Collections.Generic; using System.Text; using System.ComponentModel; using System.ComponentModel.Design; using System.Reflection; namespace PresentationModelBase { /// <summary> /// Base class for all PresentationModels. /// </summary> public abstract class PresentationModel : IPresentationModel { static PresentationModel() { _serviceLocator = AppDomain.CurrentDomain.GetData("servicelocator") as IServiceProvider; if (_serviceLocator == null) _serviceLocator = CreateProvider(); } private static IServiceProvider CreateProvider() { ServiceCreator creator = new ServiceCreator(); return new ServiceContainer(creator); } #region Fields static IServiceProvider _serviceLocator; #endregion #region Methods protected T Get<T>() { return (T)_serviceLocator.GetService(typeof(T)); } public virtual void InvokeMethod(string methodName, object[] parameters, params Type[] typeParameters) { MethodInfo currentMethod = this.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); if (currentMethod == null) throw new ArgumentException("Cannot find this method"); Type returnType = currentMethod.ReturnType; if (returnType != typeof(void)) throw new ArgumentException("Methods called by this command must return void"); if (currentMethod.IsGenericMethodDefinition) currentMethod = currentMethod.MakeGenericMethod(typeParameters); currentMethod.Invoke(this, parameters); } #endregion #region Properties private string _displayName; public virtual string DisplayName { get { return _displayName; } set { _displayName = value; } } #endregion #region INotifyPropertyChanged Members private PropertyChangedEventHandler _handler; public event PropertyChangedEventHandler PropertyChanged { add { _handler += value; } remove { _handler -= value; } } protected virtual void OnPropertyChanged(string propertyName) { if (_handler != null) { var e = new PropertyChangedEventArgs(propertyName); _handler(this, e); } } #endregion #region IDisposable Members public void Dispose() { OnDispose(); } protected virtual void OnDispose() { } #endregion } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)