public bool CheckAssembly() { //Try to load target DLL var assembly = typeof(MicroDBHelpers.MicroDBTransaction).Assembly;; //Check GUID var guidAttribute = LinqSearchAlternate.FirstOrDefault(assembly.GetCustomAttributes(typeof(GuidAttribute), true)) as GuidAttribute; if (guidAttribute == null) { return(false); } if (guidAttribute.Value.Equals("1fc8371f-18f4-4693-b233-a8736f9cded7", StringComparison.OrdinalIgnoreCase) == false) { return(false); } //Check PublicKeyToken var publicKeyToken = BitConverter.ToString(assembly.GetName().GetPublicKeyToken()).Replace("-", ""); if (publicKeyToken.Equals("68750db11b24ebb2", StringComparison.OrdinalIgnoreCase) == false) { return(false); } //Finally return(true); }
/// <summary> /// Check [MicroDBHelper.dll] is can be loaded successfully /// </summary> /// <returns></returns> public static bool CheckAssembly() { #region Solution1. Well in Winform, but Bad in Asp.net /* * try * { * var assmblyLoaderType = typeof(AssemblyInspectorContext); * var assemblyLoader = (IAssemblyInspectorContext)appDomain.CreateInstanceFromAndUnwrap(assmblyLoaderType.Assembly.Location, assmblyLoaderType.FullName); * return assemblyLoader.CheckAssembly(); * } * catch (Exception) * { * return false; * } * finally * { * //Release the temporary AppDomain * AppDomain.Unload(appDomain); * } */ #endregion #region Solution2 try { var assembly = Assembly.Load("MicroDBHelper"); //Check GUID var guidAttribute = LinqSearchAlternate.FirstOrDefault(assembly.GetCustomAttributes(typeof(GuidAttribute), true)) as GuidAttribute; if (guidAttribute == null) { return(false); } if (guidAttribute.Value.Equals("1fc8371f-18f4-4693-b233-a8736f9cded7", StringComparison.OrdinalIgnoreCase) == false) { return(false); } //Check PublicKeyToken var publicKeyToken = BitConverter.ToString(assembly.GetName().GetPublicKeyToken()).Replace("-", ""); if (publicKeyToken.Equals("68750db11b24ebb2", StringComparison.OrdinalIgnoreCase) == false) { return(false); } //Finally return(true); } catch (Exception) { return(false); } #endregion }
/// <summary> /// Convert the "DataTable" object to the "Entity List" with target Type /// </summary> /// <typeparam name="T">target type</typeparam> /// <param name="dt">DateTable object</param> /// <returns>a list of result</returns> public static IList <T> ToList <T>(this DataTable dt) where T : class { var list = new List <T>(); //check is it NULL if (dt == null) { return(list); } //get Properties var plist = new List <PropertyInfo>(typeof(T).GetProperties()); //get dic<Propertie,ColumnAttribute> var pdic_col_raw = LinqSearchAlternate.Select(plist, (o => new InformationForPropertyInfo { Properties = o, ColumnAttribute = Attribute.GetCustomAttribute(o, typeof(ColumnAttribute)) as ColumnAttribute })); var pdic_col = LinqSearchAlternate.ToDictionary(pdic_col_raw, (o => o.Properties)); //get dic<Propertie,IgnoreAttribute> var pdic_Ign_raw = LinqSearchAlternate.Select(plist, (o => new { Properties = o, IgnoreAttribute = Attribute.GetCustomAttribute(o, typeof(IgnoreAttribute)) as IgnoreAttribute })); var pdic_Ign = LinqSearchAlternate.ToDictionary(pdic_Ign_raw, (o => o.Properties)); //loop to convert Properties and Values foreach (DataRow item in dt.Rows) { try { T current = Activator.CreateInstance <T>(); for (int i = 0; i < dt.Columns.Count; i++) { PropertyInfo info = null; #region Get Target Property /* You can overwrite this folded code, in order to simply combine your logic. * For example, just use: * info = plist.Find(p => p.Name == dt.Columns[i].ColumnName); */ //Richer logic : info = LinqSearchAlternate.FirstOrDefault(plist, (o => { var colAtt = pdic_col[o].ColumnAttribute; if (colAtt != null) { /* own ColumnAttribute, then use the rule from ColumnAttribute */ bool ignoreCase = !colAtt.CaseSensitiveToMatchedName; return(String.Compare(dt.Columns[i].ColumnName, colAtt.MatchedName, ignoreCase) == 0); } else { /* Not own ColumnAttribute, then check "CaseSensitive" rule from EntityConversionDefaultSettings and "MatchedName" from "Same as DataColumn::ColumnName" */ bool ignoreCase = !EntityConversionDefaultSettings.CaseSensitiveToColumnName; return(String.Compare(dt.Columns[i].ColumnName, o.Name, ignoreCase) == 0); } })); #endregion try { if (info != null && info.CanWrite) { #region Ignore this column Or Not if (pdic_Ign[info].IgnoreAttribute != null) { continue; } #endregion object rawValue = item[i]; //Set Value if (rawValue != null && !Convert.IsDBNull(rawValue)) { //Support Nullable Type Type safeType = Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType; //Support Enum: if (safeType.IsEnum) { info.SetValue(current, Enum.Parse(safeType, rawValue.ToString()), null); continue; } //Normal: info.SetValue(current, Convert.ChangeType(rawValue, safeType), null); } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(String.Format("## [MicroDBHelperExpansionPack.EntityConversion] System.Data.ADOExtensions::ToList<T> found an exception when convert [{0}] property, message is {1} ", info.Name, ex.Message)); continue; } } list.Add(current); } catch (Exception) { continue; } } //return all result return(list); }