111,125
社区成员
发帖
与我相关
我的任务
分享 class ObjectDescriptionProvider : TypeDescriptionProvider
{
public ObjectDescriptionProvider() : base(TypeDescriptor.GetProvider(typeof(object))) { }
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);
PropertyInfo pi = objectType.GetProperty("Name");
if (pi != null&&pi.PropertyType==typeof(string) )
{
BrowsableAttribute[] bas = (BrowsableAttribute[])pi.GetCustomAttributes(typeof(BrowsableAttribute), false);
if (bas.Length > 0 && !bas[0].Browsable)
return new NameCustomTypeDescriptor(defaultDescriptor);
}
return defaultDescriptor;
}
}
class NameCustomTypeDescriptor : CustomTypeDescriptor
{
public NameCustomTypeDescriptor(ICustomTypeDescriptor parent)
: base(parent)
{
pd = TypeDescriptor.CreateProperty(parent.GetType().GetField("_objectType", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(parent) as Type, "Name", typeof(string), BrowsableAttribute.Yes);
}
private PropertyDescriptor pd;
public override PropertyDescriptorCollection GetProperties()
{
PropertyDescriptorCollection pdc = base.GetProperties();
PropertyDescriptor[] pds = new PropertyDescriptor[pdc.Count + 1];
pds[0] = pd;
pdc.CopyTo(pds, 1);
return new PropertyDescriptorCollection(pds);
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc = base.GetProperties(attributes);
PropertyDescriptor[] pds = new PropertyDescriptor[pdc.Count + 1];
pds[0] = pd;
pdc.CopyTo(pds, 1);
return new PropertyDescriptorCollection(pds);
}
}
static void Main()
{
foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies())
AddNameProperty(ass);
AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(CurrentDomain_AssemblyLoad);
//code
static void AddNameProperty(Assembly ass)
{
foreach (Type t in ass.GetTypes())
{
try
{
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(t);
PropertyDescriptor pd = pdc.Find("Name", false);
if (pd != null && !pd.IsBrowsable)
{
pdc.GetType().GetField("readOnly", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(pdc, false);
pdc.Add(TypeDescriptor.CreateProperty(t, "Name", typeof(string), BrowsableAttribute.Yes));
}
}
catch
{
}
}
}
static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
{
AddNameProperty(args.LoadedAssembly);
}
private void propertyGrid1_SelectedObjectsChanged(object sender, EventArgs e)
{
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(propertyGrid1.SelectedObject.GetType());
PropertyDescriptor pd = pdc.Find("Name", false);
if (pd != null && !pd.IsBrowsable)
{
pdc.GetType().GetField("readOnly", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(pdc, false);
pdc.Add(TypeDescriptor.CreateProperty(propertyGrid1.SelectedObject.GetType(), "Name", typeof(string), BrowsableAttribute.Yes));
}
} class NameCustomTypeDescriptor : CustomTypeDescriptor
{
public NameCustomTypeDescriptor(ICustomTypeDescriptor parent,Type objectType)
: base(parent)
{
pd = TypeDescriptor.CreateProperty(objectType, "Name", typeof(string), BrowsableAttribute.Yes);
}
//.................
class ObjectDescriptionProvider : TypeDescriptionProvider
{
public ObjectDescriptionProvider() : base(TypeDescriptor.GetProvider(typeof(Object))) { }
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
ICustomTypeDescriptor defaultDescriptor = base.GetTypeDescriptor(objectType, instance);
PropertyInfo pi = objectType.GetProperty("Name");
if (pi != null && pi.PropertyType==typeof(string))
{
BrowsableAttribute[] bas = (BrowsableAttribute[])pi.GetCustomAttributes(typeof(BrowsableAttribute), false);
if (bas.Length > 0 && !bas[0].Browsable)
return new NameCustomTypeDescriptor(defaultDescriptor);
}
return defaultDescriptor;
}
}
class NameCustomTypeDescriptor : CustomTypeDescriptor
{
public NameCustomTypeDescriptor(ICustomTypeDescriptor parent)
: base(parent)
{
}
public override PropertyDescriptorCollection GetProperties()
{
PropertyDescriptorCollection pdc = base.GetProperties();
PropertyDescriptor[] pds = new PropertyDescriptor[pdc.Count + 1];
pds[0] = new NamePropertyDescriptor();
pdc.CopyTo(pds, 1);
return new PropertyDescriptorCollection(pds);
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
PropertyDescriptorCollection pdc = base.GetProperties(attributes);
PropertyDescriptor[] pds = new PropertyDescriptor[pdc.Count + 1];
pds[0] = new NamePropertyDescriptor();
pdc.CopyTo(pds, 1);
return new PropertyDescriptorCollection(pds);
}
}
class NamePropertyDescriptor : PropertyDescriptor
{
public NamePropertyDescriptor() : base("(Name)", null) { }
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get
{
return typeof(object);
}
}
public override object GetValue(object component)
{
return component.GetType().GetProperty("Name").GetValue(component, null);
}
public override bool IsReadOnly
{
get
{
return false;
}
}
public override Type PropertyType
{
get
{
return typeof(string);
}
}
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
component.GetType().GetProperty("Name").SetValue(component, value, null);
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}