Mark field in child class as NonSerialized
-
Hi, as the title indicates I want to mark a field in a child class with a NonSerialized attribute so that it won't be serialized. Simply put:
[Serializable]
class Parent
{
protected int SomeField;
}[Serializable]
class Child : Parent
{
[NonSerialized]
protected int SomeField;
}Of course this does not work, but also the new keyword does not help me because it only hides the field and does not overwrite it. Is there any other way to achieve the desired effect? Regards Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
-
Hi, as the title indicates I want to mark a field in a child class with a NonSerialized attribute so that it won't be serialized. Simply put:
[Serializable]
class Parent
{
protected int SomeField;
}[Serializable]
class Child : Parent
{
[NonSerialized]
protected int SomeField;
}Of course this does not work, but also the new keyword does not help me because it only hides the field and does not overwrite it. Is there any other way to achieve the desired effect? Regards Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
-
Hi, of course you are right, but I need this as the parent class saves information in this field which the child classes do not need anymore because they compute it dynamically. And of course we are not talking about an int field, but about an array of a more complex data type which would consume up to 20 or 30 KB per instance which would be serialized and "thrown away" after deserialization as it has to be recomputed anyways. I hope this helps to better understand the purpose of what I want to achieve. Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
-
Hi, of course you are right, but I need this as the parent class saves information in this field which the child classes do not need anymore because they compute it dynamically. And of course we are not talking about an int field, but about an array of a more complex data type which would consume up to 20 or 30 KB per instance which would be serialized and "thrown away" after deserialization as it has to be recomputed anyways. I hope this helps to better understand the purpose of what I want to achieve. Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
If you don't want to store it, but on deserialization want to get default values. Then maybe the OnDeserializedAttribute can be of use... http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx
-
If you don't want to store it, but on deserialization want to get default values. Then maybe the OnDeserializedAttribute can be of use... http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx
Thanks for the link, this seems quite useful. But this does not solve my problem: the parent class has to save the field whereas the child classes don't have to save it, so how do I declare the field in the child classes? As I said I cannot mark it as NonSerialized in the parent class as it needs to be saved there. Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
-
Thanks for the link, this seems quite useful. But this does not solve my problem: the parent class has to save the field whereas the child classes don't have to save it, so how do I declare the field in the child classes? As I said I cannot mark it as NonSerialized in the parent class as it needs to be saved there. Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
maybe something like this could be usefull?! [OnDeserializing()] internal void OnDeserializingMethod(StreamingContext context) { this.nonserializedmember = reInitialise(); foreach (Child c in this.childs) { child.newNonserializedValue = this.nonserializedmember; } }
-
Thanks for the link, this seems quite useful. But this does not solve my problem: the parent class has to save the field whereas the child classes don't have to save it, so how do I declare the field in the child classes? As I said I cannot mark it as NonSerialized in the parent class as it needs to be saved there. Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
I just answered this question myself :). I just had to use OnSerializing and OnSerialized to set the field to null and reset it to its old value respectively. Thank you very much for your help. Regards, Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again
-
I just answered this question myself :). I just had to use OnSerializing and OnSerialized to set the field to null and reset it to its old value respectively. Thank you very much for your help. Regards, Dust Signs
The number you dialed is imaginary. Please turn your phone by 90 degrees and try again