/// <summary> /// /// </summary> /// <param name="t"></param> /// <returns></returns> public static string CheckRequiredFields(T t) { string b = string.Empty; var c = t.GetType().GetProperties(); foreach (var i in c) { var p = i.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.RequiredAttribute), false).FirstOrDefault(); if (p == null) { continue; } var AttrDisPlayName = i.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), false); System.ComponentModel.DisplayNameAttribute DisPlayName = AttrDisPlayName.FirstOrDefault() as System.ComponentModel.DisplayNameAttribute; var v = i.GetValue(t, null); if (v == null || string.IsNullOrEmpty(v.ToString())) { if (DisPlayName == null) { return(i.Name); } return(DisPlayName.DisplayName); } } return(b); }
/// <summary> /// 如果没有DisplayName则返回 null /// </summary> /// <param name="info"></param> /// <returns></returns> public static string GetDisplayName(System.Reflection.PropertyInfo info) { object[] _DisplayList = info.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true); if (_DisplayList.Length == 0) { return(null); } System.ComponentModel.DisplayNameAttribute _Display = (System.ComponentModel.DisplayNameAttribute)_DisplayList[0]; return(_Display.DisplayName); }
public static DataTable ToDataTable <TResult>(this IEnumerable <TResult> ListValue, bool useDisplayName = false) where TResult : class, new() { //建立一個回傳用的 DataTable DataTable dt = new DataTable(); //取得映射型別 Type type = typeof(TResult); //宣告一個 PropertyInfo 陣列,來接取 Type 所有的共用屬性 PropertyInfo[] PI_List = null; System.ComponentModel.DisplayNameAttribute displayName = null; foreach (var item in ListValue) { //判斷 DataTable 是否已經定義欄位名稱與型態 if (dt.Columns.Count == 0) { //取得 Type 所有的共用屬性 PI_List = item.GetType().GetProperties(); //將 List 中的 名稱 與 型別,定義 DataTable 中的欄位 名稱 與 型別 foreach (var item1 in PI_List) { Type t = Nullable.GetUnderlyingType(item1.PropertyType) ?? item1.PropertyType; if (useDisplayName) { displayName = item1.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true).FirstOrDefault() as System.ComponentModel.DisplayNameAttribute; } dt.Columns.Add(displayName != null ? displayName.DisplayName : item1.Name, t); } } //在 DataTable 中建立一個新的列 DataRow dr = dt.NewRow(); //將資料足筆新增到 DataTable 中 foreach (var item2 in PI_List) { if (useDisplayName) { displayName = item2.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true).FirstOrDefault() as System.ComponentModel.DisplayNameAttribute; } dr[displayName != null ? displayName.DisplayName : item2.Name] = item2.GetValue(item, null) == null ? DBNull.Value : item2.GetValue(item, null); } dt.Rows.Add(dr); } dt.AcceptChanges(); return(dt); }
static void WriteDocs() { FileStream file = new FileStream("docs.html", FileMode.Create); StreamWriter docs = new StreamWriter(file); Action <StreamWriter, string, string, string> WriteTag = (StreamWriter S, string Tab, string Tag, string P) => S.WriteLine(Tab + "<" + Tag + ">" + P + "</" + Tag + ">"); docs.WriteLine("<section id=\"components\">"); docs.WriteLine("\t<h3>Components</h3>"); docs.WriteLine("\t<p>This section describes the operation of the basic component types provided in LiveSPICE. All of the components in the library are directly or indirectly (via subcircuits) implemented using these component types.</p>"); Type root = typeof(Circuit.Component); foreach (Assembly i in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type j in i.GetTypes().Where(j => j.IsPublic && !j.IsAbstract && root.IsAssignableFrom(j))) { try { System.ComponentModel.DisplayNameAttribute name = j.CustomAttribute <System.ComponentModel.DisplayNameAttribute>(); System.ComponentModel.DescriptionAttribute desc = j.CustomAttribute <System.ComponentModel.DescriptionAttribute>(); docs.WriteLine("\t<section id=\"" + j.Name + "\">"); docs.WriteLine("\t<h4>" + (name != null ? name.DisplayName : j.Name) + "</h4>"); if (desc != null) { WriteTag(docs, "\t\t", "p", desc.Description); } docs.WriteLine("\t\t<h5>Properties</h5>"); docs.WriteLine("\t\t<ul>"); foreach (PropertyInfo p in j.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(k => k.CustomAttribute <Serialize>() != null)) { desc = p.CustomAttribute <System.ComponentModel.DescriptionAttribute>(); StringBuilder prop = new StringBuilder(); prop.Append("<span class=\"property\">" + p.Name + "</span>"); if (desc != null) { prop.Append(": " + desc.Description); } WriteTag(docs, "\t\t\t", "li", prop.ToString()); } docs.WriteLine("\t\t</ul>"); } catch (Exception) { } } } docs.WriteLine("</section> <!-- components -->"); }
static JsonDatabase() { _PlayerFields = new Dictionary <String, System.Reflection.PropertyInfo>(); _ItemFields = new Dictionary <String, System.Reflection.PropertyInfo>(); foreach (System.Reflection.PropertyInfo prop in typeof(DBPlayer).GetProperties().Where(p => p.CanWrite && p.CanRead)) { System.ComponentModel.BrowsableAttribute browsableAttribute = (System.ComponentModel.BrowsableAttribute)prop.GetCustomAttributes(typeof(System.ComponentModel.BrowsableAttribute), true).FirstOrDefault(); if (browsableAttribute != null && !browsableAttribute.Browsable) { continue; } String keyName = prop.Name; System.ComponentModel.DisplayNameAttribute displayNameAttribute = (System.ComponentModel.DisplayNameAttribute)prop.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true).FirstOrDefault(); if (displayNameAttribute != null && !String.IsNullOrEmpty(displayNameAttribute.DisplayName)) { keyName = displayNameAttribute.DisplayName; } _PlayerFields[keyName] = prop; } foreach (System.Reflection.PropertyInfo prop in typeof(DBItem).GetProperties().Where(p => p.CanWrite && p.CanRead)) { System.ComponentModel.BrowsableAttribute browsableAttribute = (System.ComponentModel.BrowsableAttribute)prop.GetCustomAttributes(typeof(System.ComponentModel.BrowsableAttribute), true).FirstOrDefault(); if (browsableAttribute != null && !browsableAttribute.Browsable) { continue; } String keyName = prop.Name; System.ComponentModel.DisplayNameAttribute displayNameAttribute = (System.ComponentModel.DisplayNameAttribute)prop.GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), true).FirstOrDefault(); if (displayNameAttribute != null && !String.IsNullOrEmpty(displayNameAttribute.DisplayName)) { keyName = displayNameAttribute.DisplayName; } _ItemFields[keyName] = prop; } }
public static DataTable ListToDataTableByModelDisplayName <T>(List <T> list) { DataTable dt = new DataTable(); object[] objs = null; string displayName = string.Empty; foreach (PropertyInfo info in typeof(T).GetProperties()) { objs = info.GetCustomAttributes(true); displayName = string.Empty; foreach (object item in objs) { if (item is System.ComponentModel.DisplayNameAttribute) { System.ComponentModel.DisplayNameAttribute displayNameAttr = (System.ComponentModel.DisplayNameAttribute)item; if (displayNameAttr != null) { displayName = displayNameAttr.DisplayName; break; } } } dt.Columns.Add(new DataColumn(string.IsNullOrEmpty(displayName) == true ? info.Name : displayName, GetNullableType(info.PropertyType))); } displayName = string.Empty; foreach (T t in list) { DataRow row = dt.NewRow(); foreach (PropertyInfo info in typeof(T).GetProperties()) { objs = info.GetCustomAttributes(true); displayName = string.Empty; foreach (object item in objs) { if (item is System.ComponentModel.DisplayNameAttribute) { System.ComponentModel.DisplayNameAttribute displayNameAttr = (System.ComponentModel.DisplayNameAttribute)item; if (displayNameAttr != null) { displayName = displayNameAttr.DisplayName; break; } } } if (!IsNullableType(info.PropertyType)) { row[string.IsNullOrEmpty(displayName) == true ? info.Name : displayName] = info.GetValue(t, null); } else { row[string.IsNullOrEmpty(displayName) == true ? info.Name : displayName] = (info.GetValue(t, null) ?? DBNull.Value); } } dt.Rows.Add(row); } return(dt); }
/// <summary> /// Return a List of Variance records representing the differences between two entity objects /// </summary> /// <typeparam name="T">The Entity Type</typeparam> /// <param name="sourceObject">The Source object</param> /// <param name="targetObject">The Taget object</param> /// <param name="ignoreID">set to true to ensure the ID property is not compared</param> /// <returns>Lists of Variance records <see cref="List{T}"/> <seealso cref="Variance"/></returns> public static List <Variance> DetailedCompare <T>(this T sourceObject, T targetObject, bool ignoreID = false) { try { List <Variance> variances = new List <Variance>(); if (sourceObject != null && targetObject != null) { var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var property in properties) { string propertyName = property.Name; bool isEditable = true; // used to skip properties var ComputedATT = property.GetCustomAttribute(typeof(Dapper.Contrib.Extensions.ComputedAttribute)); var KeyAtt = property.GetCustomAttribute(typeof(Dapper.Contrib.Extensions.KeyAttribute)); var EditableATT = property.GetCustomAttribute(typeof(EditableAttribute)); if (EditableATT != null) { isEditable = property.GetCustomAttribute <EditableAttribute>().AllowEdit; } if (ComputedATT != null) { isEditable = false; } if (isEditable) { if (property.Name.ToUpper() == "ID" && ignoreID) { continue; } string category = "Misc"; System.ComponentModel.AttributeCollection attributes = System.ComponentModel.TypeDescriptor.GetProperties(sourceObject)[property.Name].Attributes; System.ComponentModel.CategoryAttribute propertyCategory = (System.ComponentModel.CategoryAttribute)attributes[typeof(System.ComponentModel.CategoryAttribute)]; System.ComponentModel.DisplayNameAttribute propertyDisplayName = (System.ComponentModel.DisplayNameAttribute)attributes[typeof(System.ComponentModel.DisplayNameAttribute)]; System.ComponentModel.DescriptionAttribute propertyDescription = (System.ComponentModel.DescriptionAttribute)attributes[typeof(System.ComponentModel.DescriptionAttribute)]; category = propertyCategory.Category; if (property.PropertyType.IsClass) { category = "Class"; } var v = new Variance { PropertyName = property.Name, PropertyType = property.PropertyType, PropertyCategory = propertyCategory.Category, PropertyDescription = propertyDescription.Description, PropertyDisplayName = (!string.IsNullOrEmpty(propertyDisplayName.DisplayName) ? propertyDisplayName.DisplayName : property.Name), OldValue = property.GetValue(sourceObject), NewValue = property.GetValue(targetObject) }; if (v.OldValue == null && v.NewValue == null) { continue; } if ((v.OldValue == null && v.NewValue != null) || (v.OldValue != null && v.NewValue == null)) { variances.Add(v); continue; } if (!v.OldValue.Equals(v.NewValue)) { variances.Add(v); } if (v.OldValue.Equals(v.NewValue) && KeyAtt != null) { v.isKeyField = true; variances.Add(v); } } } } return(variances); } catch (System.Exception) { throw; } }