public Type GetMapping(Type currentInterface, object attribute) { var currentImplementation = this.implementations[currentInterface]; Type type = null; if (attribute is InjectAttribute) { if (currentImplementation.Count > 0) { type = currentImplementation.Values.First(); } else { throw new ArgumentException("No available mapping for class: " + currentInterface.FullName); } } else if (attribute is NamedAttribute) { NamedAttribute namedAttribute = attribute as NamedAttribute; string dependencyName = namedAttribute.Name; type = currentImplementation[dependencyName]; } return(type); }
private bool TryGetNamedAttribute(out NamedAttribute namedAttribute) { namedAttribute = concreteType.GetCustomAttribute <NamedAttribute>() ?? actualServiceType.GetCustomAttribute <NamedAttribute>(); return(namedAttribute.IsNotNull()); }
internal CompositeFrameworkRegistration(IRegistrationResolver registrationResolver, TypeMap typeMap, IEnumerable <TypeMap> dependencies, Type castTo, bool disposable) { NamedAttribute namedAttribute = null; this.dependencies = dependencies; this.serviceType = typeMap.ServiceType; this.concreteType = typeMap.ConcreteType; this.registrationResolver = registrationResolver; registration = new CompositeRegistration { ServiceType = castTo ?? serviceType, FactoryType = MakeFactoryType(castTo ?? serviceType) }; SetLifetime(); registration.Name = typeMap.Name; if (typeMap.Name.IsNullOrEmpty() && TryGetNamedAttribute(out namedAttribute)) { registration.Name = namedAttribute.Name; } if (disposable) { registration.OwnedByContainer(); } As(concreteType, false); }
private TClass CreateConstructorInjection <TClass>() { Type desireClass = typeof(TClass); if (desireClass == null) { return(default(TClass)); } ConstructorInfo[] constructors = desireClass.GetConstructors(); foreach (var constructor in constructors) { if (!this.CheckForConstructorInjection <TClass>()) { continue; } InjectAttribute inject = (InjectAttribute)constructor .GetCustomAttributes(typeof(InjectAttribute), true) .FirstOrDefault(); ParameterInfo[] parameterTypes = constructor.GetParameters(); object[] constructorParams = new object[parameterTypes.Length]; int index = 0; foreach (ParameterInfo parameterType in parameterTypes) { NamedAttribute namedAttribute = parameterType.GetCustomAttribute <NamedAttribute>(true); Type dependancy = null; if (namedAttribute == null) { dependancy = this.module.GetMapping(parameterType.ParameterType, inject); } else { dependancy = this.module.GetMapping(parameterType.ParameterType, namedAttribute); } if (parameterType.ParameterType.IsAssignableFrom(dependancy)) { object instance = this.module.GetInstance(dependancy); if (instance == null) { instance = Activator.CreateInstance(dependancy); this.module.SetInstance(parameterType.ParameterType, instance); } constructorParams[index++] = instance; } } return((TClass)Activator.CreateInstance(desireClass, constructorParams)); } return(default(TClass)); }
public SettingsFormController(IConfig config, GestureParser parser, Win32MousePathTracker2 pathTracker, JsonGestureIntentStore intentStore, CanvasWindowGestureView gestureView, GlobalHotKeyManager hotkeyMgr) { _config = config; _parser = parser; _pathTracker = pathTracker; _intentStore = intentStore; _gestureView = gestureView; _hotkeyMgr = hotkeyMgr; #region 初始化支持的命令和命令视图 //Add Command Types SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(DoNothingCommand)), typeof(DoNothingCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(HotKeyCommand)), typeof(HotKeyCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(WebSearchCommand)), typeof(WebSearchCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(WindowControlCommand)), typeof(WindowControlCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(TaskSwitcherCommand)), typeof(TaskSwitcherCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(OpenFileCommand)), typeof(OpenFileCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(SendTextCommand)), typeof(SendTextCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(GotoUrlCommand)), typeof(GotoUrlCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(CmdCommand)), typeof(CmdCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(ScriptCommand)), typeof(ScriptCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(PauseWGesturesCommand)), typeof(PauseWGesturesCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(ChangeAudioVolumeCommand)), typeof(ChangeAudioVolumeCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(AddToBacklistCommand)), typeof(AddToBacklistCommand)); CommandViewFactory.Register <AddToBacklistCommand, GeneralNoParameterCommandView>(); CommandViewFactory.Register <OpenFileCommand, OpenFileCommandView>(); CommandViewFactory.Register <DoNothingCommand, GeneralNoParameterCommandView>(); CommandViewFactory.Register <HotKeyCommand, HotKeyCommandView>(); CommandViewFactory.Register <GotoUrlCommand, GotoUrlCommandView>(); CommandViewFactory.Register <PauseWGesturesCommand, GeneralNoParameterCommandView>(); CommandViewFactory.Register <WebSearchCommand, WebSearchCommandView>(); CommandViewFactory.Register <WindowControlCommand, WindowControlCommandView>(); CommandViewFactory.Register <CmdCommand, CmdCommandView>(); CommandViewFactory.Register <SendTextCommand, SendTextCommandView>(); CommandViewFactory.Register <TaskSwitcherCommand, TaskSwitcherCommandView>(); CommandViewFactory.Register <ScriptCommand, ScriptCommandView>(); CommandViewFactory.Register <ChangeAudioVolumeCommand, GeneralNoParameterCommandView>(); #endregion #region Hotcorner SupportedHotCornerCommands.Add(NamedAttribute.GetNameOf(typeof(DoNothingCommand)), typeof(DoNothingCommand)); SupportedHotCornerCommands.Add(NamedAttribute.GetNameOf(typeof(HotKeyCommand)), typeof(HotKeyCommand)); SupportedHotCornerCommands.Add(NamedAttribute.GetNameOf(typeof(CmdCommand)), typeof(CmdCommand)); HotCornerCommandViewFactory.Register <DoNothingCommand, GeneralNoParameterCommandView>(); HotCornerCommandViewFactory.Register <HotKeyCommand, HotKeyCommandView>(); HotCornerCommandViewFactory.Register <CmdCommand, CmdCommandView>(); #endregion _form = new SettingsForm(this); }
public SettingsFormController(IConfig config, GestureParser parser, Win32MousePathTracker2 pathTracker, JsonGestureIntentStore intentStore, CanvasWindowGestureView gestureView) { _config = config; _parser = parser; _pathTracker = pathTracker; _intentStore = intentStore; _gestureView = gestureView; #region 初始化支持的命令和命令视图 SupportedCommands = new Dictionary <string, Type>(); CommandViewFactory = new CommandViewFactory <CommandViewUserControl>() { EnableCaching = false }; //Add Command Types SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(DoNothingCommand)), typeof(DoNothingCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(HotKeyCommand)), typeof(HotKeyCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(WebSearchCommand)), typeof(WebSearchCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(WindowControlCommand)), typeof(WindowControlCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(TaskSwitcherCommand)), typeof(TaskSwitcherCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(OpenFileCommand)), typeof(OpenFileCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(SendTextCommand)), typeof(SendTextCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(GotoUrlCommand)), typeof(GotoUrlCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(CmdCommand)), typeof(CmdCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(PauseWGesturesCommand)), typeof(PauseWGesturesCommand)); SupportedCommands.Add(NamedAttribute.GetNameOf(typeof(ChangeAudioVolumeCommand)), typeof(ChangeAudioVolumeCommand)); CommandViewFactory.Register <OpenFileCommand, OpenFileCommandView>(); CommandViewFactory.Register <DoNothingCommand, GeneralNoParameterCommandView>(); CommandViewFactory.Register <HotKeyCommand, HotKeyCommandView>(); CommandViewFactory.Register <GotoUrlCommand, GotoUrlCommandView>(); CommandViewFactory.Register <PauseWGesturesCommand, GeneralNoParameterCommandView>(); CommandViewFactory.Register <WebSearchCommand, WebSearchCommandView>(); CommandViewFactory.Register <WindowControlCommand, WindowControlCommandView>(); CommandViewFactory.Register <CmdCommand, CmdCommandView>(); CommandViewFactory.Register <SendTextCommand, SendTextCommandView>(); CommandViewFactory.Register <TaskSwitcherCommand, TaskSwitcherCommandView>(); CommandViewFactory.Register <ChangeAudioVolumeCommand, GeneralNoParameterCommandView>(); #endregion _form = new SettingsForm(this); }
private TClass CreateFieldInjection <TClass>() { Type desireClass = typeof(TClass); var desireClassInstance = this.module.GetInstance(desireClass); if (desireClassInstance == null) { desireClassInstance = Activator.CreateInstance(desireClass); this.module.SetInstance(desireClass, desireClassInstance); } FieldInfo[] fields = desireClass.GetFields((BindingFlags)62); foreach (var field in fields) { if (field.GetCustomAttributes(typeof(InjectAttribute), true).Any()) { Type dependency = null; Type fieldType = field.FieldType; InjectAttribute injectAttribute = (InjectAttribute)field .GetCustomAttributes(typeof(InjectAttribute), true) .FirstOrDefault(); NamedAttribute namedAttribute = (NamedAttribute)field .GetCustomAttribute(typeof(NamedAttribute), true); if (namedAttribute == null) { dependency = this.module.GetMapping(fieldType, injectAttribute); } else { dependency = this.module.GetMapping(fieldType, namedAttribute); } if (fieldType.IsAssignableFrom(dependency)) { object instance = this.module.GetInstance(dependency); if (instance == null) { instance = Activator.CreateInstance(dependency); this.module.SetInstance(dependency, instance); } field.SetValue(desireClassInstance, instance); } } } return((TClass)desireClassInstance); }
private void radio_corner_1_CheckedChanged(object sender, EventArgs e) { if (!(sender as RadioButton).Checked) { return; } var tag = int.Parse((string)(sender as RadioButton).Tag); var cmd = Controller.IntentStore.HotCornerCommands[tag]; combo_hotcornerCmdTypes.SelectedItem = NamedAttribute.GetNameOf(cmd.GetType()); LoadHotCornerCmdView(cmd); }
public CompositeFrameworkRegistration(Type concreteType, Type serviceType, Type castAs = null) : base(concreteType, castAs ?? serviceType) { NamedAttribute namedAttribute = null; actualServiceType = serviceType; if (IsSingletonComposite()) { AsSingleton(); } if (TryGetNamedAttribute(out namedAttribute)) { Named(namedAttribute.Name); } }
public virtual object GetInstance(InjectionCore injectionCore, ProviderContext context) { if (context == null) { context = new ProviderContext(); } ParameterInfo[] parameterInfoList = this.providerMethod.GetParameters(); var parameters = new object[parameterInfoList.Length]; for (int i = 0; i < parameterInfoList.Length; i++) { NamedAttribute namedAttribute = parameterInfoList[i].GetCustomAttribute <NamedAttribute>(); if (namedAttribute == null) { if (context.TypedDependencies.Contains(parameterInfoList[i].ParameterType)) { throw new InvalidOperationException( $"Dependency loop detected when providing dependency type {parameterInfoList[i].ParameterType.Name}."); } context.TypedDependencies.Add(parameterInfoList[i].ParameterType); parameters[i] = injectionCore.Get(parameterInfoList[i].ParameterType, context); context.TypedDependencies.Remove(parameterInfoList[i].ParameterType); } else { if (context.NamedDependencies.Contains(namedAttribute.Name)) { throw new InvalidOperationException( $"Dependency loop detected when providing named dependency '{namedAttribute.Name}'."); } context.NamedDependencies.Add(namedAttribute.Name); parameters[i] = injectionCore.Get(namedAttribute.Name, context); context.NamedDependencies.Remove(namedAttribute.Name); } } ConstructorInfo constructor = this.providerMethod as ConstructorInfo; return(constructor != null ? constructor.Invoke(parameters) : this.providerMethod.Invoke(null, parameters)); }
private INamedFacet Create(NamedAttribute attribute, IFacetHolder holder) { return CreateAnnotation(attribute.Value, holder); }
private INamedFacet Create(NamedAttribute attribute, ISpecification holder) { return(CreateAnnotation(attribute.Value, holder)); }
private static string GetName(Type type) { return(NamedAttribute.GetNameFromType(type) ?? type.Name); }
public NamedAttributeContext() { attribute = new NamedAttribute("foo"); metadataMock = new Mock <IBindingMetadata>(); }
private INamedFacet Create(NamedAttribute attribute, ISpecification holder) { return CreateAnnotation(attribute.Value, holder); }
protected bool Equals(NamedAttribute other) { return(base.Equals(other) && string.Equals(Value, other.Value)); }
public NamedAttributeContext() { attribute = new NamedAttribute("foo"); metadataMock = new Mock<IBindingMetadata>(); }
/// <summary> /// Determines whether the specified <see cref="NamedAttribute" />, is equal to this instance. /// </summary> /// <param name="other">The other.</param> /// <returns>true if equal; otherwise, false</returns> protected bool Equals(NamedAttribute other) => base.Equals(other) && string.Equals(Value, other.Value);
public virtual string Description() { return(NamedAttribute.GetNameOf(this.GetType())); }