Thursday, July 10, 2008

C#中隐藏类属性

当对象设置为PropertyGrid.SelectObject的对象时,可使用下面的方法隐藏部分属性:

void SetPropertyVisible(object obj, string propertyName, bool visibles){
Type type = typeof(System.ComponentModel.BrowsableAttribute);
    PropertyDescriptor propDesc = TypeDescriptor.GetProperties(obj)[propertyName];
Attribute attr = propDesc.Attributes[type];
FieldInfo fieldInfo = type.GetField("browsable", BindingFlags.Instance BindingFlags.NonPublic);
fieldInfo.SetValue(attr, visible);
}

需要注意的是,定义类属性时,要将所有的属性都标记Browsable属性。
[Browsable(true)]
[ReadOnly(false)]
public string String1{
get { return this.string1; }
set { this.string1 = value; }
}

同样的也可以通过上面的方法设置属性是否只读,只需更改2行代码:
Type type = typeof(System.ComponentModel.ReadOnlyAttribute);
FieldInfo fieldInfo = type.GetField("isReadOnly", BindingFlags.Instance BindingFlags.NonPublic);

Wednesday, July 9, 2008

Protocol Buffers: Google's Data Interchange Format

Google本周一发布了该公司内部使用的开放源代码数据描述语言Protocol Buffers.Protocol Buffers与XML相似,但更简单、更小、更快.
谷歌的文档称,与具有可比性的XML文件相比,Protocol Buffers文件的尺寸要小3-10倍,解析速度要快20-100倍.
希望能果真如此,感谢Google所做的贡献。