Passing to the interface, not excluding form members

I am developing a C # application that has an interface declared in the form:

public partial class frmProjectForm : Form, IProjectInfo { } public interface IProjectInfo { string ProjectName { get; set; } string ProjectTitle { get; set; } string ProjectDescription { get; set; } string ProjectAuthor { get; set; } string ProjectCopyright { get; set; } string ProjectTrademark { get; set; } string ProjectCreationDate { get; set; } string ProjectVersion { get; set; } string ProjectFileName { get; } string ProjectFilePath { get; } string FullProjectPath { get; } } 

Somewhere in the application I want to do this:

 frmProjectForm f = new frmProjectForm(); f.ShowDialog(this); ... ... IProjectInfo getProjectInfoMembersOnly = (IProjectInfo)f; // persist the IProjectInfo members to JSON string project = JsonConvert.SerializeObject( getProjectInfoMembersOnly ); 

The JsonConvert.SerializeObject method explodes and complains about some self-regulation loop on the Button control. The casting operation does not exclude the form, and I do not know why, because I just want the interface cast to return only the interface members.

What am I doing wrong?

+4
source share
2 answers

It seems normal to me that the serialization code will work on the actual type of the object, and not on the type passed as a parameter (it is even more likely that the function accepts a parameter of type object , and is not a general function of SerializeObject<T>(T obj) ).

If you want your object to be sliced, I'm afraid you will have to cut it yourself by creating an object that implements the interface and only copying its elements.

 public class ProjectInfo : IProjectInfo { public string ProjectName { get; set; } public string ProjectTitle { get; set; } public string ProjectDescription { get; set; } public string ProjectAuthor { get; set; } public string ProjectCopyright { get; set; } public string ProjectTrademark { get; set; } public string ProjectCreationDate { get; set; } public string ProjectVersion { get; set; } public string ProjectFileName { get; private set; } public string ProjectFilePath { get; private set; } public string FullProjectPath { get; private set; } public ProjectInfo(IProjectInfo src) { this.ProjectName = src.ProjectName; this.ProjectTitle = src.ProjectTitle; this.ProjectDescription = src.ProjectDescription; this.ProjectAuthor = src.ProjectAuthor; this.ProjectCopyright = src.ProjectCopyright; this.ProjectTrademark = src.ProjectTrademark; this.ProjectCreationDate = src.ProjectCreationDate; this.ProjectVersion = src.ProjectVersion; this.ProjectFileName = src.ProjectFileName; this.ProjectFilePath = src.ProjectFilePath; this.FullProjectPath = src.FullProjectPath; } } 

And then you can use it as follows:

 frmProjectForm f = new frmProjectForm(); f.ShowDialog(this); ... ... IProjectInfo getProjectInfoMembersOnly = new ProjectInfo(f); //I only changed this line // persist the IProjectInfo members to JSON string project = JsonConvert.SerializeObject( getProjectInfoMembersOnly ); 
+1
source

If you use only the interface for serialization (it is not clear from your question), you do not need an interface at all. JSON.NET provides attributes for marking fields to be serialized. Even if you use the interface for something else, there is no reason why you could not define your class as follows:

 // Use optin so you don't have to worry about fields generated by the designer. [JsonObject(MemberSerialization.OptIn)] public partial class frmProjectForm : Form { [JsonProperty] public string ProjectName { get; set; } [JsonProperty] public string ProjectTitle { get; set; } [JsonProperty] public string ProjectDescription { get; set; } [JsonProperty] public string ProjectAuthor { get; set; } [JsonProperty] public string ProjectCopyright { get; set; } [JsonProperty] public string ProjectTrademark { get; set; } [JsonProperty] public string ProjectCreationDate { get; set; } [JsonProperty] public string ProjectVersion { get; set; } [JsonProperty] public string ProjectFileName { get; } [JsonProperty] public string ProjectFilePath { get; } [JsonProperty] public string FullProjectPath { get; } // . . . Other properties, buttons, and form fields } 

Then, when you call JSONConvert.SerializeObject() , the fields will be marked as JsonProperty .

+1
source

Source: https://habr.com/ru/post/1499127/


All Articles