以下是更改 JointItem 类中属性的 Browsable 属性的方法:
private void ChangeBrowsability(object pThis, string pProperty, bool pBrowsable)
{
PropertyDescriptor pdDescriptor = TypeDescriptor.GetProperties(pThis.GetType())[pProperty];
BrowsableAttribute baAttribute = (BrowsableAttribute)pdDescriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo fiBrowsable = baAttribute.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
fiBrowsable.SetValue(baAttribute, pBrowsable);
}
那么你可以有一个很大的 if then else 序列或类似的东西:
JointItem jiThis = WhereEverYouGetYourJointItemFrom();
if (jiThis.JointType == eJoinType.Elbow)
{
ChangeBrowsability(jiThis, "JointAngle", true);
ChangeBrowsability(jiThis, "MinAngle", true);
ChangeBrowsability(jiThis, "MaxAngle", true);
ChangeBrowsability(jiThis, "ScrewType", false);
//...
}
else ...
你当然需要你的“使用”剂量!
using System.ComponentModel;
using System.Reflection;