question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Private properties/classes and attributes from System.Attribute/ComponentModel

See original GitHub issue

Hello 😃

I am trying to serialize/deserialize for example System.Windows.Forms.Button as below:

Button btn = new Button();
var contents =
	new ConfigurationContainer()
		.Create()
	.Serialize(new XmlWriterSettings { Indent = true }, btn);

This causes an System.InvalidOperationException with:

The serializer for type 'System.Drawing.Font' could not be found.
Please ensure that the type is a valid type can be activated.
The default behavior requires an empty public constructor on the (non-abstract) class to activate.

OK. Some classes have no parameterless constuctor. Let’s build an empty serializers to prevent Cursor/Font to throw an exception:

public class CursorSerializer : IExtendedXmlCustomSerializer<Cursor>
{
	public Cursor Deserialize(XElement xElement) => null;
	public void Serializer(XmlWriter xmlWriter, Cursor obj) { }
}
public class FontSerializer : IExtendedXmlCustomSerializer<Font>
{
	public Font Deserialize(XElement xElement) => null;
	public void Serializer(XmlWriter xmlWriter, Font obj) { }
}

and extend the call:

Button btn = new Button();
var contents =
	new ConfigurationContainer()
		.Type<Cursor>().CustomSerializer(new CursorSerializer())
		.Type<Font>().CustomSerializer(new FontSerializer())
		.Create()
	.Serialize(new XmlWriterSettings { Indent = true }, btn);

This causes an System.InvalidOperationException with:

The serializer for type 'System.Windows.Forms.Control+ControlNativeWindow' could not be found.
Please ensure that the type is a valid type can be activated.
The default behavior requires an empty public constructor on the (non-abstract) class to activate.

The struct/class System.Windows.Forms.Control+ControlNativeWindow is not visible and I don’t know how to prevent the exception. Let’s extend the Button class with property with attribute:

[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
public sealed class ButtonExPropertyAttribute : Attribute { }

public class ButtonEx : Button
{
	[ButtonExPropertyAttribute]
	public int MyProperty { get; set; }
}

My questions are:

  1. How to serialize only settable and public properties?
  2. How to manipulate with property attributes (ignoring/selecting only)?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
gimi87commented, Aug 29, 2018

Thank you. Very, very much. The problem has been solved. You are the god 😃!

1reaction
gimi87commented, Aug 29, 2018

Thank you for your patience. Yes… it can be a workaround, but not a good solution, because I have to remember to Ignore modified properties. Look… I prepared a sketch where you can look at the next System.ArgumentException 😃 Classes/interfaces:

//Interface implementations
public interface IProperty { int MyProperty { get; set; } int RefID { get; set; } }
public class PropertyBase : IProperty { public virtual int MyProperty { get; set; } = 1; public int RefID { get; set; } }
public class PropertyInherit : PropertyBase { public override int MyProperty { get; set; } = 2; }

//Class-handles to interface
public class IPropertyHandleBase
{
	public IProperty HandleToIProperty { get; set; }
}
public class PropertyBaseHandle : IPropertyHandleBase
{
	public new PropertyBase HandleToIProperty { get => base.HandleToIProperty as PropertyBase; set => base.HandleToIProperty = value; }
}
public class PropertyInheritHandle : PropertyBaseHandle
{
	public new PropertyInherit HandleToIProperty { get => base.HandleToIProperty as PropertyInherit; set => base.HandleToIProperty = value; }
}

//Object to serialize/deserialize
public class TestIProperty
{
	public List<IProperty> MyProperties { get; set; } = new List<IProperty>();
	public List<IPropertyHandleBase> MyHandles { get; set; } = new List<IPropertyHandleBase>();
}

and the call:

PropertyBase prop1 = new PropertyBase { RefID = 1 };
PropertyInherit prop2 = new PropertyInherit { RefID = 2 };
PropertyBaseHandle hand1 = new PropertyBaseHandle { HandleToIProperty = prop1 };
PropertyInheritHandle hand2 = new PropertyInheritHandle { HandleToIProperty = prop2 };

TestIProperty obj = new TestIProperty();
obj.MyProperties.Add(prop1);
obj.MyProperties.Add(prop2);
obj.MyHandles.Add(hand1);
obj.MyHandles.Add(hand2);

var contents =
	new ConfigurationContainer().Extend(FallbackSerializationExtension.Default)
		.Type<IProperty>().EnableReferences(p => p.RefID)
		.Create()
	.Serialize(new XmlWriterSettings { Indent = true }, obj);
Read more comments on GitHub >

github_iconTop Results From Across the Web

PropertyTabAttribute Class (System.ComponentModel)
Initializes a new instance of the PropertyTabAttribute class using the specified type of tab and tab scope. Properties. TabClasses. Gets the types of...
Read more >
c# - Define a custom Attribute class to make a property ...
I Have some classes in my project, some properties are Browsable(false) so the user couldn't see them: public class OrderEntity{ public int Id...
Read more >
DataAnnotations In Depth
The namespace System.ComponentModel.DataAnnotations, has a group of classes, attributes and methods, to make validations in our .
Read more >
DataAnnotations - NotMapped Attribute in EF 6 & EF Core
The NotMapped attribute can be applied to properties of an entity class for which we do not want to create corresponding columns in...
Read more >
How to use data annotations in C# | InfoWorld
Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found