Serializing Generic class objects











up vote
0
down vote

favorite
1












I am trying to serialize and deserialize a List of Interface like List;
There are some generic classes that inherit this interface like below:



public class BasicQuery<T> : IQuery
{
public T Value{get;set;}
}


I have created several instances of BasicQuery for different datatypes and added to the list



List<IQuery> ListOfQueries = new List<IQuery>();
BasicQuery<int> q1 = new BasicQuery<int>(); q1.Value = 100;
ListOfQueries.Add(q1);
BasicQuery<string> q2 = new BasicQuery<string>(); q2.Value="Test";
ListOfQueries.Add(q2);


My question is how to serialize and deserialize the List of interfaces to xml which is ineherited by generic child classes.
I searched around and found one very useful link https://www.codeproject.com/Articles/38930/XML-Serialization-of-a-Class-Inherited-from-Generi?msg=3159064#xx3159064xx .
But that still do not solve my problem. Please suggest.










share|improve this question






















  • You can't serialize a member whose declared type is an interface, or a collection of values whose declared type is an interface. For confirmation and alternatives, see XML serialization of interface property. You can, however, serialize a class hierarchy when the type being serialized is abstract, as long as it is not an interface. For how, see Using XmlSerializer to serialize derived classes.
    – dbc
    Nov 22 at 17:57












  • In that case, also can you serialize and deserialize generic class types, as in my code.
    – sudeep kushwaha
    Nov 23 at 13:31










  • Yes you can serialize generics, but if the generic parameter is an interface it will not work if there are any serializable members or values of the same type, e.g. if T is an interface ISomeInterface then BasicQuery<ISomeInterface> will not be serializable by XmlSerializer, because public public ISomeInterface Value will not be serializable. But to give a full answer to your question I would need to see a Minimal, Complete, and Verifiable example. As it is your question is missing the definition for IQuery and so doesn't compile or reproduce your problem.
    – dbc
    Nov 26 at 20:59












  • IQuery could be any random interface, lets say public Interface IQuery { string Name{get;set;} string Description{get;set} }
    – sudeep kushwaha
    Nov 28 at 10:38

















up vote
0
down vote

favorite
1












I am trying to serialize and deserialize a List of Interface like List;
There are some generic classes that inherit this interface like below:



public class BasicQuery<T> : IQuery
{
public T Value{get;set;}
}


I have created several instances of BasicQuery for different datatypes and added to the list



List<IQuery> ListOfQueries = new List<IQuery>();
BasicQuery<int> q1 = new BasicQuery<int>(); q1.Value = 100;
ListOfQueries.Add(q1);
BasicQuery<string> q2 = new BasicQuery<string>(); q2.Value="Test";
ListOfQueries.Add(q2);


My question is how to serialize and deserialize the List of interfaces to xml which is ineherited by generic child classes.
I searched around and found one very useful link https://www.codeproject.com/Articles/38930/XML-Serialization-of-a-Class-Inherited-from-Generi?msg=3159064#xx3159064xx .
But that still do not solve my problem. Please suggest.










share|improve this question






















  • You can't serialize a member whose declared type is an interface, or a collection of values whose declared type is an interface. For confirmation and alternatives, see XML serialization of interface property. You can, however, serialize a class hierarchy when the type being serialized is abstract, as long as it is not an interface. For how, see Using XmlSerializer to serialize derived classes.
    – dbc
    Nov 22 at 17:57












  • In that case, also can you serialize and deserialize generic class types, as in my code.
    – sudeep kushwaha
    Nov 23 at 13:31










  • Yes you can serialize generics, but if the generic parameter is an interface it will not work if there are any serializable members or values of the same type, e.g. if T is an interface ISomeInterface then BasicQuery<ISomeInterface> will not be serializable by XmlSerializer, because public public ISomeInterface Value will not be serializable. But to give a full answer to your question I would need to see a Minimal, Complete, and Verifiable example. As it is your question is missing the definition for IQuery and so doesn't compile or reproduce your problem.
    – dbc
    Nov 26 at 20:59












  • IQuery could be any random interface, lets say public Interface IQuery { string Name{get;set;} string Description{get;set} }
    – sudeep kushwaha
    Nov 28 at 10:38















up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





I am trying to serialize and deserialize a List of Interface like List;
There are some generic classes that inherit this interface like below:



public class BasicQuery<T> : IQuery
{
public T Value{get;set;}
}


I have created several instances of BasicQuery for different datatypes and added to the list



List<IQuery> ListOfQueries = new List<IQuery>();
BasicQuery<int> q1 = new BasicQuery<int>(); q1.Value = 100;
ListOfQueries.Add(q1);
BasicQuery<string> q2 = new BasicQuery<string>(); q2.Value="Test";
ListOfQueries.Add(q2);


My question is how to serialize and deserialize the List of interfaces to xml which is ineherited by generic child classes.
I searched around and found one very useful link https://www.codeproject.com/Articles/38930/XML-Serialization-of-a-Class-Inherited-from-Generi?msg=3159064#xx3159064xx .
But that still do not solve my problem. Please suggest.










share|improve this question













I am trying to serialize and deserialize a List of Interface like List;
There are some generic classes that inherit this interface like below:



public class BasicQuery<T> : IQuery
{
public T Value{get;set;}
}


I have created several instances of BasicQuery for different datatypes and added to the list



