2011-10-07 22 views
39

tarihinde NonSerialized aşağıdaki hatayı alıyorum bumülkiyet

[XmlIgnore] 
[NonSerialized] 
public List<string> paramFiles { get; set; } 

gibi kod: Ben

[field: NonSerialized] 

aşağıdaki uyarıyı almak yazarsanız

Attribute 'NonSerialized' is not valid on this declaration type. 
It is only valid on 'field' declarations. 


'field' is not a valid attribute location for this declaration. 
Valid attribute locations for this declaration are 'property'. 
All attributes in this block will be ignored. 


Ben

[property: NonSerialized] 

yazarsanız alıyorum (tekrar) aşağıdaki hata:

Attribute 'NonSerialized' is not valid on this declaration type. 
It is only valid on 'field' declarations. 


nasıl bir özellikte [NonSerialized] kullanabilir?

[XmlIgnore] 
[ScriptIgnore] 
public List<string> paramFiles { get; set; } 

:

+0

Olası kopyası [Özellik, json için seri hale getirilemez olarak nasıl işaretlenir?] (Http://stackoverflow.com/questions/5103200/how-to-mark-a-property-as-non-serializable-for- json) –

cevap

42

iyi ... İlk hata i arka sahadan

public List<string> paramFiles { get { return list;} set { list = value; } } 
[NonSerialized] 
private List<string> list; 
+9

Önerilen destek alanını kullanmak işe yaramıyor gibi görünüyor. Mülkiyet hala serileştiriliyor. –

+0

Evet, ancak AaaSerializer adlı her sınıfta çalışmıyor. Hangi serializer kullanıyorsunuz? – wiero

+2

Doğru öznitelik [NonSerializable] değil, [NonSerializable] değil –

52

Basit kullanımı kullanmanızı öneririz http://msdn.microsoft.com/en-us/library/system.nonserializedattribute.aspx

[AttributeUsageAttribute(AttributeTargets.Field, Inherited = false)] 
[ComVisibleAttribute(true)] 
public sealed class NonSerializedAttribute : Attribute 

den .. yapamazsın o anlatmak Umarım, yardımcı olur.

+2

Gerçekten de bu, klasik ASP.NET Web Hizmeti senaryosunda tamamen kullanıcı ismini (WCF öncesi projede sıkışmış olanlar için harika bir numara) gizleyerek çalışmaktadır. – timmi4sa

+0

Mükemmel – Mik1893

+0

Bu işi yapmak için [ScriptIgnore] –