HI WELCOME TO SIRIS

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

Leave a Comment
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 { getset; }
public string username { getset; }
public string location { getset; }
}
}

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 userdetailsAs 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.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.