How to use PropertyGrid to allow editing properties without a setter? - c#

By default, PropertyGrid only allowed editing properties with public setter. I'd like to allow editing of properties without a setter.
For example:
class A {
public int X {get;set}
public int Y {get;}
}
In the example above, only X will be editable. Y will be displayed but grayed out. How can I make Y editable?
Note: making a private backing field would be OK. For example:
class A {
public int X {get;set}
private int y;
public int Y {get => y; }
}

You can build a wrapper/proxy class based on the ICustomTypeDescriptor Interface that allows you to tweak properties at runtime.
This is how you could use it:
var a = new A();
// build a proxy
var proxy = new Proxy(a);
// tweak any properties
proxy.Properties["Y"].IsReadOnly = false;
// you can also tweak attributes
proxy.Properties["Y"].Attributes.Add(new CategoryAttribute("R/O -> R/W"));
proxy.Properties["Y"].Attributes.Add(new DescriptionAttribute("This works"));
// handle property change
propertyGrid1.PropertyValueChanged += (s, e) =>
{
if (e.ChangedItem.PropertyDescriptor.Name == "Y")
{
a.Y = (int)e.ChangedItem.Value;
}
};
// select the proxy instead of the original instance
propertyGrid1.SelectedObject = proxy;
And here is the result
...
class A
{
public int X { get; set; }
public int Y { get; internal set; }
}
...
public class Proxy : ICustomTypeDescriptor
{
public Proxy(object instance)
{
if (instance == null)
throw new ArgumentNullException(nameof(instance));
Instance = instance;
Properties = TypeDescriptor.GetProperties(instance).OfType<PropertyDescriptor>().Select(d => new ProxyProperty(instance, d)).ToDictionary(p => p.Name);
}
public object Instance { get; }
public IDictionary<string, ProxyProperty> Properties { get; }
public AttributeCollection GetAttributes() => TypeDescriptor.GetAttributes(Instance);
public string GetClassName() => TypeDescriptor.GetClassName(Instance);
public string GetComponentName() => TypeDescriptor.GetComponentName(Instance);
public TypeConverter GetConverter() => TypeDescriptor.GetConverter(Instance);
public EventDescriptor GetDefaultEvent() => TypeDescriptor.GetDefaultEvent(Instance);
public object GetEditor(Type editorBaseType) => TypeDescriptor.GetEditor(Instance, editorBaseType);
public EventDescriptorCollection GetEvents() => TypeDescriptor.GetEvents(Instance);
public EventDescriptorCollection GetEvents(Attribute[] attributes) => TypeDescriptor.GetEvents(Instance, attributes);
public PropertyDescriptor GetDefaultProperty() => TypeDescriptor.GetDefaultProperty(Instance);
public PropertyDescriptorCollection GetProperties() => new PropertyDescriptorCollection(Properties.Values.Select(p => new Desc(this, p)).ToArray());
public PropertyDescriptorCollection GetProperties(Attribute[] attributes) => GetProperties();
public object GetPropertyOwner(PropertyDescriptor pd) => Instance;
private class Desc : PropertyDescriptor
{
public Desc(Proxy proxy, ProxyProperty property)
: base(property.Name, property.Attributes.ToArray())
{
Proxy = proxy;
Property = property;
}
public Proxy Proxy { get; }
public ProxyProperty Property { get; }
public override Type ComponentType => Proxy.GetType();
public override Type PropertyType => Property.PropertyType ?? typeof(object);
public override bool IsReadOnly => Property.IsReadOnly;
public override bool CanResetValue(object component) => Property.HasDefaultValue;
public override object GetValue(object component) => Property.Value;
public override void ResetValue(object component) { if (Property.HasDefaultValue) Property.Value = Property.DefaultValue; }
public override void SetValue(object component, object value) => Property.Value = value;
public override bool ShouldSerializeValue(object component) => Property.ShouldSerializeValue;
}
}
public class ProxyProperty
{
public ProxyProperty(string name, object value)
{
if (name == null)
throw new ArgumentNullException(nameof(value));
Name = name;
Value = value;
Attributes = new List<Attribute>();
}
public ProxyProperty(object instance, PropertyDescriptor descriptor)
{
if (descriptor == null)
throw new ArgumentNullException(nameof(descriptor));
Name = descriptor.Name;
Value = descriptor.GetValue(instance);
var def = descriptor.Attributes.OfType<DefaultValueAttribute>().FirstOrDefault();
if (def != null)
{
HasDefaultValue = true;
DefaultValue = def.Value;
}
IsReadOnly = (descriptor.Attributes.OfType<ReadOnlyAttribute>().FirstOrDefault()?.IsReadOnly).GetValueOrDefault();
ShouldSerializeValue = descriptor.ShouldSerializeValue(instance);
Attributes = descriptor.Attributes.Cast<Attribute>().ToList();
PropertyType = descriptor.PropertyType;
}
public string Name { get; }
public object Value { get; set; }
public object DefaultValue { get; set; }
public bool HasDefaultValue { get; set; }
public bool IsReadOnly { get; set; }
public bool ShouldSerializeValue { get; set; }
public Type PropertyType { get; set; }
public IList<Attribute> Attributes { get; }
}

There is a way to do this, but it is absurdly complex;
you create a custom TypeDescriptionProvider and link it to the type, or implement ICustomTypeDescriptor in the type, and
you create a custom PropertyDescriptor that knows how to edit the field in the GetValue and SetValue
you create a custom TypeDescriptor that echos the standard property descriptors for most things, and your new property descriptor in this case
In all seriousness, though; please don't do this! Just make the property accessible in the first place. If it isn't a settable property, you shouldn't be trying to set it.
based on the comments it sounds like what you actually need is "popsicle immutability"; consider:
class Foo {
private bool _frozen;
public void Freeze() => _frozen = true;
protected void ThrowIfFrozen() {
if (_frozen) throw new InvalidOperationException(
"The object cannot be changed once Freeze has been called");
}
private int _x, _y;
public int X {
get => _x;
set {
if (value != _x) {
ThrowIfFrozen();
_x = value;
}
}
}
public int Y {
get => _y;
set {
if (value != _y) {
ThrowIfFrozen();
_y = value;
}
}
}
}

Related

Protobuf-net Serializing Parent Class inherited object property marked as [ProtoIgnore()] throwing "No serializer defined for type: System.Object"

All the objects in our system inherit a base class which has got a property of type object.
I have tried adding protoignore attribute to all the properties of the base class as well but that doesn't seem to work as well.
class Program
{
static void Main(string[] args)
{
Vehicle vehicle = new Vehicle();
vehicle.BodyStyleDescription = "4x4";
vehicle.BodyStyleText = "Prestige Medium";
dynamic protobufModel = TypeModel.Create();
AddTypeToModel<Vehicle>(protobufModel);
using (MemoryStream compressed = new MemoryStream())
{
using (GZipStream gzip = new GZipStream(compressed, CompressionMode.Compress, true))
{
protobufModel.Serialize(gzip, vehicle);
}
string str = Convert.ToBase64String(compressed.GetBuffer(), 0, Convert.ToInt32(compressed.Length));
}
}
public static MetaType AddTypeToModel<T>(RuntimeTypeModel typeModel)
{
var properties = typeof(T).GetProperties().Select(p => p.Name).OrderBy(name => name);
return typeModel.Add(typeof(T), true).Add(properties.ToArray());
}
}
Following is the hierarchy of the object
public interface IObjectBaseClass
{
[ProtoIgnore()]
object Parent { get; set; }
[ProtoIgnore()]
bool IsSaved { get; set; }
[ProtoIgnore()]
string XmlAtLoad { get; set; }
}
public class ObjectBaseClass : IObjectBaseClass
{
public ObjectBaseClass()
{
}
[ProtoIgnore()]
internal object _Parent;
[ProtoIgnore()]
internal bool _IsSaved;
[ProtoIgnore()]
internal string _XmlAtLoad;
[ProtoIgnore()]
public bool IsSaved
{
get { return _IsSaved; }
set { _IsSaved = value; }
}
[ProtoIgnore()]
public object Parent
{
get { return _Parent; }
set { _Parent = value; }
}
[ProtoIgnore()]
public string XmlAtLoad
{
get { return _XmlAtLoad; }
set { _XmlAtLoad = value; }
}
}
public class Vehicle : ObjectBaseClass
{
private string _BodyStyleText;
private string _BodyStyleDescription;
public string BodyStyleDescription
{
get { return _BodyStyleDescription; }
set { _BodyStyleDescription = value; }
}
public string BodyStyleText
{
get { return _BodyStyleText; }
set { _BodyStyleText = value; }
}
}
Your problem is that when you do typeModel.Add(typeof(T), true).Add(properties.ToArray()) you are adding all properties of T to the runtime type model, including those marked with ProtoIgnore. You can see this by calling the debugging method protobufModel.GetSchema(typeof(Vehicle)) which returns:
message Object {
}
message Vehicle {
optional string BodyStyleDescription = 1;
optional string BodyStyleText = 2;
optional bool IsSaved = 3;
optional Object Parent = 4;
optional string XmlAtLoad = 5;
}
To avoid adding properties marked with [ProtoIgnore], you could do:
public static MetaType AddTypeToModel<T>(RuntimeTypeModel typeModel)
{
var properties = typeof(T)
.GetProperties()
.Where(p => !p.GetCustomAttributes<ProtoIgnoreAttribute>().Any())
.Select(p => p.Name)
.OrderBy(name => name);
return typeModel.Add(typeof(T), true).Add(properties.ToArray());
}
Alternatively, since you are manually annotating some of your models with protobuf attributes anyway, you could mark the derived types with [ProtoContract(ImplicitFields = ImplicitFields.AllPublic)], e.g.:
[ProtoContract(ImplicitFields = ImplicitFields.AllPublic)]
public class Vehicle : ObjectBaseClass
{
private string _BodyStyleText;
private string _BodyStyleDescription;
public string BodyStyleDescription
{
get { return _BodyStyleDescription; }
set { _BodyStyleDescription = value; }
}
public string BodyStyleText
{
get { return _BodyStyleText; }
set { _BodyStyleText = value; }
}
}
Using either method, the schema for Vehicle becomes:
message Vehicle {
optional string BodyStyleDescription = 1;
optional string BodyStyleText = 2;
}
This is what you require.

How to auto-ignore all properties with no corresponding constructor parameter when serializing

Is there some non-attribute-based method of ignoring all properties that don't have a corresponding constructor parameter when serializing? For example, when serializing this class, property Combo should be ignored. A round-trip serialization/deserialization of an instance of MyClass doesn't require Combo to be serialized. Ideally I could use some out-of-the-box setting.
public class MyClass
{
public MyClass(int myInt, string myString)
{
this.MyInt = myInt;
this.MyString = myString;
}
public int MyInt { get; }
public string MyString { get; }
public string Combo => this.MyInt + this.MyString;
}
You can do this with a custom IContractResolver:
public class ConstructorPropertiesOnlyContractResolver : DefaultContractResolver
{
readonly bool serializeAllWritableProperties;
public ConstructorPropertiesOnlyContractResolver(bool serializeAllWritableProperties)
: base()
{
this.serializeAllWritableProperties = serializeAllWritableProperties;
}
protected override JsonObjectContract CreateObjectContract(Type objectType)
{
var contract = base.CreateObjectContract(objectType);
if (contract.CreatorParameters.Count > 0)
{
foreach (var property in contract.Properties)
{
if (contract.CreatorParameters.GetClosestMatchProperty(property.PropertyName) == null)
{
if (!serializeAllWritableProperties || !property.Writable)
property.Readable = false;
}
}
}
return contract;
}
}
Then use it like:
var settings = new JsonSerializerSettings { ContractResolver = new ConstructorPropertiesOnlyContractResolver(false) };
var json = JsonConvert.SerializeObject(myClass, Formatting.Indented, settings );
Pass true for serializeAllWritableProperties if you also want to serialize read/write properties that are not included in the constructor parameter list, e.g. AnUnrelatedReadWriteProperty in:
public class MyClass
{
public MyClass(int myInt, string myString)
{
this.MyInt = myInt;
this.MyString = myString;
}
public int MyInt { get; private set; }
public string MyString { get; private set; }
public string Combo { get { return this.MyInt + this.MyString; } }
public string AnUnrelatedReadWriteProperty { get; set; }
}
Note you may want to cache your contract resolver for best performance.

