用反射来做的时候好像只能对属性或方法取到它们的类型,对于一个不是属性,或方法的一个公有成员是不好取它的类型的,起码我不知道,所以我建议把你的程序改一下,把那几个
public static a a1 = new a("a1");
public static a a2 = new a("a2");
public static a a3 = new a("a3");
改成属性的声明形式,代码如下可以实现:using System;namespace test
{
class a
{
public string Name; public a(string name)
{
this.Name = name;
}
} class b
{
private static a a1 = new a("a1");
private static a a2 = new a("a2");
private static a a3 = new a("a3"); public static a A1
{
get
{
return a1;
}
set 
{
a1 = value;
}
}
public static a A2
{
get
{
return a2;
}
set
{
a2 = value;
}
}
public static a A3
{
get
{
return a3;
}
set
{
a3 = value;
}
}
public static a SomeMethod()
{
return new a("a");
}
} //我怎么利用反射来取得b中a类型数据的Name值列表。大概就是如下面这个方法 class c
{
/// <summary>
/// 得到b中a类型的属性的属性名称
/// </summary>
/// <returns></returns>
public string GetNameList()
{
string list = "";
//System.Reflection.MethodInfo[ ] s = typeof(b).GetMethods();
System.Reflection.PropertyInfo[ ] s = typeof(b).GetProperties();
for(int i = 0; i < s.Length; i ++)
{
//string t1 = s[i].ReturnType.Name;
string t1 = s[i].PropertyType.Name;
string t2 = typeof(a).Name;
if (t1==t2)
{
list = list + strTypeName = s[i].Name;
System.Console.WriteLine(strTypeName);
}
}
return list;
}
}
}

解决方案 »

  1.   

    方法改一下:
    public string GetNameList()
    {
    string list = "";
    //System.Reflection.MethodInfo[ ] s = typeof(b).GetMethods();
    System.Reflection.PropertyInfo[ ] s = typeof(b).GetProperties();
    for(int i = 0; i < s.Length; i ++)
    {
    //string t1 = s[i].ReturnType.Name;
    string t1 = s[i].PropertyType.Name;
    string t2 = typeof(a).Name;
    if (t1==t2)
    {
    string strTypeName = null;
    strTypeName = s[i].Name; list = list + strTypeName;
    System.Console.WriteLine(strTypeName);
    }
    }
    return list;
    }