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 13:01:32
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
06/11/2010 12:57:21
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:
01488465
Vues:
36
>>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 ?

LocationEntity is a standard class with properties. The beginning looks like this.
using System;
using System.ComponentModel;

using OakLeaf.MM.Main;

namespace BTSystems.PantryWare.Business
{
/// <summary>
/// Summary description for LocationEntity.
/// </summary>
[Serializable]
public partial class LocationEntity : ABusinessEntity
{
	/// <summary>
	/// AddressNumber
	/// </summary>
	public string AddressNumber
	{
		get
		{
			if (this.Row != null)
				return (string)mmType.GetNullableDbValue(this.Row["AddressNumber"]);
			else
				return this._addressNumber;
		}
		set
		{
			if (this.Row != null)
				this.Row["AddressNumber"] = mmType.SetNullableDbValue(value); 
			this._addressNumber = value;
			this.OnPropertyChanged(new PropertyChangedEventArgs("AddressNumber"));
		}
	}
	private string _addressNumber;
At the moment I am not getting to see the exception because it is getting caught deeper somewhere. I would have to test this outside of the application or find a location that would allow it. I was hoping there would be somthing obvious. What else does a class need to be serializable?
Tim
Timothy Bryan
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform