遍历成员

abc3000 2007-01-18 06:39:49
我要写一个方法

传过来一个对象的实例,可以使任何类

我要遍历这个实例拥有多少成员和属性,并且要知道他们的名字,

请问怎么写这个方法?
...全文
233 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangyj 2007-02-12
  • 打赏
  • 举报
回复
请问如何给各成员进行赋值呢?
huangyj 2007-02-12
  • 打赏
  • 举报
回复
这个确实不错。
abc3000 2007-01-19
  • 打赏
  • 举报
回复
哦 对了 之后再想得到比如snoMemberType的值该怎么写?
abc3000 2007-01-19
  • 打赏
  • 举报
回复
谢谢楼上各位
liujia_0421 2007-01-18
  • 打赏
  • 举报
回复
如果你不想获得从父类即Object继承来的一些方法,而且又想获得类中的私有成员..
可以通过BindingFlags来进行控制...

for example:
class Program
{
static void Main(string[] args)
{
Student st = new Student();
PrintInstanceInfor(st);
}
static void PrintInstanceInfor(object obj)
{
Type t = obj.GetType();
MemberInfo[] myMember = t.GetMembers(BindingFlags .Instance |BindingFlags .DeclaredOnly |BindingFlags .Public |BindingFlags .NonPublic);
foreach (MemberInfo m in myMember)
{
Console.WriteLine("Name: " + m.Name + "\t\tMemberType: " + m.MemberType.ToString());
}
}
}

输出如下:
Name: get_Sno MemberType: Method
Name: set_Sno MemberType: Method
Name: get_Sname MemberType: Method
Name: set_Sname MemberType: Method
Name: ReturnString MemberType: Method
Name: OutputInfor MemberType: Method
Name: .ctor MemberType: Constructor
Name: Sno MemberType: Property
Name: Sname MemberType: Property
Name: sno MemberType: Field
Name: sname MemberType: Field
liujia_0421 2007-01-18
  • 打赏
  • 举报
回复
simple example:

有一个Student类,定义如下:
public class Student
{
private string sno;
private string sname;
public string Sno
{
get { return sno; }
set { this.sno = value; }
}
public string Sname
{
get { return sname; }
set { this.sname = value; }
}
public Student()
{

}
private string ReturnString()
{
return sno + sname;
}
public void OutputInfor()
{
Console.WriteLine("OutputInfor");
}
}

测试:
class Program
{
static void Main(string[] args)
{
Student st = new Student();
PrintInstanceInfor(st);
}
static void PrintInstanceInfor(object obj)
{
Type t = obj.GetType();
MemberInfo[] myMember = t.GetMembers();
foreach (MemberInfo m in myMember)
{
Console.WriteLine("Name: " + m.Name + "\t\tMemberType: " + m.MemberType.ToString());
}
}
}

输出如下:
Name: get_Sno MemberType: Method
Name: set_Sno MemberType: Method
Name: get_Sname MemberType: Method
Name: set_Sname MemberType: Method
Name: OutputInfor MemberType: Method
Name: GetType MemberType: Method
Name: ToString MemberType: Method
Name: Equals MemberType: Method
Name: GetHashCode MemberType: Method
Name: .ctor MemberType: Constructor
Name: Sno MemberType: Property
Name: Sname MemberType: Property
liujia_0421 2007-01-18
  • 打赏
  • 举报
回复
TO:传过来一个对象的实例,可以使任何类

我要遍历这个实例拥有多少成员和属性,并且要知道他们的名字

try..

public void PrintInstanceInfor(object obj)
{
Type t = obj.GetType();
MemberInfo[] myMember = t.GetMembers();
foreach (MemberInfo m in myMember)
{
Console.WriteLine("Name: " + m.Name + "\t\tMemberType: " + m.MemberType.ToString());
}
}
灰太狼 2007-01-18
  • 打赏
  • 举报
