Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Get List Object Properties and Values using Reflection in C#, VB.NET

Feb 16, 2017
Introduction:

Here we will learn how to use reflection to get list object properties and values in c#, vb.net with example or get all properties and values of an object in c#, vb.net with example or get list of properties from list of objects using reflection in c#, vb.net or how to get property names and values using reflection in c#, vb.net with example. By using Reflection properties we can easily get list object property names and values based on our requirements.

Description:

In previous articles I explained send generic list as parameter to function in c#, convert list object to json string in c#, vb.net, bind generic list to gridview example in asp.net, jQuery bind data to asp.net gridview example, dictionary object in c#, vb.net with example, static constructor in c#, vb.net with example, difference between ref and out in c#, vb.net with example and many more articles related to generic list, asp.net, mvc, c#, vb.net. Now I will explain how to get all property names and values of an object in c#, vb.net with example.

By using Reflection properties (PropertiesInfo) we can easily get list object property names and values based on our requirements.

Following is the simple code snippet to get all the property names and values of an object in c#, vb.net.

C# Code


Type type = user.GetType();
PropertyInfo[] props = type.GetProperties();
string str = "{";
foreach (var prop in props)
{
str+= (prop.Name+":"+  prop.GetValue(user))+",";
}
return str.Remove(str.Length-1)+"}";

VB.NET Code


Dim type As Type = user.[GetType]()
Dim props() As PropertyInfo = type.GetProperties()
Dim str As String = "{"
For Each prop In props
str += (prop.Name + ":" + CStr(prop.GetValue(user))) + ","
Next
Return str.Remove(str.Length - 1) + "}"

If you want complete example to get all the properties and values of an object write the code like as shown following.

C# Code


using System;
using System.Collections.Generic;
using System.Reflection;

namespace SampleConsoleApp
{
class Program
{
static void Main(string[] args)
{
List<userdetails> items = new List<userdetails>();
items.Add(new userdetails { userid = 1, username = "suresh", location = "chennai" });
items.Add(new userdetails { userid = 2, username = "rohini", location = "guntur" });
items.Add(new userdetails { userid = 3, username = "praveen", location = "bangalore" });
items.Add(new userdetails { userid = 4, username = "sateesh", location = "vizag" });
items.Add(new userdetails { userid = 5, username = "madhav", location = "nagpur" });
items.Add(new userdetails { userid = 6, username = "honey", location = "nagpur" });
string strmsg = string.Empty;
foreach (var user in items)
{
strmsg = GetPropertyValues(user);
Console.WriteLine(strmsg);
}
Console.ReadLine();
}
private static string GetPropertyValues(userdetails user)
{
Type type = user.GetType();
PropertyInfo[] props = type.GetProperties();
string str = "{";
foreach (var prop in props)
{
str+= (prop.Name+":"+  prop.GetValue(user))+",";
}
return str.Remove(str.Length-1)+"}";
}
}
class userdetails
{
public int userid { get; set; }
public string username { get; set; }
public string location { get; set; }
}
}

VB.NET Code


Imports System.Reflection

Module Module1
Sub Main()
Dim items As New List(Of userdetails)()
Dim user As New userdetails()
items.Add(New userdetails() With {
.userid = 1,
.username = "suresh",
.location = "chennai"
})
items.Add(New userdetails() With {
.userid = 2,
.username = "rohini",
.location = "guntur"
})
items.Add(New userdetails() With {
.userid = 3,
.username = "praveen",
.location = "bangalore"
})
items.Add(New userdetails() With {
.userid = 4,
.username = "sateesh",
.location = "vizag"
})
items.Add(New userdetails() With {
.userid = 5,
.username = "madhav",
.location = "nagpur"
})
items.Add(New userdetails() With {
.userid = 6,
.username = "honey",
.location = "nagpur"
})
Dim strmsg As String = String.Empty
For Each user In items
strmsg = GetPropertyValues(user)
Console.WriteLine(strmsg)
Next
Console.ReadLine()
End Sub
Private Function GetPropertyValues(user As userdetails) As String
Dim type As Type = user.[GetType]()
Dim props() As PropertyInfo = type.GetProperties()
Dim str As String = "{"
For Each prop In props
str += (prop.Name + ":" + CStr(prop.GetValue(user))) + ","
Next
Return str.Remove(str.Length - 1) + "}"
End Function
Class userdetails
Public Property userid() As Integer
Get
Return m_userid
End Get
Set
m_userid = Value
End Set
End Property
Private m_userid As Integer
Public Property username() As String
Get
Return m_username
End Get
Set
m_username = Value
End Set
End Property
Private m_username As String
Public Property location() As String
Get
Return m_location
End Get
Set
m_location = Value
End Set
End Property
Private m_location As String
End Class
End Module

If you observe above code we added namespace “System.Reflection” to get properties and values of an object using PropertyInfo property.

Demo

When we run above code we will get result like as shown below

Get List Object Properties and Values using Reflection in C#, VB.NET

This is how we can access all the property names and values of an object based on our requirements.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.