/// <summary> /// 添加注册组件 /// </summary> /// <param name="componentPairs"></param> public static void AddComponentPairs(List <ComponentPair> componentPairs) { ComponentPairs.AddRange(componentPairs); PropComponentPairs.AddRange(componentPairs.Where(pair => pair.ComponentType.HasImplementedRawGeneric(typeof(PropertyComponentBase <>)) && !pair.ComponentType.IsAbstract).ToList()); FieldComponentPairs.AddRange(componentPairs.Where(pair => pair.ComponentType.HasImplementedRawGeneric(typeof(FieldComponentBase <>)) && !pair.ComponentType.IsAbstract).ToList()); ViewComponentPairs.AddRange(componentPairs.Where(pair => pair.ComponentType.HasImplementedRawGeneric(typeof(ModelComponentBase <>)) && !pair.ComponentType.IsAbstract).ToList()); }
public static Type GetPropComponentTypeByProperty <TModel>(PropertyInfo property) { var propertyType = property.PropertyType; Type componentType = null; if (propertyType.IsGenericType) { var genericTypeDefinition = propertyType.GetGenericTypeDefinition(); // nullable类型 if (genericTypeDefinition == typeof(Nullable <>)) { var type = propertyType.GetGenericArguments()[0]; componentType = PropComponentPairs.Where(pair => pair.DataType == type.FullName).FirstOrDefault().ComponentType; } // list 类型 if (genericTypeDefinition == typeof(List <object>).GetGenericTypeDefinition()) { var propAttribute = property.GetCustomAttribute <PropAttribute>(); Console.WriteLine("list:" + propAttribute.ComponentType); try { componentType = PropComponentPairs.Where(pair => pair.ComponentType.FullName.Contains(propAttribute.ComponentType)).FirstOrDefault()?.ComponentType; Console.WriteLine("get type:" + componentType); } catch (Exception e) { Console.WriteLine("====" + e); } } if (propertyType.IsEnum) { componentType = PropComponentPairs.Where(pair => pair.DataType == typeof(Enum).FullName).FirstOrDefault().ComponentType; } } else { // Console.WriteLine("system basic data type:" + propertyType.FullName); var pair = PropComponentPairs.Where(pair => pair.DataType == propertyType.FullName).FirstOrDefault(); if (pair != null) { componentType = pair.ComponentType; } } if (componentType == null) { Console.WriteLine("error: not found component for property type:" + propertyType + " prop component count:" + PropComponentPairs.Count); return(null); } if (componentType.IsGenericType) { componentType = componentType.MakeGenericType(typeof(TModel)); } return(componentType); }
public static Type GetFieldComponentTypeByProperty <TModel>(PropertyInfo property) { var propertyType = property.PropertyType; Type componentType = null; var fieldAttribute = property.GetCustomAttribute <FormFieldAttribute>(); if (propertyType.IsGenericType) { var genericTypeDefinition = propertyType.GetGenericTypeDefinition(); // nullable类型 if (genericTypeDefinition == typeof(Nullable <>)) { var type = propertyType.GetGenericArguments()[0]; Console.WriteLine("is nullable" + type.FullName); componentType = FieldComponentPairs.Where(pair => pair.DataType == type.FullName).FirstOrDefault().ComponentType; } // list 类型 if (genericTypeDefinition == typeof(List <object>).GetGenericTypeDefinition()) { try { componentType = PropComponentPairs.Where(pair => pair.ComponentType.FullName.Contains(fieldAttribute.ComponentType)).FirstOrDefault()?.ComponentType; Console.WriteLine("get type:" + componentType); } catch (Exception e) { Console.WriteLine("====" + e); } } if (propertyType.IsEnum) { componentType = FieldComponentPairs.Where(pair => pair.DataType == typeof(Enum).FullName).FirstOrDefault().ComponentType; } } else { Console.WriteLine("pair:"); componentType = FieldComponentPairs.Where(pair => pair.DataType == propertyType.FullName).FirstOrDefault()?.ComponentType; } if (fieldAttribute != null) { try { Console.WriteLine("attribute type:" + fieldAttribute.ComponentType); componentType = FieldComponentPairs.Where(pair => pair.ComponentType.FullName.Contains(fieldAttribute.ComponentType)).FirstOrDefault()?.ComponentType; if (componentType != null) { Console.WriteLine("yes found componentType:" + componentType.FullName); } }catch (Exception e) { Console.WriteLine("exception: not found" + e); Console.WriteLine(e); } } if (componentType == null) { Console.WriteLine("error: not found component for field type:" + propertyType); return(null); } if (componentType.IsGenericType) { Console.WriteLine(typeof(TModel).Name); componentType = componentType.MakeGenericType(typeof(TModel)); } return(componentType); }