Answered a similar question earlier. Here s a code sample, and link to the other question.
Hiding Public Functions
You re going to be most interested in the GetValue method of the System.Reflection.PropertyInfo class.
C# Code Sample
public bool SetProperty(object obj, string PropertyName, object val)
{
object property_value = null;
System.Reflection.PropertyInfo[] properties_info = obj.GetType.GetProperties;
System.Reflection.PropertyInfo property_info = default(System.Reflection.PropertyInfo);
foreach (System.Reflection.PropertyInfo prop in properties_info) {
if (prop.Name == PropertyName) property_info = prop;
}
if (property_info != null) {
try {
property_info.SetValue(obj, val, null);
return true;
}
catch (Exception ex) {
return false;
}
}
else {
return false;
}
}
public object GetProperty(object obj, string PropertyName)
{
object property_value = null;
System.Reflection.PropertyInfo[] properties_info = obj.GetType.GetProperties;
System.Reflection.PropertyInfo property_info = default(System.Reflection.PropertyInfo);
foreach (System.Reflection.PropertyInfo prop in properties_info) {
if (prop.Name == PropertyName) property_info = prop;
}
if (property_info != null) {
try {
property_value = property_info.GetValue(obj, null);
return property_value;
}
catch (Exception ex) {
return null;
}
}
else {
return null;
}
}
VB Code Sample
Public Function SetProperty(ByVal obj As Object, ByVal PropertyName As String, ByVal val As Object) As Boolean
Dim property_value As Object
Dim properties_info As System.Reflection.PropertyInfo() = obj.GetType.GetProperties
Dim property_info As System.Reflection.PropertyInfo
For Each prop As System.Reflection.PropertyInfo In properties_info
If prop.Name = PropertyName Then property_info = prop
Next
If property_info IsNot Nothing Then
Try
property_info.SetValue(obj, val, Nothing)
Return True
Catch ex As Exception
Return False
End Try
Else
Return False
End If
End Function
Public Function GetProperty(ByVal obj As Object, ByVal PropertyName As String) As Object
Dim property_value As Object
Dim properties_info As System.Reflection.PropertyInfo() = obj.GetType.GetProperties
Dim property_info As System.Reflection.PropertyInfo
For Each prop As System.Reflection.PropertyInfo In properties_info
If prop.Name = PropertyName Then property_info = prop
Next
If property_info IsNot Nothing Then
Try
property_value = property_info.GetValue(obj, Nothing)
Return property_value
Catch ex As Exception
Return Nothing
End Try
Else
Return Nothing
End If
End Function