Пример #1
0
    /// <summary>
    /// Gets a comma separated string with the column names. Meant to build queries.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public static string GetColumnNames(Type type)
    {
        //getting properties in a specific order
        Dictionary <string, string> Mapping = new Dictionary <string, string>();
        TypeInfo            typeInfo        = type.GetTypeInfo();
        List <PropertyInfo> properties      = new List <PropertyInfo>();
        PropertyInfo        idPropertyInfo  = typeInfo.GetProperty("Id");

        if (idPropertyInfo != null)
        {
            properties.Add(typeInfo.GetProperty("Id"));                          //TODO: this is because Join reader always assume Id comes first
        }
        properties.AddRange(typeInfo.GetProperties().Where(p => p.Name != "Id" && !p.CustomAttributes.Any(x => x.AttributeType == typeof(NotMappedAttribute))).OrderBy(p => p.Name).ToList());
        foreach (PropertyInfo p in properties)
        {
            ColumnNameAttribute cma = p.GetCustomAttribute <ColumnNameAttribute>();
            TableAliasAttribute taa = p.GetCustomAttribute <TableAliasAttribute>();
            if (taa == null)
            {
                throw new Exception("Table alias must be defined for each mapped attribute.");
            }
            Mapping.Add(p.Name, cma != null ? $"{taa.Alias}.{cma.ColumnName}" : $"{taa.Alias}.{p.Name}");
        }
        return(String.Join(", ", Mapping.Values));
    }
Пример #2
0
        /// <summary>
        /// 获取T的类名
        /// </summary>
        /// <returns></returns>
        private string getEntityName()
        {
            Type   type                    = typeof(T);
            string name                    = type.Name;         //获取当前成员的名称
            string fullName                = type.FullName;     //获取类的全部名称不包括程序集
            string nameSpace               = type.Namespace;    //获取该类的命名空间
            var    assembly                = type.Assembly;     //获取该类的程序集名
            var    module                  = type.Module;       //获取该类型的模块名
            var    memberInfos             = type.GetMembers(); //得到所有公共成员
            TableAliasAttribute tableAlias = (TableAliasAttribute)type.GetCustomAttribute(typeof(TableAliasAttribute), true);

            if (tableAlias != null)
            {
                return(tableAlias.Alias);
            }
            return(name.ToLower());
        }