Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am looping through property collection using below loop:
C#
foreach (System.Reflection.PropertyInfo eachProperty in this.UIPageAttributes.GetType().GetProperties())
{

}


Now for eachProperty i want to ,compare it against the List<> collection. For checking against any other data type and even for any entity i m using:
C#
if (eachProperty.GetType().Equals(typeof(string))

  if(eachProperty.GetType().Equals(typeof(EntityClass)))
;;;;


But for checking List<> collection type, I didnt find anything where I can directly check for this type,

Any help would be great...
Posted
Updated 15-Nov-11 19:47pm
v3

First of all eachProperty has the type System.Reflection.PropertyInfo, not the type it describes. So, eachProperty.GetType() will always return typeof(System.Reflection.PropertyInfo). The type described by this meta-data is eachProperty.PropertyType. Use it to solve your problem.

Some minor problem is using object.Equals for type comparison. It's not good and is even of low performance because its implementation may need to check up type dynamically (you may not care about it, but nevertheless). Instead, use comparison via "==" operator which only works with the type of System.Type and is also more readable.

The ultimate goal of all this activity is not clear (you did not explain it, which is a common mistake) and looks somewhat suspicious to me. Every time the reflected types are explicitly compared suggests it could be OOP violation, a substitution of real OOP design. I cannot say for sure based on your information, so perhaps just think about it.

—SA
 
Share this answer
 
v2
Comments
coolsanjay 16-Nov-11 2:48am    
What I wanted to do I will describe now. Lets say we have Employee class with some properties like FirstBName, LastName. Also having one property as Departments which is nothing but List<department>, departments which employee belongs to. Now, I want to return string formatted property name and its corresponding value so instead of hardcoding like string str = "FirstName:" + employee.FirstName i thot to use Reflection and use:
foreach (System.Reflection.PropertyInfo eachProperty in this.EmployeeObject.GetType().GetProperties())
{

}

So properties I am able to fetch using
if (eachProperty.PropertyType == typeof(string) && eachProperty.GetValue(this.EmployeeObject, null) != null &&
!string.IsNullOrEmpty(eachProperty.GetValue(this.EmployeeObject, null).ToString()))

But how to check for List<department>?
coolsanjay 16-Nov-11 2:55am    
SAKryukov, Please help me how to solve List<. comparison problem with propertyType.
if (eachProperty.PropertyType == typeof(List<>))?
Sergey Alexandrovich Kryukov 16-Nov-11 3:03am    
Yes, what's wrong with this?
--SA
Sergey Alexandrovich Kryukov 16-Nov-11 3:03am    
The original idea is in fact good, I appreciate it, the idea of implementation is not.
You need to Reflect the Employee class only once, keep the set of properties to show and get the value and name of each property -- the name only once, but the value each time it is needed. Keep each property in some "reflection cache" type so you won't re-reflect it each time, only use to GetValue.

List is no different, this is the property, so you will get its name in the same way. GetValue will return the instance of the list. The problem is: is the list heterogeneous or not? What do you need to do with the list? If this is only returning the string to present somewhere, do the following: for the type of the element, override object.ToString accordingly. If this is something else, explain what, I would be able to help.

--SA
coolsanjay 16-Nov-11 3:46am    
Right now I have written
if(eachProperty.Name.Equals("products")) // products is List<Product>
{
IEnumerable<Product> productList = eachProperty.GetValue(this.UIPageAttributes, null) as IEnumerable<Product>;

int idx = 1;

if (productList != null)
{
foreach (Product eachProduct in productList)
{

string.Format("{0}:{1}|", product.Name,
product.GetValue(eachProduct, null).ToString())
}
}
C#
foreach (System.Reflection.PropertyInfo eachProperty in this.UIPageAttributes.GetType().GetProperties())
{
if (eachProperty.PropertyType == this.EmployeeObject.DepartmentList.GetType())  

Solved my problem.

Thanks for suggestions.
 
Share this answer
 
v2
you have to implement iComparable Interface in your EntityClass class.

and you have to implement the equals method inside your class, you have to write your own logic to check the equality of two objects.

follow the link to learn more..

http://support.microsoft.com/kb/320727[^]


mark as answer if solves your problem, it motivates :)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 2:50am    
Pandya, you did not earn motivation here. Your "solution" won't solve anything. Implementing IComparable is completely redundant and hence wrong. OP needs comparison of classed, not class instances. Same thing about "equals".

Operator "==" does correct comparison for types. OP's problem is completely different, and I've completely solved it. Sorry, this is false solution. It deserves a vote no more than 1, totally negative result.

--SA
If you know the type of elements of the list, you can use typeof(List< TypeHere >). If you just want to check if it is a list, use this: eachProperty.GetType().Name.Equals("List`1")
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 2:53am    
Sorry, the solution is all wrong. Comparing anything by name is a bad style and is not really reliable. Also, immediate string constants... very bad. If you mistype, a compiler will never see the problem. Types are reliably compared with "==". Also, this is irrelevant to OP's problem. I solved it completely, please see. I'm really sorry, but this is a false solution, deserves 1. I would better removed it.
--SA
dan!sh 16-Nov-11 3:24am    
I agree this is not really a way to do this.
I did not spent time to really analyze what exactly OP is trying to do. I still do not see any need to use reflection here. I understand he wants to format properties as string but why? He might really do well without reflection.
dan!sh 16-Nov-11 3:24am    
I will not remove it since people reading will also get to know the wrong way of doing this.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900