Private properties/classes and attributes from System.Attribute/ComponentModel
See original GitHub issueHello 😃
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:
- How to serialize only settable and public properties?
- How to manipulate with property attributes (ignoring/selecting only)?
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you. Very, very much. The problem has been solved. You are the god 😃!
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 nextSystem.ArgumentException
😃 Classes/interfaces:and the call: