110,534
社区成员
发帖
与我相关
我的任务
分享
public class DynamicDescriptionProvider : TypeDescriptionProvider
{
public DynamicDescriptionProvider() : base() { }
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
return new DynamicTypeDescriptor(objectType, instance);
}
}
public class DynamicTypeDescriptor : CustomTypeDescriptor
{
public DynamicTypeDescriptor(Type objectType, object instance)
: base()
{
if (instance != null)
{
var tmp = (IDictionary<string, object>)instance;
var names = tmp.Keys;
foreach (var name in names)
{
customFields.Add(new DynamicPropertyDescriptor(name, instance));
}
}
}
List<PropertyDescriptor> customFields = new List<PropertyDescriptor>();
public override PropertyDescriptorCollection GetProperties()
{
return new PropertyDescriptorCollection(customFields.ToArray());
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return new PropertyDescriptorCollection(customFields.ToArray());
}
}
public class DynamicPropertyDescriptor : PropertyDescriptor
{
Type propertyType = typeof(object);
public DynamicPropertyDescriptor(string name, object instance)
: base(name, null)
{
var obj = (IDictionary<string, object>)instance;
if (obj[name] != null)
propertyType = obj[name].GetType();
}
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get
{
return typeof(FastExpando);
}
}
public override object GetValue(object component)
{
IDictionary<string, object> obj = (IDictionary<string, object>)component;
return obj[Name];
}
public override bool IsReadOnly
{
get
{
return false;
}
}
public override Type PropertyType
{
get
{
return propertyType;
}
}
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
IDictionary<string, object> obj = (IDictionary<string, object>)component;
obj[Name] = value;
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}