Ο κώδικάς μου από μια βιβλιοθήκη που έχω φτοιάξει.
using System;
using System.Reflection;
namespace GiveYourNamespace
{
public class Property
{
public Property()
{
}
protected static void SetValue(object TargetObject,PropertyInfo Property,object Value)
{
Property.SetValue(TargetObject,Value,null);
}
protected static object GetValue(object TargetObject,PropertyInfo Property)
{
return Property.GetValue(TargetObject,null);
}
public static void SetValue(object TargetObject,string PropertyName,object Value)
{
if(PropertyName=="")
return;
PropertyInfo xInfo=GetProperty(TargetObject.GetType().GetProperties(),PropertyName);
if(xInfo==null)
return;
SetValue(TargetObject,xInfo,Value);
}
public static object GetValue(object TargetObject,string PropertyName)
{
if(PropertyName=="")
return null;
PropertyInfo xInfo=GetProperty(TargetObject.GetType().GetProperties(),PropertyName);
if(xInfo==null)
return null;
return GetValue(TargetObject,xInfo);
}
protected static PropertyInfo GetProperty(PropertyInfo[] Properties,string PropertyName)
{
for(int i=0;i<Properties.Length;i++)
{
if(Properties
.Name==PropertyName)
return Properties
;
}
return null;
}
}
}
Η χρήση πχ GiveYourNamespace.Property.SetValue(oForm,"Visible",true);
Στο λαμπτήρα είναι σε άγκιστρα το i του for-loop αλλά έχει bug η εμφάνιση.