如题,怎么在静态方法中得到当前类的Type。由于是静态方法,所以不能使用this关键字。
楼上的是对的.
可以在静态类(如Test)里的静态方法(FuncOne)里使用如下.
System.Reflection.MemberInfo[] infos = typeof(Test).GetMember(name);
我也曾经这么用过,如下.
http://www.cnblogs.com/zhouxin/archive/2009/12/04/1617036.html
public class DataOperate<T> where T : new()
{
private static readonly List<PropertyInfo> ps = null;
private static readonly List<Info> infos = null;
private static readonly PropertyInfo time_Info = null;
private static readonly DateTime time = DateTime.Now;
private const int startIndex = 9;
static DataOperate()
{
if (ps == null)
{
infos = new List<Info>();
ps = typeof(T).GetProperties().ToList();
foreach (PropertyInfo item in ps)
{
if (item.ToString().Contains("String"))
{
Info info = new Info();
ColumnAttribute a = Attribute.GetCustomAttribute(item, typeof(ColumnAttribute)) as ColumnAttribute;
string length = a.DbType.Substring(startIndex, a.DbType.IndexOf(')') - startIndex);
int len = 0;
if (int.TryParse(length, out len))
{
info.Length = len;
info.ProInfo = item;
infos.Add(info);
}
}
else if(item.ToString().Contains("DateTime"))
{
time_Info = item;
}
}
}
}