日期:2014-05-18  浏览次数:20933 次

c#反射如何获取一个类的基本信息如PublicKeyToken等
我想通过反射获取一个类的type,Version,Culture,PublicKeyToken。请问该怎么做?

------解决方案--------------------
C# code
        /// <summary>
        /// 返回IList数据集
        /// </summary>
        /// <param name="startIndex">开始页数</param>
        /// <param name="endIndex">结束页数</param>
        /// <returns>实体类集合</returns>
        public IList<T> GetData(int startIndex, int endIndex)
        {
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            SqlParameter[] parameters = SetParameter(false);

            IList<T> list = new List<T>();
            Type type = typeof(T);
            PropertyInfo[] properties = type.GetProperties();
            using (SqlDataReader sdr = SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.StoredProcedure, "KFS_Pagination", parameters))
            {
                while (sdr.Read())
                {
                    T t = Activator.CreateInstance<T>();
                    for (int i = 0; i < properties.Length; i++)
                    {
                        properties[i].SetValue(t, sdr[i + 1], null);

                    }

                    list.Add(t);
                }
            }
            return list;
        }

------解决方案--------------------
探讨
C# code///<summary>/// 返回IList数据集///</summary>///<param name="startIndex">开始页数</param>///<param name="endIndex">结束页数</param>///<returns>实体类集合</returns>public IList<T> GetData(int startIndex,int endIndex)
{this.startIndex= startIndex;this.endIndex= endIndex;
SqlParameter[] parameters= SetParameter(false);

IList<T> list=new List<T>();
Type type=typeof(T);
PropertyInfo[] properties= type.GetProperties();using (SqlDataReader sdr= SqlHelper.ExecuteReader(SqlHelper.connectionString, CommandType.StoredProcedure,"KFS_Pagination", parameters))
{while (sdr.Read())
{
T t= Activator.CreateInstance<T>();for (int i=0; i< properties.Length; i++)
{
properties[i].SetValue(t, sdr[i+1],null);

}

list.Add(t);
}
}return list;
}

------解决方案--------------------
int i = 42;
System.Type type = i.GetType();
System.Console.WriteLine(type);

System.Reflection.Assembly o = System.Reflection.Assembly.Load("A.dll");
System.Console.WriteLine(o.GetName());


------解决方案--------------------
C# code

            Type t = typeof(int);
            Console.WriteLine(t.FullName);
            Console.WriteLine(t.Assembly.FullName);