Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Answered a similar question earlier. Here's a code sample, and link to the other question. <a href="https://stackoverflow.com/questions/1754516/hiding-public-functions/1755672#1755672">Hiding Public Functions</a></p> <p>You're going to be most interested in the <a href="http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo.getvalue.aspx" rel="nofollow noreferrer">GetValue</a> method of the System.Reflection.PropertyInfo class.</p> <p>C# Code Sample</p> <pre><code>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;     } } </code></pre> <p>VB Code Sample</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload