T Y P E R E F L E C T I O N, L AT E B I N D I N G, A N D AT T R I B U T E - B A S E D P R O G R A M M I N G
To illustrate the process of applying attributes in C#, assume you wish to build a class named
Motorcycle that can be persisted in a binary format. To do so, simply apply the [Serializable]
attribute to the class definition. If you have a field that should not be persisted, you may apply the
[NonSerialized] attribute:
// This class can be saved to disk.
[Serializable]
public class Motorcycle
{
// However this field will not be persisted.
[NonSerialized]
float weightOfCurrentPassengers;
// These fields are still serializable.
bool hasRadioSystem;
bool hasHeadSet;
bool hasSissyBar;
}
Do'stlaringiz bilan baham: