using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Reflection; namespace Caliburn.Metadata { public class PresenterInfo : InfoBase { #region Fields private readonly Dictionary _actions = new Dictionary(); #endregion #region Constructor private PresenterInfo() {} #endregion #region Properties public IList Actions { get { return new ReadOnlyCollection( new List(_actions.Values) ); } } #endregion #region Methods public ActionInfo FindAction(string action) { ActionInfo info; _actions.TryGetValue(action, out info); return info; } #endregion #region Factory public static PresenterInfo Create(Type presenterType) { PresenterInfo info = new PresenterInfo(); LoadMetadata(presenterType, info); CreateActions(presenterType, info); return info; } private static void LoadMetadata(Type presenterType, PresenterInfo info) { object[] attributes = presenterType.GetCustomAttributes(true); foreach(object att in attributes) { MetadataProvider provider = att as MetadataProvider; if(provider != null) info.AddMetadata(provider); else { PresenterAttribute presenterAttribute = att as PresenterAttribute; if(presenterAttribute != null) { if(!string.IsNullOrEmpty(presenterAttribute.Rescue)) info.InitializeRescue(presenterType, presenterAttribute.Rescue); } } } } private static void CreateActions(Type presenterType, PresenterInfo info) { MethodInfo[] methodInfos = presenterType.GetMethods(BindingFlags.Instance | BindingFlags.Public); foreach(MethodInfo methodInfo in methodInfos) { ActionInfo actionInfo = ActionInfo.Create(info, methodInfo); if(actionInfo != null) info._actions[actionInfo.Name] = actionInfo; } } #endregion } }