Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Public class Person
{
public string name{get;set;}//get this via Model class
..

...
}

public class Employee
{
public Person person{get;set}
}

public class Model:Employee
{
}









...


...


Model model=new model();


Need to get the Names and values of properties of Person class via model class through reflection ....Please Help Iam stuck
Posted
Updated 21-Jun-15 4:27am
v3
Comments
Andreas Gieriet 21-Jun-15 13:54pm    
Where are you stuck?
What did you try?
Andi

Have a look at this: Converting a List to a DataTable[^] - it gets the property names and values.
 
Share this answer
 
Comments
Member 11665036 21-Jun-15 11:04am    
OriginalGriff , thanks buddy for the quick response , i respect it , but i need to get it done through Reflection only , Data table will create problems for me .....
OriginalGriff 21-Jun-15 11:17am    
For goodness sake - did you expect us to sit down and just write the code for you?
Follow the link, and look at the code - it's not complicated - and it takes a list of objects, extracts the property names and values and fills in a dataTable using them. Take out the datatable stuff, and what do you have left?
Member 11665036 21-Jun-15 12:42pm    
dont ,panic ,i said it cant fit my needs , rest ur solution is best ,and i am not telling u to write the code for me , i said i have a problem which ur solution can't solve
Member 11665036 21-Jun-15 11:05am    
Something like this, Obj.GetType().GetProperties();
Ralf Meier 21-Jun-15 13:38pm    
Have you took a look to the linked solution of OriginalGriff ?
You don't need the Datatable - but all what you need is to be seen in the example ...
Here is a VB-Solution - of course VB and not C# - but I normally work with VB.
But most of that, what is necessary for you, is .Net and should be easy converted to C# ...
VB
Public Shared Function GetProperty(ByVal component As Object, ByVal propertyName As String) As Object
    Try
        Dim pd As System.ComponentModel.PropertyDescriptor = System.ComponentModel.TypeDescriptor.GetProperties(component)(propertyName)

        Return pd.GetValue(component)
    Catch
        Return Nothing
    End Try
End Function


Public Shared Function GetPropertyList(ByVal component As Object) As String()
    Dim pis As System.Reflection.PropertyInfo() = component.GetType().GetProperties()

    Dim myLen As Integer = pis.GetLength(0) - 1
    Dim myArray(myLen) As String
    For i As Integer = 0 To myLen
        myArray(i) = pis(i).Name
    Next

    Return myArray
End Function


But it is in fact nothing else as the posted Solution of OriginalGriff ...
 
Share this answer
 

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