Xceed WPF propertyGrid show item for expanded collection

How, do I display a ObservableCollection<> of custom objects in the Xceed WPF PropertyGrid in which each List Item can be expanded to display the custom objects properties. (ie:
----PropertyGrid-----
CoreClass
(+/-) ObservableCollection< CustomClass >
(+/-) CustomClass.Object1
Property1: Value
Property2: Value
…
PropertyN: Value
(+/-) CustomClass.Object2
Property1: Value
Property2: Value
…
PropertyN: Value
If I use [ExpandableObject] on the ObservableCollection<> it only shows the Counts property.
Edit:(Added code)
MainWindow.xaml:
<Window x:Class="PropGridExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PropGridExample"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<xctk:PropertyGrid x:Name="PropertyGrid" SelectedObject="{Binding BindingItem}"></xctk:PropertyGrid>
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
MainWindowViewModel mwvm = new MainWindowViewModel();
this.DataContext = mwvm;
InitializeComponent();
}
}
MainWindowViewModel.cs
public class MainWindowViewModel
{
public Item BindingItem { get; set; }
public MainWindowViewModel()
{
BindingItem = new Item();
}
public class Item
{
public int ID { get; set; }
[ExpandableObject()]
public ObservableCollection<CustomClass> Classes { get; set; }
public Item()
{
ID = 1;
Classes = new ObservableCollection<CustomClass>();
Classes.Add(new CustomClass() { Name = "CustomFoo" });
}
}
public class CustomClass
{
public string Name { get; set; }
[ExpandableObject()]
public ObservableCollection<type> Types { get; set; }
public CustomClass()
{
Types = new ObservableCollection<type>();
Types.Add(new type() { name = "foo", value = "bar" });
Types.Add(new type() { name = "bar", value = "foo" });
}
}
public class type
{
public string name { get; set; }
public string value { get; set; }
}
}
Note that most of this idea comes from the CodeProject project you linked to. The article gets you most of the way there, but as you note, it does not expand each item in the collection for the WPF PropertyGrid. In order to do that, each "item" needs to have an ExpandableObjectAttribute.
In order to allow future StackOverflow readers to understand, I'm going to start from beginning.
From the beginning
So, starting from this example:
public class MainWindowViewModel
{
/// <summary> This the object we want to be able to edit in the data grid. </summary>
public ComplexObject BindingComplexObject { get; set; }
public MainWindowViewModel()
{
BindingComplexObject = new ComplexObject();
}
}
public class ComplexObject
{
public int ID { get; set; }
public ObservableCollection<ComplexSubObject> Classes { get; set; }
public ComplexObject()
{
ID = 1;
Classes = new ObservableCollection<ComplexSubObject>();
Classes.Add(new ComplexSubObject() { Name = "CustomFoo" });
Classes.Add(new ComplexSubObject() { Name = "My Other Foo" });
}
}
public class ComplexSubObject
{
public string Name { get; set; }
public ObservableCollection<SimpleValues> Types { get; set; }
public ComplexSubObject()
{
Types = new ObservableCollection<SimpleValues>();
Types.Add(new SimpleValues() { name = "foo", value = "bar" });
Types.Add(new SimpleValues() { name = "bar", value = "foo" });
}
}
public class SimpleValues
{
public string name { get; set; }
public string value { get; set; }
}
In order for the WPF PropertyGrid to be able to edit each item in the ObservableCollection, we need to provide a type descriptor for the collection which return the items as "Properties" of that collection so they can be edited. Because we cannot statically determine the items from a collection (as each collection has different number of elements), it means that the collection itself must be the TypeDescriptor, which means implementing ICustomTypeDescriptor.
(note that only GetProperties is important for our purposes, the rest just delegates to TypeDescriptor):
public class ExpandableObservableCollection<T> : ObservableCollection<T>,
ICustomTypeDescriptor
{
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
// Create a collection object to hold property descriptors
PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);
for (int i = 0; i < Count; i++)
{
pds.Add(new ItemPropertyDescriptor<T>(this, i));
}
return pds;
}
#region Use default TypeDescriptor stuff
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(this, noCustomTypeDesc: true);
}
string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(this, noCustomTypeDesc: true);
}
string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(this, noCustomTypeDesc: true);
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(this, noCustomTypeDesc: true);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, noCustomTypeDesc: true);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, noCustomTypeDesc: true);
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, noCustomTypeDesc: true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, noCustomTypeDesc: true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, noCustomTypeDesc: true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
return TypeDescriptor.GetProperties(this, attributes, noCustomTypeDesc: true);
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
#endregion
}
Additionally, we need an implementation of ItemPropertyDescriptor, which I provide here:
public class ItemPropertyDescriptor<T> : PropertyDescriptor
{
private readonly ObservableCollection<T> _owner;
private readonly int _index;
public ItemPropertyDescriptor(ObservableCollection<T> owner, int index)
: base("#" + index, null)
{
_owner = owner;
_index = index;
}
public override AttributeCollection Attributes
{
get
{
var attributes = TypeDescriptor.GetAttributes(GetValue(null), false);
if (!attributes.OfType<ExpandableObjectAttribute>().Any())
{
// copy all the attributes plus an extra one (the
// ExpandableObjectAttribute)
// this ensures that even if the type of the object itself doesn't have the
// ExpandableObjectAttribute, it will still be expandable.
var newAttributes = new Attribute[attributes.Count + 1];
attributes.CopyTo(newAttributes, newAttributes.Length - 1);
newAttributes[newAttributes.Length - 1] = new ExpandableObjectAttribute();
// overwrite the array
attributes = new AttributeCollection(newAttributes);
}
return attributes;
}
}
public override bool CanResetValue(object component)
{
return false;
}
public override object GetValue(object component)
{
return Value;
}
private T Value
=> _owner[_index];
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
_owner[_index] = (T)value;
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
public override Type ComponentType
=> _owner.GetType();
public override bool IsReadOnly
=> false;
public override Type PropertyType
=> Value?.GetType();
}
Which for the most part, just sets up reasonable defaults, which you can tweak to serve your needs.
One thing to note is that you may implement the Attributes property differently, depending on your use case. If you don't do the "add it to the attribute collection if it's not there", then you need to add the attribute to the classes/types that you want to expand; if you do keep that code in, then you'll be able to expand every item in the collection no matter if the class/type has the attribute or not.
It then becomes a matter of using ExpandableObservableCollection in place of ObservableCollection. This kind of sucks as it means your ViewModel has view-stuff-ish stuff in it, but ¯\_(ツ)_/¯.
Additionally, you need to add the ExpandableObjectAttribute to each of the properties that is a ExpandableObservableCollection.
Code Dump
If you're following along at home, you can use the following dialog code to run the example:
<Window x:Class="WpfDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfDemo"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<xctk:PropertyGrid x:Name="It" />
</Grid>
</Window>
-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace WpfDemo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
It.SelectedObject = new MainWindowViewModel().BindingComplexObject;
}
}
}
And here's the complete ViewModel implementation:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
namespace WpfDemo
{
public class MainWindowViewModel
{
/// <summary> This the object we want to be able to edit in the data grid. </summary>
public ComplexObject BindingComplexObject { get; set; }
public MainWindowViewModel()
{
BindingComplexObject = new ComplexObject();
}
}
[ExpandableObject]
public class ComplexObject
{
public int ID { get; set; }
[ExpandableObject]
public ExpandableObservableCollection<ComplexSubObject> Classes { get; set; }
public ComplexObject()
{
ID = 1;
Classes = new ExpandableObservableCollection<ComplexSubObject>();
Classes.Add(new ComplexSubObject() { Name = "CustomFoo" });
Classes.Add(new ComplexSubObject() { Name = "My Other Foo" });
}
}
[ExpandableObject]
public class ComplexSubObject
{
public string Name { get; set; }
[ExpandableObject]
public ExpandableObservableCollection<SimpleValues> Types { get; set; }
public ComplexSubObject()
{
Types = new ExpandableObservableCollection<SimpleValues>();
Types.Add(new SimpleValues() { name = "foo", value = "bar" });
Types.Add(new SimpleValues() { name = "bar", value = "foo" });
}
}
public class SimpleValues
{
public string name { get; set; }
public string value { get; set; }
}
public class ExpandableObservableCollection<T> : ObservableCollection<T>,
ICustomTypeDescriptor
{
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
// Create a collection object to hold property descriptors
PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null);
for (int i = 0; i < Count; i++)
{
pds.Add(new ItemPropertyDescriptor<T>(this, i));
}
return pds;
}
#region Use default TypeDescriptor stuff
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(this, noCustomTypeDesc: true);
}
string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(this, noCustomTypeDesc: true);
}
string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(this, noCustomTypeDesc: true);
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(this, noCustomTypeDesc: true);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, noCustomTypeDesc: true);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, noCustomTypeDesc: true);
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, noCustomTypeDesc: true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, noCustomTypeDesc: true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, noCustomTypeDesc: true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
return TypeDescriptor.GetProperties(this, attributes, noCustomTypeDesc: true);
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return this;
}
#endregion
}
public class ItemPropertyDescriptor<T> : PropertyDescriptor
{
private readonly ObservableCollection<T> _owner;
private readonly int _index;
public ItemPropertyDescriptor(ObservableCollection<T> owner, int index)
: base("#" + index, null)
{
_owner = owner;
_index = index;
}
public override AttributeCollection Attributes
{
get
{
var attributes = TypeDescriptor.GetAttributes(GetValue(null), false);
if (!attributes.OfType<ExpandableObjectAttribute>().Any())
{
// copy all the attributes plus an extra one (the
// ExpandableObjectAttribute)
// this ensures that even if the type of the object itself doesn't have the
// ExpandableObjectAttribute, it will still be expandable.
var newAttributes = new Attribute[attributes.Count + 1];
attributes.CopyTo(newAttributes, newAttributes.Length - 1);
newAttributes[newAttributes.Length - 1] = new ExpandableObjectAttribute();
// overwrite the original
attributes = new AttributeCollection(newAttributes);
}
return attributes;
}
}
public override bool CanResetValue(object component)
{
return false;
}
public override object GetValue(object component)
{
return Value;
}
private T Value
=> _owner[_index];
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
_owner[_index] = (T)value;
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
public override Type ComponentType
=> _owner.GetType();
public override bool IsReadOnly
=> false;
public override Type PropertyType
=> Value?.GetType();
}
}
MackieChan provided the major clues for this...
There is no need to inherit from ICustomTypeDescriptor as similar results can be achieved using type converters.
Firstly create an expandable object type converter and override the GetProperties method. For example, if you wish to maintain the index order of a generic IList type:
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
using System.ComponentModel;
public class MyExpandableIListConverter<T> : ExpandableObjectConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
if (value is IList<T> list)
{
PropertyDescriptorCollection propDescriptions = new PropertyDescriptorCollection(null);
IEnumerator enumerator = list.GetEnumerator();
int counter = -1;
while (enumerator.MoveNext())
{
counter++;
propDescriptions.Add(new ListItemPropertyDescriptor<T>(list, counter));
}
return propDescriptions;
}
else
{
return base.GetProperties(context, value, attributes);
}
}
}
With the ListItemPropertyDescriptor being defined as follows:
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
using System.ComponentModel;
public class ListItemPropertyDescriptor<T> : PropertyDescriptor
{
private readonly IList<T> owner;
private readonly int index;
public ListItemPropertyDescriptor(IList<T> owner, int index) : base($"[{index}]", null)
{
this.owner = owner;
this.index = index;
}
public override AttributeCollection Attributes
{
get
{
var attributes = TypeDescriptor.GetAttributes(GetValue(null), false);
//If the Xceed expandable object attribute is not applied then apply it
if (!attributes.OfType<ExpandableObjectAttribute>().Any())
{
attributes = AddAttribute(new ExpandableObjectAttribute(), attributes);
}
//set the xceed order attribute
attributes = AddAttribute(new PropertyOrderAttribute(index), attributes);
return attributes;
}
}
private AttributeCollection AddAttribute(Attribute newAttribute, AttributeCollection oldAttributes)
{
Attribute[] newAttributes = new Attribute[oldAttributes.Count + 1];
oldAttributes.CopyTo(newAttributes, 1);
newAttributes[0] = newAttribute;
return new AttributeCollection(newAttributes);
}
public override bool CanResetValue(object component)
{
return false;
}
public override object GetValue(object component)
{
return Value;
}
private T Value
=> owner[index];
public override void ResetValue(object component)
{
throw new NotImplementedException();
}
public override void SetValue(object component, object value)
{
owner[index] = (T)value;
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
public override Type ComponentType
=> owner.GetType();
public override bool IsReadOnly
=> false;
public override Type PropertyType
=> Value?.GetType();
}
Then you need to dynamically decorate the types you wish to display in the property grid with ExpandableObjectAttribute and TypeConverterAttribute. I create a 'decoration manager' to achieve this as follows.
using System.ComponentModel;
using Xceed.Wpf.Toolkit.PropertyGrid.Attributes;
public static class TypeDecorationManager
{
public static void AddExpandableObjectConverter(Type T)
{
TypeDescriptor.AddAttributes(T, new TypeConverterAttribute(typeof(ExpandableObjectConverter)));
TypeDescriptor.AddAttributes(T, new ExpandableObjectAttribute());
}
public static void AddExpandableIListConverter<I>(Type T)
{
TypeDescriptor.AddAttributes(T, new TypeConverterAttribute(typeof(MyExpandableIListConverter<I>)));
TypeDescriptor.AddAttributes(T, new ExpandableObjectAttribute());
}
}
Call AddExpandableObjectConverter for any type you would like to be expandable in the property grid and AddExpandableIListConverter for any IList type you would like to be expandable on the grid.
For example, if you have a curve object with some properties including an IList then all of the properties and list items can be made expandable as follows:
TypeDecorationManager.AddExpandableObjectConverter(typeof(Curve));
TypeDecorationManager.AddExpandableObjectConverter(typeof(CurvePoint));
AddCoreExpandableListConverter<CurvePoint>(typeof(IList<CurvePoint>));

Designing a custom PetaPoco mapper that supports HeadSprings Enumeration Class

I'm attempting to create a mapper so PetaPoco can hydrate and persist POCOs with Enumeration class properties. See more about Enumeration classes here or here.
For instance, Take this class.
public class PetType : Headspring.Enumeration<PetType>
{
public static readonly PetType Frog = new PetType(1, "Frog");
public static readonly PetType Cat = new PetType(2, "Cat");
public static readonly PetType Fish = new PetType(3, "Fish");
public static readonly PetType Dog = new PetType(4, "Dog");
private PetType(int value, string displayName) : base(value, displayName) { }
}
Which can be used like so:
var MyPet = PetType.Dog;
Here is the Poco I want to hydrate/persist with the database:
public class Pet
{
public int ID { get; set; }
public string OwnerName { get; set; }
public DateTime DateOfBirth { get; set; }
public string PetName{ get; set; }
public PetType PetType{ get; set; }
}
I have designed a custom mapper that will work with PetType:
class EnumClassMapper : PetaPoco.StandardMapper
{
public override Func<object, object> GetFromDbConverter(System.Reflection.PropertyInfo targetProperty, Type sourceType)
{
if (targetProperty.PropertyType == typeof(PetType))
{
return (x) => PetType.FromValue((int) x);
}
return base.GetFromDbConverter(targetProperty, sourceType);
}
public override Func<object, object> GetToDbConverter(System.Reflection.PropertyInfo sourceProperty)
{
if (sourceProperty.PropertyType == typeof(PetType))
{
return (x) => ((PetType)x).Value;
}
return base.GetToDbConverter(sourceProperty);
}
}
However suppose I create another Enumeration subclass for disposition.
public class Disposition: Headspring.Enumeration<Disposition>
{
public static readonly Friendly = new Disposition(1, "Friendly");
public static readonly Timid = new Disposition(2, "Timid");
public static readonly Aggressive = new Disposition(3, "Aggressive");
private Disposition(int value, string displayName) : base(value, displayName) { }
}
I don't want to have to update my mapper every time I create a new subclass of the Enumeration class. I prefer that the mapping code could recognize that the property type is a descendent of the Enumeration class, and map accordingly. I assume the answer is to make use of reflection, but I don't know how to proceed.
What about
public class EnumClassMapper<T> : PetaPoco.StandardMapper
where T : Headspring.Enumeration<T>
{
public override Func<object, object> GetFromDbConverter(System.Reflection.PropertyInfo targetProperty, Type sourceType)
{
return (x) => Enumeration<T, int>.FromValue((int) x);
}
public override Func<object, object> GetToDbConverter(System.Reflection.PropertyInfo sourceProperty)
{
return (x) => ((T)x).Value;
}
}
var builder = DatabaseConfiguration.Build()
.UsingConnectionStringName("sqlite")
.UsingDefaultMapper<ConventionMapper>(m =>
{
m.FromDbConverter = (targetProperty, sourceType) =>
{
if (targetProperty == null)
return null;
var t = targetProperty.PropertyType;
if (t.BaseType == null || ! t.BaseType.IsGenericType)
return null;
if (t.BaseType.GetGenericTypeDefinition() != typeof(Headspring.Enumeration<>))
return null;
return ((IMapper)Activator.CreateInstance(typeof(EnumClassMapper<>).MakeGenericType(t))).GetFromDbConverter(targetProperty, sourceType);
};
m.ToDbConverter = sourceProperty =>
{
if (sourceProperty == null)
return null;
var t = sourceProperty.PropertyType;
if (t.BaseType == null || !t.BaseType.IsGenericType)
return null;
if (t.BaseType.GetGenericTypeDefinition() != typeof(Headspring.Enumeration<>))
return null;
return ((IMapper)Activator.CreateInstance(typeof(EnumClassMapper<>).MakeGenericType(t))).GetToDbConverter(sourceProperty);
};
});
var db = builder.Create();

How can I create a Fluent NHibernate Convention that ignores properties that don't have setters

I'm looking for a FluentNH (Fluent NHibernate) convention or configuration that ignores all properties that have no setter:
It would still map these:
public class foo{
public virtual int bar {get; private set;}
}
And omit these:
public class foo{
public virtual int fizz{get;private set;}
public virtual int bar{get {return fizz;}} //<-------
}
You should use a custom mapping configuration
public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Member member)
{
return member.CanWrite;
}
}
Usage :
var nhConfiguration = new Configuration().Configure();
var mappingConfiguration = new DefaultMappingConfiguration();
var.fluentConfiguration = Fluently.Configure(nhConfiguration );
.Mappings(m => m.AutoMappings.Add(
AutoMap.AssemblyOf<MappedType>(mappingConfiguration)
));
var sessionFactory = this.fluentConfiguration.BuildSessionFactory();
However, private setters won't get mapped. You should get them as protected
Use this:
public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Member member)
{
if (member.IsProperty && !member.CanWrite)
{
return false;
}
return base.ShouldMap(member);
}
}
That should handle the case of no setter and private setter.
I know this is old question but code below do well with private setters.
public override bool ShouldMap(Member member)
{
var prop = member.DeclaringType.GetProperty(member.Name);
bool isPropertyToMap =
prop != null &&
prop.GetSetMethod(true) != null &&
member.IsProperty;
return
base.ShouldMap(member) && isPropertyToMap;
}
Another way is to use an attribute.
public class MyEntity
{
[NotMapped]
public bool A => true;
}
public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
public override bool ShouldMap(Member member)
{
if (member.MemberInfo.GetCustomAttributes(typeof(NotMappedAttribute), true).Length > 0)
{
return false;
}
return base.ShouldMap(member);
}
}

Categories