List<IQuery> ListOfQueries = new List<IQuery>();
BasicQuery<int> q1 = new BasicQuery<int>(); q1.Value = 100;
ListOfQueries.Add(q1);
BasicQuery<string> q2 = new BasicQuery<string>(); q2.Value="Test";
ListOfQueries.Add(q2);


My question is how to serialize and deserialize the List of interfaces to xml which is ineherited by generic child classes.
I searched around and found one very useful link https://www.codeproject.com/Articles/38930/XML-Serialization-of-a-Class-Inherited-from-Generi?msg=3159064#xx3159064xx .
But that still do not solve my problem. Please suggest.







c# xml xml-serialization xml-deserialization system.reflection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 12:43









sudeep kushwaha

2817




2817












  • You can't serialize a member whose declared type is an interface, or a collection of values whose declared type is an interface. For confirmation and alternatives, see XML serialization of interface property. You can, however, serialize a class hierarchy when the type being serialized is abstract, as long as it is not an interface. For how, see Using XmlSerializer to serialize derived classes.
    – dbc
    Nov 22 at 17:57












  • In that case, also can you serialize and deserialize generic class types, as in my code.
    – sudeep kushwaha
    Nov 23 at 13:31










  • Yes you can serialize generics, but if the generic parameter is an interface it will not work if there are any serializable members or values of the same type, e.g. if T is an interface ISomeInterface then BasicQuery<ISomeInterface> will not be serializable by XmlSerializer, because public public ISomeInterface Value will not be serializable. But to give a full answer to your question I would need to see a Minimal, Complete, and Verifiable example. As it is your question is missing the definition for IQuery and so doesn't compile or reproduce your problem.
    – dbc
    Nov 26 at 20:59












  • IQuery could be any random interface, lets say public Interface IQuery { string Name{get;set;} string Description{get;set} }
    – sudeep kushwaha
    Nov 28 at 10:38




















  • You can't serialize a member whose declared type is an interface, or a collection of values whose declared type is an interface. For confirmation and alternatives, see XML serialization of interface property. You can, however, serialize a class hierarchy when the type being serialized is abstract, as long as it is not an interface. For how, see Using XmlSerializer to serialize derived classes.
    – dbc
    Nov 22 at 17:57












  • In that case, also can you serialize and deserialize generic class types, as in my code.
    – sudeep kushwaha
    Nov 23 at 13:31










  • Yes you can serialize generics, but if the generic parameter is an interface it will not work if there are any serializable members or values of the same type, e.g. if T is an interface ISomeInterface then BasicQuery<ISomeInterface> will not be serializable by XmlSerializer, because public public ISomeInterface Value will not be serializable. But to give a full answer to your question I would need to see a Minimal, Complete, and Verifiable example. As it is your question is missing the definition for IQuery and so doesn't compile or reproduce your problem.
    – dbc
    Nov 26 at 20:59












  • IQuery could be any random interface, lets say public Interface IQuery { string Name{get;set;} string Description{get;set} }
    – sudeep kushwaha
    Nov 28 at 10:38


















You can't serialize a member whose declared type is an interface, or a collection of values whose declared type is an interface. For confirmation and alternatives, see XML serialization of interface property. You can, however, serialize a class hierarchy when the type being serialized is abstract, as long as it is not an interface. For how, see Using XmlSerializer to serialize derived classes.
– dbc
Nov 22 at 17:57






You can't serialize a member whose declared type is an interface, or a collection of values whose declared type is an interface. For confirmation and alternatives, see XML serialization of interface property. You can, however, serialize a class hierarchy when the type being serialized is abstract, as long as it is not an interface. For how, see Using XmlSerializer to serialize derived classes.
– dbc
Nov 22 at 17:57














In that case, also can you serialize and deserialize generic class types, as in my code.
– sudeep kushwaha
Nov 23 at 13:31




In that case, also can you serialize and deserialize generic class types, as in my code.
– sudeep kushwaha
Nov 23 at 13:31












Yes you can serialize generics, but if the generic parameter is an interface it will not work if there are any serializable members or values of the same type, e.g. if T is an interface ISomeInterface then BasicQuery<ISomeInterface> will not be serializable by XmlSerializer, because public public ISomeInterface Value will not be serializable. But to give a full answer to your question I would need to see a Minimal, Complete, and Verifiable example. As it is your question is missing the definition for IQuery and so doesn't compile or reproduce your problem.
– dbc
Nov 26 at 20:59






Yes you can serialize generics, but if the generic parameter is an interface it will not work if there are any serializable members or values of the same type, e.g. if T is an interface ISomeInterface then BasicQuery<ISomeInterface> will not be serializable by XmlSerializer, because public public ISomeInterface Value will not be serializable. But to give a full answer to your question I would need to see a Minimal, Complete, and Verifiable example. As it is your question is missing the definition for IQuery and so doesn't compile or reproduce your problem.
– dbc
Nov 26 at 20:59














IQuery could be any random interface, lets say public Interface IQuery { string Name{get;set;} string Description{get;set} }
– sudeep kushwaha
Nov 28 at 10:38






IQuery could be any random interface, lets say public Interface IQuery { string Name{get;set;} string Description{get;set} }
– sudeep kushwaha
Nov 28 at 10:38



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431308%2fserializing-generic-class-objects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431308%2fserializing-generic-class-objects%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks