Solved

Properties by reflexion

Posted on 2013-01-14
4
153 Views
Last Modified: 2013-01-26
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

0
Comment
Question by:Crashman
  • 3
4 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Which is the subclass? You have two classes with the same exact name (y), which will not compile.
0
 
LVL 8

Author Comment

by:Crashman
Comment Utility
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.
0
 
LVL 8

Accepted Solution

by:
Crashman earned 0 total points
Comment Utility
Solved, every entity used IEditableObject, the hastable is saved before BeginEdit, later, every object iterates his own hashtable
0
 
LVL 8

Author Closing Comment

by:Crashman
Comment Utility
Solved by my self
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Email Body 4 45
(code works) but print excel in Landscape 5 49
How do I get the id from URL? 19 45
C# Export DataGridView 4 37
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now