NonSerializedAttribute Sınıf
Tanım
Seri hale getirilebilir bir sınıf alanının serileştirilmeyeceğini belirtir.Indicates that a field of a serializable class should not be serialized. Bu sınıf devralınamaz.This class cannot be inherited.
public ref class NonSerializedAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Field, Inherited=false)]
public sealed class NonSerializedAttribute : Attribute
public sealed class NonSerializedAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field, Inherited=true)]
public sealed class NonSerializedAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Field, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class NonSerializedAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Field, Inherited=false)>]
type NonSerializedAttribute = class
inherit Attribute
type NonSerializedAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Field, Inherited=true)>]
type NonSerializedAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Field, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type NonSerializedAttribute = class
inherit Attribute
Public NotInheritable Class NonSerializedAttribute
Inherits Attribute
- Devralma
- Öznitelikler
Örnekler
Aşağıdaki örnek, özniteliğiyle işaretlenmiş bir nesnenin serileştirmesini SerializableAttribute ve serileştirilmiş nesne içinde ile işaretlenmiş bir alanın davranışını gösterir NonSerializedAttribute .The following example demonstrates serialization of an object marked with the SerializableAttribute attribute, and the behavior of a field marked with the NonSerializedAttribute in the serialized object.
Not
Kod, SoapFormatter nesneyi seri hale getirmek için sınıfını kullanır.The code uses the SoapFormatter class to serialize the object. Sınıfı, bir proje için varsayılan olarak yüklenmeyen system.runtime.serialization.formatters.soap.dll bulunur.The class is found in the system.runtime.serialization.formatters.soap.dll, which is not loaded by default into a project. Kodu çalıştırmak için projenize DLL başvurusu eklemeniz gerekir.To run the code, you must add a reference to the DLL to your project.
#using <system.dll>
#using <system.messaging.dll>
#using <System.Runtime.Serialization.Formatters.Soap.dll>
using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Soap;
// A test object that needs to be serialized.
[Serializable]
ref class TestSimpleObject
{
private:
int member1;
String^ member2;
String^ member3;
double member4;
public:
// A field that is not serialized.
[NonSerialized]
String^ member5;
TestSimpleObject()
{
member1 = 11;
member2 = "hello";
member3 = "hello";
member4 = 3.14159265;
member5 = "hello world!";
}
void Print()
{
Console::WriteLine( "member1 = ' {0}'", member1 );
Console::WriteLine( "member2 = ' {0}'", member2 );
Console::WriteLine( "member3 = ' {0}'", member3 );
Console::WriteLine( "member4 = ' {0}'", member4 );
Console::WriteLine( "member5 = ' {0}'", member5 );
}
};
int main()
{
// Creates a new TestSimpleObject object.
TestSimpleObject^ obj = gcnew TestSimpleObject;
Console::WriteLine( "Before serialization the Object* contains: " );
obj->Print();
// Opens a file and serializes the object into it in binary format.
Stream^ stream = File::Open( "data.xml", FileMode::Create );
SoapFormatter^ formatter = gcnew SoapFormatter;
//BinaryFormatter* formatter = new BinaryFormatter();
formatter->Serialize( stream, obj );
stream->Close();
// Empties obj.
obj = nullptr;
// Opens file S"data.xml" and deserializes the object from it.
stream = File::Open( "data.xml", FileMode::Open );
formatter = gcnew SoapFormatter;
//formatter = new BinaryFormatter();
obj = dynamic_cast<TestSimpleObject^>(formatter->Deserialize( stream ));
stream->Close();
Console::WriteLine( "" );
Console::WriteLine( "After deserialization the object contains: " );
obj->Print();
}
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
//using System.Runtime.Serialization.Formatters.Binary;
public class Test {
public static void Main() {
// Creates a new TestSimpleObject object.
TestSimpleObject obj = new TestSimpleObject();
Console.WriteLine("Before serialization the object contains: ");
obj.Print();
// Opens a file and serializes the object into it in binary format.
Stream stream = File.Open("data.xml", FileMode.Create);
SoapFormatter formatter = new SoapFormatter();
//BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Close();
// Empties obj.
obj = null;
// Opens file "data.xml" and deserializes the object from it.
stream = File.Open("data.xml", FileMode.Open);
formatter = new SoapFormatter();
//formatter = new BinaryFormatter();
obj = (TestSimpleObject)formatter.Deserialize(stream);
stream.Close();
Console.WriteLine("");
Console.WriteLine("After deserialization the object contains: ");
obj.Print();
}
}
// A test object that needs to be serialized.
[Serializable()]
public class TestSimpleObject {
public int member1;
public string member2;
public string member3;
public double member4;
// A field that is not serialized.
[NonSerialized()] public string member5;
public TestSimpleObject() {
member1 = 11;
member2 = "hello";
member3 = "hello";
member4 = 3.14159265;
member5 = "hello world!";
}
public void Print() {
Console.WriteLine("member1 = '{0}'", member1);
Console.WriteLine("member2 = '{0}'", member2);
Console.WriteLine("member3 = '{0}'", member3);
Console.WriteLine("member4 = '{0}'", member4);
Console.WriteLine("member5 = '{0}'", member5);
}
}
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
Public Class Test
Public Shared Sub Main()
' Creates a new TestSimpleObject object.
Dim obj As New TestSimpleObject()
Console.WriteLine("Before serialization the object contains: ")
obj.Print()
' Opens a file and serializes the object into it in binary format.
Dim stream As Stream = File.Open("data.xml", FileMode.Create)
Dim formatter As New SoapFormatter()
formatter.Serialize(stream, obj)
stream.Close()
' Empties obj.
obj = Nothing
' Opens file "data.xml" and deserializes the object from it.
stream = File.Open("data.xml", FileMode.Open)
formatter = New SoapFormatter()
obj = CType(formatter.Deserialize(stream), TestSimpleObject)
stream.Close()
Console.WriteLine("")
Console.WriteLine("After deserialization the object contains: ")
obj.Print()
End Sub
End Class
' A test object that needs to be serialized.
<Serializable()> Public Class TestSimpleObject
Public member1 As Integer
Public member2 As String
Public member3 As String
Public member4 As Double
' A member that is not serialized.
<NonSerialized()> Public member5 As String
Public Sub New()
member1 = 11
member2 = "hello"
member3 = "hello"
member4 = 3.14159265
member5 = "hello world!"
End Sub
Public Sub Print()
Console.WriteLine("member1 = '{0}'", member1)
Console.WriteLine("member2 = '{0}'", member2)
Console.WriteLine("member3 = '{0}'", member3)
Console.WriteLine("member4 = '{0}'", member4)
Console.WriteLine("member5 = '{0}'", member5)
End Sub
End Class
Açıklamalar
BinaryFormatter SoapFormatter Bir nesneyi seri hale getirmek için veya sınıflarını kullanırken, NonSerializedAttribute bir alanın serileştirilmesi için özniteliğini kullanın.When using the BinaryFormatter or SoapFormatter classes to serialize an object, use the NonSerializedAttribute attribute to prevent a field from being serialized. Örneğin, hassas verilerin serileştirmesini engellemek için bu özniteliği kullanabilirsiniz.For example, you can use this attribute to prevent the serialization of sensitive data.
Özniteliği için hedef nesneler, NonSerializedAttribute serileştirilebilir bir sınıfın ortak ve özel alanlarıdır.The target objects for the NonSerializedAttribute attribute are public and private fields of a serializable class. Varsayılan olarak, sınıfları ile işaretlenmedikçe seri hale getirilebilir değildir SerializableAttribute .By default, classes are not serializable unless they are marked with SerializableAttribute. Serileştirme işlemi sırasında bir sınıfın tüm ortak ve özel alanları varsayılan olarak serileştirilir.During the serialization process all the public and private fields of a class are serialized by default. İle işaretlenmiş alanlar NonSerializedAttribute serileştirme sırasında hariç tutulur.Fields marked with NonSerializedAttribute are excluded during serialization. XmlSerializerBir nesneyi seri hale getirmek için sınıfını kullanıyorsanız, XmlIgnoreAttribute aynı işlevselliği almak için sınıfını kullanın.If you are using the XmlSerializer class to serialize an object, use the XmlIgnoreAttribute class to get the same functionality. Alternatif olarak, ISerializable serileştirme işlemini açıkça denetlemek için arabirimini uygulayın.Alternatively, implement the ISerializable interface to explicitly control the serialization process. Uygulayan sınıfların ISerializable hala ile işaretlenmiş olması gerektiğini unutmayın SerializableAttribute .Note that classes that implement ISerializable must still be marked with SerializableAttribute.
NonSerializedAttributeSınıfı bir olaya uygulamak için, aşağıdaki C# kodunda gösterildiği gibi, öznitelik konumunu alanı olarak ayarlayın.To apply the NonSerializedAttribute class to an event, set the attribute location to field, as shown in the following C# code.
[field:NonSerializedAttribute()]
public event ChangedEventHandler Changed;
Bir alan seri hale getirilmez, ancak seri durumdan çıktıktan sonra sağlanması gereken varsayılan bir değer gerektiriyorsa, alanı bir değere sahip olan bir yöntem oluşturabilir, sonra OnDeserializedAttribute metodunu yöntemine uygulayabilirsiniz.If a field is not serialized, but it still requires a default value that must be supplied after deserialization, you can create a method that supplies the field with a value, then apply the OnDeserializedAttribute to the method.
Öznitelikleri kullanma hakkında daha fazla bilgi için bkz. öznitelikler.For more information about using attributes, see Attributes.
Oluşturucular
| NonSerializedAttribute() |
NonSerializedAttribute sınıfının yeni bir örneğini başlatır.Initializes a new instance of the NonSerializedAttribute class. |
Özellikler
| TypeId |
Türetilmiş bir sınıfta uygulandığında, bunun için benzersiz bir tanımlayıcı alır Attribute .When implemented in a derived class, gets a unique identifier for this Attribute. (Devralındığı yer: Attribute) |
Yöntemler
| Equals(Object) |
Bu örneğin belirtilen bir nesneye eşit olup olmadığını gösteren bir değeri döndürür.Returns a value that indicates whether this instance is equal to a specified object. (Devralındığı yer: Attribute) |
| GetHashCode() |
Bu örneğe ilişkin karma kodu döndürür.Returns the hash code for this instance. (Devralındığı yer: Attribute) |
| GetType() |
TypeGeçerli örneği alır.Gets the Type of the current instance. (Devralındığı yer: Object) |
| IsDefaultAttribute() |
Türetilmiş bir sınıfta geçersiz kılınırsa, bu örneğin değerinin türetilmiş sınıf için varsayılan değer olup olmadığını gösterir.When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Devralındığı yer: Attribute) |
| Match(Object) |
Türetilmiş bir sınıfta geçersiz kılınırsa, bu örneğin belirtilen bir nesneye eşit olup olmadığını gösteren bir değer döndürür.When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Devralındığı yer: Attribute) |
| MemberwiseClone() |
Geçerli bir basit kopyasını oluşturur Object .Creates a shallow copy of the current Object. (Devralındığı yer: Object) |
| ToString() |
Geçerli nesneyi temsil eden dizeyi döndürür.Returns a string that represents the current object. (Devralındığı yer: Object) |
Belirtik Arabirim Kullanımları
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Bir ad kümesini karşılık gelen bir dağıtma tanımlayıcısı kümesine eşler.Maps a set of names to a corresponding set of dispatch identifiers. (Devralındığı yer: Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Bir arabirimin tür bilgilerini almak için kullanılabilen bir nesnenin tür bilgilerini alır.Retrieves the type information for an object, which can be used to get the type information for an interface. (Devralındığı yer: Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Bir nesnenin sağladığı tür bilgisi arabirimlerinin sayısını alır (0 ya da 1).Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Devralındığı yer: Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Bir nesne tarafından sunulan özelliklere ve yöntemlere erişim sağlar.Provides access to properties and methods exposed by an object. (Devralındığı yer: Attribute) |