有没有一种方法可以使"Browsable"属性有条件地出现,这样应用它的属性有时会出现在属性页上,有时则不会?
谢谢 :)
有没有一种方法可以使"Browsable"属性有条件地出现,这样应用它的属性有时会出现在属性页上,有时则不会?
谢谢 :)
我不确定这是否适用于你的情况,但你可以通过调用下面的函数来在运行时调整“Browsable”修饰。
/// <summary>
/// Set the Browsable property.
/// NOTE: Be sure to decorate the property with [Browsable(true)]
/// </summary>
/// <param name="PropertyName">Name of the variable</param>
/// <param name="bIsBrowsable">Browsable Value</param>
private void setBrowsableProperty(string strPropertyName, bool bIsBrowsable)
{
// Get the Descriptor's Properties
PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(this.GetType())[strPropertyName];
// Get the Descriptor's "Browsable" Attribute
BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrowsable = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
// Set the Descriptor's "Browsable" Attribute
isBrowsable.SetValue(theDescriptorBrowsableAttribute, bIsBrowsable);
}
没有简单的方法。
您可以通过实现ICustomTypeDescriptor来解决这个问题。这里有一篇关于实现ICustomTypeDescriptor的好文章:实现ICustomTypeDescriptor。
或者,您可以将自己的ControlDesigner与您的类相关联,并重写PreFilterProperties方法以添加或删除在属性网格中查看的属性。
ExpandableObjectConverter
的自定义TypeDescriptor
,并根据需要包含/排除给定属性 - 但这仅适用于属性页使用的PropertyGrid
。更复杂的方法是使用ICustomTypeDescriptor
/TypeDescriptionProvider
- 这样可以在诸如DataGridView
之类的东西中工作。John Cummings的解决方案对我来说基本有效,但由于他引入了泛型(尽管非常聪明),导致以下两个问题:
1- 版本SetBrowsableProperty<T>(T obj, string strPropertyName, bool bIsBrowsable)
将在将集合作为参数obj传递时失败,因为在这种情况下,T将是IEnumerable的实现(例如List、Array等),而不是实际预期的集合类型。
2- 它允许传递原始类型,这在这种情况下是毫无意义的,几乎总会失败。
完整修订后的解决方案:
以下是修订后的解决方案,解决了这些问题并适用于我的情况:(我稍微重命名了方法和变量)
首先,执行改变Browsable属性值的主要工作的实际方法:
/// <summary>
/// Sets the Browsable attribute value of a property of a non premitive type.
/// NOTE: The class property must be decorated with [Browsable(...)] attribute.
/// </summary>
/// <param name="type">The type that contains the property, of which the Browsable attribute value needs to be changed</param>
/// <param name="propertyName">Name of the type property, of which the Browsable attribute value needs to be changed</param>
/// <param name="isBrowsable">The new Browsable value</param>
public static void SetBrowsableAttributeOfAProperty(Type type, string propertyName, bool isBrowsable)
{
//Validate type - disallow primitive types (this will eliminate problem-2 as mentioned above)
if (type.IsEnum || BuiltInTypes.Contains(type))
throw new Exception($"The type '{type.Name}' is not supported");
var objPropertyInfo = TypeDescriptor.GetProperties(type);
// Get the Descriptor's Properties
PropertyDescriptor theDescriptor = objPropertyInfo[propertyName];
if (theDescriptor == null)
throw new Exception($"The property '{propertyName}' is not found in the Type '{type}'");
// Get the Descriptor's "Browsable" Attribute
BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo browsablility = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
// Set the Descriptor's "Browsable" Attribute
browsablility.SetValue(theDescriptorBrowsableAttribute, isBrowsable);
}
<T>
的变体解决方案:public static void SetBrowsableAttributeOfAProperty<T>(string propertyName, bool isBrowsable)
{
SetBrowsableAttributeOfAProperty(typeof(T), propertyName, isBrowsable);
}
现在,之前出现问题的过载(overload)已得到解决,以下修改代码已经解决了它:
/// <summary>
/// Sets the Browsable attribute value of a property of a non premitive type.
/// NOTE: The class property must be decorated with [Browsable(...)] attribute.
/// </summary>
/// <param name="obj">An instance of the type that contains the property, of which the Browsable attribute value needs to be changed.</param>
/// <param name="propertyName">Name of the type property, of which the Browsable attribute value needs to be changed</param>
/// <param name="isBrowsable">Browsable Value</param>
public static void SetBrowsableAttributeOfAProperty<T>(T obj, string propertyName, bool isBrowsable)
{
if (typeof(T).GetInterface("IEnumerable") != null && typeof(T) != typeof(string)) //String type needs to be filtered out as String also implements IEnumerable<char> but its not a normal collection rather a primitive type
{
//Get the element type of the IEnumerable collection
Type objType = obj.GetType().GetGenericArguments()?.FirstOrDefault(); //when T is a collection that implements IEnumerable except Array
if (objType == null) objType = obj.GetType().GetElementType(); //when T is an Array
SetBrowsableAttributeOfAProperty(objType, propertyName, isBrowsable);
}
else
SetBrowsableAttributeOfAProperty(typeof(T), propertyName, isBrowsable);
以下是一个实用函数,可以获取所有C#系统内置的(原始)类型:
public static List<Type> BuiltInTypes
{
get
{
if (builtInTypes == null)
builtInTypes = Enum.GetValues(typeof(TypeCode)).Cast<TypeCode>().Select(t => Type.GetType("System." + Enum.GetName(typeof(TypeCode), t)))
.ToList();
return builtInTypes;
}
}
用法:
class Foo
{
[Browsable(false)]
public string Bar { get; set; }
}
void Example()
{
SetBrowsableAttributeOfAProperty<Foo>("Bar", true); //works
Foo foo = new Foo();
SetBrowsableAttributeOfAProperty(foo, "Bar", false); //works
List<Foo> foos = new List<Foo> { foo, new Foo { Bar = "item2" } };
SetBrowsableAttributeOfAProperty(foos, "Bar", true); //works now, whereas it would crash with an exception in John Cummings's solution
}
/// <summary>
/// Set the Browsable property.
/// NOTE: Be sure to decorate the property with [Browsable(true)]
/// </summary>
/// <param name="PropertyName">Name of the variable</param>
/// <param name="bIsBrowsable">Browsable Value</param>
private void SetBrowsableProperty<T>(string strPropertyName, bool bIsBrowsable)
{
// Get the Descriptor's Properties
PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(typeof(T))[strPropertyName];
// Get the Descriptor's "Browsable" Attribute
BrowsableAttribute theDescriptorBrowsableAttribute = (BrowsableAttribute)theDescriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrowsable = theDescriptorBrowsableAttribute.GetType().GetField("Browsable", BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.Instance);
// Set the Descriptor's "Browsable" Attribute
isBrowsable.SetValue(theDescriptorBrowsableAttribute, bIsBrowsable);
}
/// <summary>
/// Set the Browsable property.
/// NOTE: Be sure to decorate the property with [Browsable(true)]
/// </summary>
/// <param name="obj">An instance of the object whose property should be modified.</param>
/// <param name="PropertyName">Name of the variable</param>
/// <param name="bIsBrowsable">Browsable Value</param>
private void SetBrowsableProperty<T>(T obj, string strPropertyName, bool bIsBrowsable)
{
SetBrowsableProperty<T>(strPropertyName, bIsBrowsable);
}
使用方法:
class Foo
{
[Browsable(false)]
public string Bar { get; set; }
}
void Example()
{
SetBrowsableProperty<Foo>("Bar", true);
Foo foo = new Foo();
SetBrowsableProperty(foo, "Bar", false);
}
我在搜索中遇到了这个问题,想要声明某些成员在 IntelliSense 中可见或隐藏,并能够一次性更改所有需要在编译时隐藏的内容。我不确定这是否是您正在寻找的,但我找到了我的问题的答案...认为分享一下也无妨。
我设置了一个条件编译符号(在项目属性的“生成”选项卡中找到)IS_VIS(如果您想要显示某些成员,则值为 true;如果您想要隐藏它们,则值为 false),然后:
#if IS_VIS
public const System.ComponentModel.EditorBrowsableState isVis =
ComponentModel.EditorBrowsableState.Always;
#else
public const System.ComponentModel.EditorBrowsableState isVis =
ComponentModel.EditorBrowsableState.Never;
#endif
然后在属性中引用isVis变量:
[EditorBrowsable(isVis)]
public string myMethod...
我用VB做了这个,然后匆忙转换成了C#。如果有什么不对的地方,请告诉我。
public static void SetBrowsableAttributeValue(Type type, string propertyName, object value)
{
// Get the Descriptor's Properties
var descriptor = TypeDescriptor.GetProperties(type)[propertyName];
// Get the Descriptor's "Browsable" Attribute
var attrib = (BrowsableAttribute) descriptor.Attributes[typeof(BrowsableAttribute)];
var isBrow = attrib.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(a=>a.Name.Contains("browsable", StringComparison.OrdinalIgnoreCase));
// Set the Descriptor's "Browsable" Attribute
isBrow?.SetValue(attrib, value);
}
PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(this.GetType())[strPropertyName];
中出现了 nullValue 异常。我想知道在哪里放置这些函数以及它如何反映在属性网格上(在函数中没有看到 propertyGrid 对象)。 - Ganesh Kamath - 'Code Frenzy'PropertyDescriptor theDescriptor = TypeDescriptor.GetProperties(vObject.GetType())[strPropertyName];
,其中 vObject 是我正在修改的对象。 - Ganesh Kamath - 'Code Frenzy'var isBrow = attrib.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic).FirstOrDefault(a=>a.Name.Contains("browsable", StringComparison.OrdinalIgnoreCase));
- undefined