Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Copy a class to avoid the referenced object going away
Message
De
06/11/2010 14:01:01
 
 
À
06/11/2010 13:57:49
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
Divers
Thread ID:
01488462
Message ID:
01488473
Vues:
27
>>>>>>>Hi All,
>>>>>>>
>>>>>>>I am trying to build a list of objects but if the underlying referenced object changes or goes away I have problems. To avoid this I am trying some code I found on c-shaprcorner to copy an object. I have tried two different versions, but both blow up on the third line and I am unable at the moment to find why. The objects passed in appear to be valid and the entity object has the [serializable] attribute on it.
>>>>>>>
>>>>>>>Option One
>>>>>>>
>>>>>>>public static LocationEntity MakeEntityCopy(LocationEntity entityToIsolate)
>>>>>>>{
>>>>>>>	MemoryStream stream = new MemoryStream();
>>>>>>>	IFormatter formatter = new BinaryFormatter();
>>>>>>>	formatter.Serialize(stream, entityToIsolate);
>>>>>>>	stream.Seek(0, 0);
>>>>>>>	LocationEntity IsolatedEntity = (LocationEntity)formatter.Deserialize(stream);
>>>>>>>	stream.Close();
>>>>>>>	return IsolatedEntity;
>>>>>>>}
>>>>>>>
>>>>>>>
>>>>>>>Option Two
>>>>>>>
>>>>>>>public static LocationEntity MakeEntityCopy(LocationEntity entityToIsolate)
>>>>>>>{
>>>>>>>	MemoryStream stream = new MemoryStream();
>>>>>>>	BinaryFormatter bf = new BinaryFormatter();
>>>>>>>	bf.Serialize(stream, entityToIsolate);
>>>>>>>	stream.Position = 0;
>>>>>>>	LocationEntity IsolatedEntity = (LocationEntity)bf.Deserialize(stream);
>>>>>>>	stream.Close();
>>>>>>>	return IsolatedEntity;
>>>>>>>}
>>>>>>>
>>>>>>>
>>>>>>>Is there anything obvious I am missing?
>>>>>>
>>>>>>What is LocationEntity ? Simply marking it as serializable doesn't neccessarily make it so .....
>>>>>>And what's the exact exception ?
>>>>>
>>>>>
>>>>>OK, stupid me. I wrapped the call in a try catch and this is the error
>>>>>Type 'BTSystems.PantryWare.Business.ABusinessEntity' in Assembly 'BTSystems.PantryWare.Business, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
>>>>>
>>>>>What is needed for the class to be Serializable other than the attribute?
>>>>
>>>>The serializer will try to serialize the complete object tree. That means that all properties of the class must be serializable. Which, for example, means that if a property holds a reference to another object the serializer will attempt to serialize that as well. Look carefully at the members of the class you are trying to serialize. If there are any properties that are not essential then mark them as non-serializable. If there are items that are essential but are not natively serializable then you can implement your own SerializationSurrogate to handle them.
>>>
>>>Ok, This may mean I am out of gas on this effort. The ABusinessEntity from which this inherits doesn't have anything in it, but it inherits from the mmBusinessEntity class which is also marked as serializable but has several additional properties. I can't really adjust that. I have serialied entities before without this issue, but I may have to look back on that code to see what I was doing different.
>>
>>So LocationEntity is derived from ABusinessEntity which, in turn, is derived from mmBusinessEntity and the last is marked as serializable? So you could try marknig ABusinessEntity as serializable. Unless that (or LocationEntity itself) has added properties that are *not* serializable then it should work - but you might end up with a pretty hefty clone (g)
>
>
>Marking the ABusinessEntity as Serializable did the trick. There is nothing in that class but it is the place where I could add functionality to all entities at an application level. Since Entities are so supposed to be lightweight (s), there hopefully isnt' anything heavy in there. I am not sure since that is required the template created entity objects that are already marked as serializable and the base class was, why the generated Application level doesn't have that by default. Now I know to add it.
>Thanks for your help. I appreciate it greatly

I guess the compiler can't assume that a derived class is serializable simply because the parent class is.....
Anyway glad you got it working.......
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform