Link to home
Start Free TrialLog in
Avatar of Haver Ramirez
Haver Ramirez

asked on

Properties by reflexion

how can access sub class properties?, I can access Y properties, in this case Name, but not x, another case is the same but instead of single reference of x, with a list of x, in this second case how iterate every object.

	public class X
	{
		public int ID{get;set;}	
		public int Name{get;set;}
	}

	public class y
	{

		public string Name{get;set;}
		public x Reference{get:set;}
	}

	//Second case with a List
	public class y
	{

		public string Name{get;set;}
		public List<x> Reference{get:set;}
	}

	public static void Main()
	{
		y classY = new y();
		y.Name = "some text here";
		y.x.ID = "1";
		y.x.Name ="some text for x here";
	}

	// in another class, pass y
	// so, in this method I only can get 'y' values, but not x
	Hashtable table = new Hashtable();
	public void GetProperties(object p)
	{
	    Type mytype = p.GetType();
	    var properties = mytype.GetProperties(BindingFlags.Public | BindingFlags.Instance);

	    foreach (var property in properties)
	    {
	    	table.Add(property.Name, property.GetValue(p, null));
		}	
	}

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

Which is the subclass? You have two classes with the same exact name (y), which will not compile.
Avatar of Haver Ramirez
Haver Ramirez

ASKER

the other class named Y is a list, and this is second case...in the first, how iterate all objects including references and the other the same case but with a list instead a single object.
ASKER CERTIFIED SOLUTION
Avatar of Haver Ramirez
Haver Ramirez

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Solved by my self