private void DoReload() { if (SelectedObject == null) { Categories = new GridEntryCollection <CategoryItem>(); Properties = new GridEntryCollection <PropertyItem>(); } else { // Collect BrowsableCategoryAttribute items var categoryAttributes = AttributeHelper.GetAttributes <BrowsableCategoryAttribute>(SelectedObject); browsableCategories = new List <BrowsableCategoryAttribute>(categoryAttributes); // Collect BrowsablePropertyAttribute items var propertyAttributes = AttributeHelper.GetAttributes <BrowsablePropertyAttribute>(SelectedObject); browsableProperties = new List <BrowsablePropertyAttribute>(propertyAttributes); // Collect categories and properties var properties = CollectProperties(currentObjects); // TODO: This needs more elegant implementation var categories = new GridEntryCollection <CategoryItem>(CollectCategories(properties)); if (_categories != null && _categories.Count > 0) { CopyCategoryFrom(_categories, categories); } Categories = categories; //new CategoryCollection(CollectCategories(properties)); Properties = new GridEntryCollection <PropertyItem>(properties); } }
public ActionResult TeamSelections(TeamSelectionsViewModel model) { if (!ModelState.IsValid) { return(View(model)); } //get league positions var selections = new List <LeaguePosition>(); var properties = AttributeHelper.GetPropertiesWithAttribute <TeamSelectionsViewModel, LeaguePositionAttribute>(); foreach (var prop in properties) { var lp = new LeaguePosition(); var attr = AttributeHelper.GetAttributes <TeamSelectionsViewModel, LeaguePositionAttribute>(prop); var league = attr.League; lp.Team = AttributeHelper.GetPropertyValue <TeamSelectionsViewModel>(model, prop); lp.Position = attr.Position; selections.Add(lp); } var email = User.Identity.GetUserName(); using (var db = new leaguepredictorEntities()) { var player = db.players.Where(p => p.email.Equals(email)).SingleOrDefault(); if (player == null) { player = new player { email = email, name = model.Name }; db.players.Add(player); db.SaveChanges(); } else { //remove previous selections foreach (var ps in player.players_selections.ToList()) { db.players_selections.Remove(ps); } } foreach (var sel in selections) { var team = db.teams.Where(t => t.team_name == sel.Team).Single(); player.players_selections.Add(new players_selections { team = team, position = sel.Position }); } db.SaveChanges(); } return(View(model)); }
private static XFrmWorkAttribute getXFrmWorkAttribute(Type type, Type interfaceType) { var item = AttributeHelper.GetAttributes <XFrmWorkAttribute>(type).FirstOrDefault(); if (item != null) { return(item); } var desc = AttributeHelper.GetAttributes <DescriptionAttribute>(type).FirstOrDefault(); var des = desc == null?type.ToString() : desc.Description; return(new XFrmWorkAttribute(type.Name, des) { MyType = type }); }
public override void SetGroupedProperty(PropertyInfo property) { if (_config.Attribute.Type != AutoFormsType.Group) { return; } var props = AttributeHelper.GetAttributes <AutoFormsAttribute>(property); if (props == null) { return; } var attribute = ControlBase.GetFilteredAttribute(_config.Filter, props); if (attribute == null) { return; } string format = null; if (string.IsNullOrEmpty(attribute.Label) == false) { format = attribute.Label + ": {0}"; } var v = CreateItemLabel(property.Name, property.PropertyType, LabelStyle, format); if (v == null) { return; } _controlGrid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); Grid.SetRow(v, _controlGrid.RowDefinitions.Count - 1); _controlGrid.Children.Add(v); }
/// <summary> /// 获取所有插件 /// </summary> /// <param name="isRecursiveDirectory">是否进行目录递归搜索</param> public static void GetAllPlugins(bool isRecursiveDirectory) { var loader = PluginLoadControllor.Instance; var allDllFileList = new List <string>(); foreach (var s in OrderedSearchFolder) { allDllFileList.AddRange( from file in Directory.GetFileSystemEntries(s) where Path.GetExtension(file) == ".dll" select file); } allDllFileList = allDllFileList.Distinct().ToList(); Type[] types = null; var dllPluginFils = new List <string>(); //最终要进行处理的的dll文件 if (PluginLoadControllor.IsNormalLoaded == false) { dllPluginFils = allDllFileList; } else { dllPluginFils.AddRange( loader.PluginDllNames.Select( pluginDllName => allDllFileList.FirstOrDefault(d => Path.GetFileNameWithoutExtension(d) == pluginDllName)) .Where(file => file != null)); //否则将插件文件名称拼接为完整的文件路径 } { foreach (var file in dllPluginFils) { Assembly assembly; try { var name = AppDomain.CurrentDomain.GetAssemblies() .FirstOrDefault(d2 => d2.GetName().Name == Path.GetFileNameWithoutExtension(file)); if (name != null) { assembly = name; } else { assembly = Assembly.LoadFrom(file); } } catch (Exception ex) { XLogSys.Print.Error(ex); continue; } try { types = assembly.GetTypes(); } catch (Exception ex) { XLogSys.Print.Error(ex); continue; } if (types == null) { continue; } foreach (var type in types) { if (type.IsInterface) { continue; } foreach (var interfacename in loader.PluginNames) //对该Type,依次查看是否实现了插件名称列表中的接口 { var interfaceType = type.GetInterface(interfacename.Name); if (interfaceType != null) { var interfaceName = interfacename.Name; // Iterate through all the Attributes for each method. try { foreach (Attribute attr in type.GetCustomAttributes(typeof(XFrmWorkAttribute), false)) //获取该插件的XFrmWorkAttribute标识 { var attr2 = attr as XFrmWorkAttribute; attr2.MyType = type; //将其类型赋值给XFrmWorkAttribute List <XFrmWorkAttribute> pluginInfo = null; //保存到插件字典当中 if (PluginDictionary.TryGetValue(interfaceType, out pluginInfo)) { pluginInfo.Add(attr2); //若插件字典中已包含了该interfaceType的键,则直接添加 } else { var collection = new List <XFrmWorkAttribute> { attr2 }; lock (Lockobj) { if (!PluginDictionary.ContainsKey(interfaceType)) { PluginDictionary.Add(interfaceType, collection); //否则新建一项并添加之 } } } var file2 = Path.GetFileNameWithoutExtension(file); if (!loader.PluginDllNames.Contains(file2)) //若插件文件列表中不包含此文件则添加到文件目录中 { loader.PluginDllNames.Add(file2); } } } catch (Exception ex) { ex.HelpLink = type.ToString(); throw ex; } } } } } #endregion } var allTypes = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()); var dicts = loader.PluginNames.ToList(); //针对dll搜索策略的插件 if (!dicts.Any()) { return; } foreach (var type in allTypes) { // string[] interfaces = type.GetInterfaces().Select(d => d.Name).ToArray(); if (type.IsAbstract || type.IsInterface) { continue; } foreach (var item in dicts) { List <XFrmWorkAttribute> attrs = null; var iType = type.GetInterface(item.Name); if (iType == null) { continue; } var ignore = AttributeHelper.GetAttributes <XFrmWorkIgnore>(type); if (ignore.Any()) { return; //如果发现被标记忽略且名字一致的插件,则忽略它 } if (PluginDictionary.TryGetValue(iType, out attrs)) { if (attrs.FirstOrDefault(d => d.MyType == type) == null) { attrs.Add(getXFrmWorkAttribute(type, iType)); } } else { attrs = new List <XFrmWorkAttribute> { getXFrmWorkAttribute(type, iType) }; PluginDictionary.Add(iType, attrs); } } } }
public EntityResult <TModel> GetModelFromPost <TModel>() where TModel : class, new() { Type objectType = typeof(TModel); var propertySetters = ExpressionReflector.GetSetters(objectType); EntityResult <TModel> result = new EntityResult <TModel>(); var values = ExpressionReflector.GetProperties(objectType); NameValueCollection form = HttpContext.Current.Request.Form; TModel local = Activator.CreateInstance <TModel>(); foreach (PropertyInfo info in values.Values) { object obj2 = null; string str = info.Name.ToLower(); Type underlyingType = Nullable.GetUnderlyingType(info.PropertyType); ValidationAttribute[] attributes = AttributeHelper.GetAttributes <ValidationAttribute>(info); if (form.AllKeys.Contains <string>(str, StringComparer.InvariantCultureIgnoreCase)) { string str2 = form[str]; try { if (underlyingType != null) { obj2 = Convert.ChangeType(str2, underlyingType); } else { obj2 = Convert.ChangeType(str2, info.PropertyType); } } catch (FormatException) { } } else if (underlyingType == null) { //不是可空类型,必须要有一个值,所以应该提示一个错误 var requiredAttr = attributes.FirstOrDefault(x => x is RequiredAttribute); if (requiredAttr == null || string.IsNullOrWhiteSpace(requiredAttr.ErrorMessage)) { result.Message = "表单项 " + info.Name + " 验证失败"; } else { result.Message = requiredAttr.ErrorMessage; } return(result); } foreach (ValidationAttribute attribute in attributes) { if (!((attribute == null) || attribute.IsValid(obj2))) { result.Message = string.IsNullOrEmpty(attribute.ErrorMessage) ? ("表单项 " + info.Name + " 验证失败") : attribute.ErrorMessage; return(result); } } if ((obj2 == null) && (underlyingType == null)) { try { obj2 = Activator.CreateInstance(info.PropertyType); } catch (MissingMethodException) { obj2 = null; } } propertySetters[info.Name](local, obj2); } result.Model = local; result.Success = true; return(result); }
public static Utils.Entity.EntityResult <TModel> Validate <TModel>(Form form) where TModel : class, new() { var controls = form.GetControls(); EntityResult <TModel> er = new EntityResult <TModel>(); var pis = ExpressionReflector.GetProperties(typeof(TModel)); TModel model = new TModel(); foreach (Control item in controls.Values) { if (item is TextBox) { var prefix = GetControlPrefix <TextBox>(); var propertyPair = pis.Where(x => item.Name == prefix + x.Value.Name).FirstOrDefault(); if (string.IsNullOrEmpty(propertyPair.Key)) { continue; } var propertyInfo = propertyPair.Value; Type propertyType = Nullable.GetUnderlyingType(propertyInfo.PropertyType); if (propertyType == null) { propertyType = propertyInfo.PropertyType; } object value = item.Text; value = Convert.ChangeType(value, propertyType); var attrs = AttributeHelper.GetAttributes <ValidationAttribute>(propertyInfo); foreach (var attr in attrs) { if (attr != null && !attr.IsValid(value)) { er.Message = string.IsNullOrEmpty(attr.ErrorMessage) ? ("表单项" + item.Name + "验证失败") : attr.ErrorMessage; return(er); } } ExpressionReflector.SetValue(model, propertyPair.Key, value); } else if (item is NumericUpDown) { var prefix = GetControlPrefix <NumericUpDown>(); var propertyPair = pis.Where(x => item.Name == prefix + x.Value.Name).FirstOrDefault(); if (string.IsNullOrEmpty(propertyPair.Key)) { continue; } var propertyInfo = propertyPair.Value; Type propertyType = Nullable.GetUnderlyingType(propertyInfo.PropertyType); if (propertyType == null) { propertyType = propertyInfo.PropertyType; } object value = item.Text; value = Convert.ChangeType(value, propertyType); var attrs = AttributeHelper.GetAttributes <ValidationAttribute>(propertyInfo); foreach (var attr in attrs) { if (attr != null && !attr.IsValid(value)) { er.Message = string.IsNullOrEmpty(attr.ErrorMessage) ? ("表单项" + item.Name + "验证失败") : attr.ErrorMessage; return(er); } } ExpressionReflector.SetValue(model, propertyPair.Key, value); } else if (item is ComboBox) { var prefix = GetControlPrefix <ComboBox>(); var propertyPair = pis.Where(x => item.Name == prefix + x.Value.Name).FirstOrDefault(); if (string.IsNullOrEmpty(propertyPair.Key)) { continue; } var propertyInfo = propertyPair.Value; Type propertyType = Nullable.GetUnderlyingType(propertyInfo.PropertyType); if (propertyType == null) { propertyType = propertyInfo.PropertyType; } object value = item.Text; value = Convert.ChangeType(value, propertyType); var attrs = AttributeHelper.GetAttributes <ValidationAttribute>(propertyInfo); foreach (var attr in attrs) { if (attr != null && !attr.IsValid(value)) { er.Message = string.IsNullOrEmpty(attr.ErrorMessage) ? ("表单项" + item.Name + "验证失败") : attr.ErrorMessage; return(er); } } ExpressionReflector.SetValue(model, propertyPair.Key, value); } } er.Model = model; er.Success = true; return(er); }