回复
用反射,代碼如下:
下面的程序是一个名为ReflectionExample的简单项目,里面包括MainClass.cs和InstanceClass.cs两个文件,MainClass里面利用字符串来创建InstanceClass类并且调用InstanceClass中的属性和方法,其中包括调用缺省构造函数和调用重载构造函数、调用无参数的成员函数和带参数的成员函数四个方法,还有一个ReturnString属性,程序如下。
InstanceClass.cs文件
using System;

namespace ReflectionExample
{
/// <summary>
/// InstanceClass 的摘要描述。
/// </summary>
public class InstanceClass
{
private string returnString;
public string ReturnString
{
get { return returnString ; }
set { returnString = value ;}
}

public InstanceClass()
{
//
// TODO: 在此加入建构函式的程序代码
//
this.returnString = "Creat object without Parameter";
}

public InstanceClass( string str )
{
this.returnString = str;
}

public void FunctionWithoutParameter()
{
Console.WriteLine( "Function Without Parameter" );
}

public void FunctionWithParameter( string str )
{
Console.WriteLine( str );
}
}
}
MainClass.cs文件
using System;
using System.Reflection;

namespace ReflectionExample
{
/// <summary>
/// Class1 的摘要描述。
/// </summary>
class MainClass
{
private Type type = null;
/// <summary>
/// 应用程序的主进入点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此加入启动应用程序的程序代码
//
Type type = typeof(InstanceClass);//利用typeof来获得InstanceClass类型。
MainClass mainClass = new MainClass(type);//初始化MainClass类
mainClass.GetObjectMethod();//调用无参数的函数
mainClass.GetObjectMethod( "Function With Parameter" );//调用带参数的函数
mainClass.GetObjectProperty();//调用缺省构造函数来初始化InstanceClass中的ReturnString属性
mainClass.GetObjectProperty("Creat object with Parameter");//调用重载构造函数来初始化InstanceClass中的ReturnString属性
}

public MainClass( Type type )
{
this.type = type ;
}

public void GetObjectMethod()
{
try
{
//Activator.CreateInstance()调用类的缺省构造函数来创建实例
object o = Activator.CreateInstance( type );
//利用MethodInfo类来获得从指定类中的成员函数
MethodInfo mi = type.GetMethod("FunctionWithoutParameter" );
//调用此成员函数
mi.Invoke( o , null );
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
}
finally
{
Console.ReadLine();
}
}

public void GetObjectMethod( string str )
{
try
{
object o = Activator.CreateInstance( type );
//利用MethodInfo类来获得从指定类中符合条件的成员函数
MethodInfo mi = type.GetMethod( "FunctionWithParameter" , BindingFlags.Public |BindingFlags.Instance , null , new Type[]{ typeof(string) } , null );
mi.Invoke( o , new object[]{ str } );
}
catch( Exception ex )
{
Console.WriteLine( ex.Message );
}
finally
{
Console.ReadLine();
}
}

public void GetObjectProperty( )
{
try
{
object o = Activator.CreateInstance( type );
//利用PropertyInfo类来获得从指定类中的属性
PropertyInfo pi = type.GetProperty( "ReturnString" , typeof(string) );
//打印属性值
Console.WriteLine( pi.GetValue( o , null ) );
}
catch( Exception ex )
{
Console.WriteLine( ex.ToString() );
}
finally
{
Console.ReadLine();
}
}

public void GetObjectProperty( string str )
{
try
{
//Activator.CreateInstance()调用类的重载构造函数来创建实例
object o = Activator.CreateInstance( type , new object[]{ "Creat object with Parameter" } );
PropertyInfo pi = type.GetProperty( "ReturnString" , typeof(string) );
Console.WriteLine( pi.GetValue( o , null ) );
}
catch( Exception ex )
{
Console.WriteLine( ex.ToString() );
}
finally
{
Console.ReadLine();
}
}
}
}
Red_angelX 2007-01-18
  • 打赏
  • 举报
回复
用反射!
ChengKing 2007-01-18
  • 打赏
  • 举报
回复
有现有技术,用.net 反射技术.
abc3000 2007-01-18
  • 打赏
  • 举报
回复
传过来一个对象的实例,可以是任何类

110,561

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