public static List <ColumnInfo> ColumnInfoFromType(object atype) { if (atype == null) { return(null); } Type t = atype.GetType(); PropertyInfo[] props = t.GetProperties(); List <ColumnInfo> dict = new List <ColumnInfo>(); foreach (PropertyInfo prp in props) { var attributes = prp.GetCustomAttributes(false); var columnattributesa = attributes.Where(a => a.GetType() == typeof(ExcludeAttribute)); var column = columnattributesa.FirstOrDefault(a => a.GetType() == typeof(ExcludeAttribute)); ExcludeAttribute ea = column as ExcludeAttribute; if (ea is null) { ColumnInfo cinfo = new ColumnInfo(); cinfo.ColumnValue = prp.GetValue(atype, new object[] { }); //Column Name var columnattributes = attributes.Where(a => a.GetType() == typeof(DbColumnAttribute)); column = columnattributes.FirstOrDefault(a => a.GetType() == typeof(DbColumnAttribute)); DbColumnAttribute cl = column as DbColumnAttribute; cinfo.ColumnName = cl.Name; //Autoincrument var Attrib = attributes.Where(a => a.GetType() == typeof(AutoIncrementAttribute)); column = Attrib.FirstOrDefault(a => a.GetType() == typeof(AutoIncrementAttribute)); AutoIncrementAttribute ai = column as AutoIncrementAttribute; cinfo.AutoIncrement = (ai is null) ? false : ai.Value; //Primary Key Attrib = attributes.Where(a => a.GetType() == typeof(PrimaryKeyAttribute)); column = Attrib.FirstOrDefault(a => a.GetType() == typeof(PrimaryKeyAttribute)); PrimaryKeyAttribute pk = column as PrimaryKeyAttribute; cinfo.PrimaryKey = (pk is null) ? false : pk.Value; //AllowNullAttribute Attrib = attributes.Where(a => a.GetType() == typeof(AllowNullAttribute)); column = Attrib.FirstOrDefault(a => a.GetType() == typeof(AllowNullAttribute)); AllowNullAttribute an = column as AllowNullAttribute; cinfo.AllowNull = (an is null) ? false : an.Value; //DataTypeAttribute Attrib = attributes.Where(a => a.GetType() == typeof(DataTypeAttribute)); column = Attrib.FirstOrDefault(a => a.GetType() == typeof(DataTypeAttribute)); DataTypeAttribute dt = column as DataTypeAttribute; cinfo.DataType = dt.Value; //DonotSelect Attrib = attributes.Where(a => a.GetType() == typeof(DoNotSelectAttribute)); column = Attrib.FirstOrDefault(a => a.GetType() == typeof(DoNotSelectAttribute)); DoNotSelectAttribute ds = column as DoNotSelectAttribute; cinfo.DoNotSelect = (ds is null) ? false : ds.Value; dict.Add(cinfo); } } return(dict); }
/// <summary> /// Processes the Attribute metadata to generate a CompiledCommandAttribute. /// </summary> /// <exception cref="MetadataException"> /// If the attribute is a parameter attribute and another parameter attribute /// has been processed with the same parameter-set name. /// </exception> private void ProcessAttribute( string memberName, Attribute attribute, ref Collection <ValidateArgumentsAttribute> validationAttributes, ref Collection <ArgumentTransformationAttribute> argTransformationAttributes, ref string[] aliases) { if (attribute == null) { return; } CompiledAttributes.Add(attribute); // Now process the attribute based on it's type if (attribute is ParameterAttribute paramAttr) { ProcessParameterAttribute(memberName, paramAttr); return; } ValidateArgumentsAttribute validateAttr = attribute as ValidateArgumentsAttribute; if (validateAttr != null) { if (validationAttributes == null) { validationAttributes = new Collection <ValidateArgumentsAttribute>(); } validationAttributes.Add(validateAttr); if ((attribute is ValidateNotNullAttribute) || (attribute is ValidateNotNullOrEmptyAttribute)) { this.CannotBeNull = true; } return; } AliasAttribute aliasAttr = attribute as AliasAttribute; if (aliasAttr != null) { if (aliases == null) { aliases = aliasAttr.aliasNames; } else { var prevAliasNames = aliases; var newAliasNames = aliasAttr.aliasNames; aliases = new string[prevAliasNames.Length + newAliasNames.Length]; Array.Copy(prevAliasNames, aliases, prevAliasNames.Length); Array.Copy(newAliasNames, 0, aliases, prevAliasNames.Length, newAliasNames.Length); } return; } ArgumentTransformationAttribute argumentAttr = attribute as ArgumentTransformationAttribute; if (argumentAttr != null) { if (argTransformationAttributes == null) { argTransformationAttributes = new Collection <ArgumentTransformationAttribute>(); } argTransformationAttributes.Add(argumentAttr); return; } AllowNullAttribute allowNullAttribute = attribute as AllowNullAttribute; if (allowNullAttribute != null) { this.AllowsNullArgument = true; return; } AllowEmptyStringAttribute allowEmptyStringAttribute = attribute as AllowEmptyStringAttribute; if (allowEmptyStringAttribute != null) { this.AllowsEmptyStringArgument = true; return; } AllowEmptyCollectionAttribute allowEmptyCollectionAttribute = attribute as AllowEmptyCollectionAttribute; if (allowEmptyCollectionAttribute != null) { this.AllowsEmptyCollectionArgument = true; return; } ObsoleteAttribute obsoleteAttr = attribute as ObsoleteAttribute; if (obsoleteAttr != null) { ObsoleteAttribute = obsoleteAttr; return; } PSTypeNameAttribute psTypeNameAttribute = attribute as PSTypeNameAttribute; if (psTypeNameAttribute != null) { this.PSTypeName = psTypeNameAttribute.PSTypeName; } }