![]() |
Languages »
C# »
General
Intermediate
License: The Code Project Open License (CPOL)
How to expand .NET structs and sealed classes in C#By Kaveh ShahbazianA way for expanding sealed data structures in .NET |
C# 1.0, Windows, .NET 1.0VS2005, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Sometimes it is required to add some functionality to existing data structures. The OOP school's way for this is to use inheritance. In situations where we consume interfaces instead of class or structure types we can extend the interface easily. In some languages like Ruby data structures are never sealed. So we can add extra abilities of our objects to the processing workflow of the running engine of the language. However, if the data structures are sealed then there could be no way to do this.
Here we'll see a way to convert two types implicitly that 'simulates' inheritance and when needed our type will be cast internally by the CLR.
IDisposable is a formal way for doing this). Because the wrapped type does not release it's captured resources, there may be some conflicts in execution path. So the finalization process must takes place properly.
public static implicit operator string(String V)
{
return V.mval;
}
public static implicit operator String(string n)
{
return new String(n);
}
//REQUIRED BECAUSE OF CONVERSION OPERATORS
public override bool Equals(object obj)
{
return this.mval == (string)obj;
}
public override int GetHashCode()
{
return this.mval.GetHashCode();
}
As you can see by browsing the provided code 'mval' is the internal place for Equals and GetHashCode. These implementations are needed by the CLR for performing the conversion.This is in fact same as previous step and the separation of conceptual paragraph is only an emphasis on it's importance that needs more care.
After this we have done and we have a new type that is equivalent to standard string. We can use it to provide a new type 'email' that is a string with a special format or in any sort of data that will be presented in a formatted string.
main method that performs a simple test : static void Main (string[] args)
{
UpperCaseString ucs = "this Is A teST TeXt.";
Console.WriteLine (ucs);
Console.WriteLine (ucs << 8);
string newString = ucs << 8 >> 1;
Console.WriteLine (newString);
//
Console.WriteLine ("Press any key to continue ...");
Console.ReadKey ();
}
UpperCaseString is an always upper case string class. As you see the instance of UpperCaseString has been initialized by an ordinary assignment. Then it is consumed by the WriteLine method of the Console class to print its value. There is no explicit conversion needed here.
There are new overloaded operators there : '>>' and '<<'. The first one shifts the string n characters to right and '<<' shifts the string n characters to left. Again there no necessary instructions for constructing a new instance of System.String named here newString. The result of '>>' and '<<' operations are automatically converted to the proper type (here System.String).
This was a little example of using this feature to attach additional functionalities and properties to existing sealed types. You can use the definition of UpperCaseString as a template for providing your specialized types. In addition you may want to provide some generic tools for performing needed applications.
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 1 Aug 2006 Editor: Chris Maunder |
Copyright 2006 by Kaveh Shahbazian Everything else Copyright © CodeProject, 1999-2009 Web09 | Advertise on the Code Project |