LISTINg 16-1:
A simple calculations extension
public class Calcs
{
private Int32 Data;
public Calcs(Int32 Value)
{
this.Data = Value;
}
public override string ToString()
{
return Data.ToString();
}
public static Calcs operator +(Calcs Value1, Calcs Value2)
{
return new Calcs(Value1.Data + Value2.Data);
}
public static Calcs operator -(Calcs Value1, Calcs Value2)
{
return new Calcs(Value1.Data - Value2.Data);
}
public static Calcs operator *(Calcs Value1, Calcs Value2)
{
return new Calcs(Value1.Data * Value2.Data);
}
public static Calcs operator /(Calcs Value1, Calcs Value2)
{
return new Calcs(Value1.Data / Value2.Data);
}
public Calcs Inc()
{
return new Calcs(this.Data + 1);
}
public Calcs Dec()
{
return new Calcs(this.Data - 1);
}
}
548592c16.indd 331
2/24/10 12:49:22 PM
www.finebook.ir
332
❘
CHAPTER 16
ExtEnding ironPython Using C#
In most cases, you want to create a constructor that accepts the kind of data you want to manipulate
with the extension. In this case, the constructor accepts an
Int32
value. Interestingly enough, the
constructor is the only place where you normally reference the data type of the data directly. In all
other cases, you work with the data type indirectly by using the extension class.
Another issue is displaying the data in IronPython. The default implementation of the
ToString()
method displays the class name, which isn’t helpful. Consequently, you must override the default
implementation of
ToString()
and provide your own output. In this case, the method simply
returns the current value of the private variable
Data
as a string.
This example deals with operators. Of course, there are two kinds of operators, unary and binary.
The method you implement for each kind of operator is different.
To create a binary operator, you must consider that the operator will work with two instances
of the
Calcs
class. In short, the operator works with the base class and you must declare it as
static
. In this example, the
+
operator is binary, so the code declares it as
static
. The method
also accepts the two instances of the
Calcs
class as input. In order to return output, the method
must create a new instance of the
Calcs
class with the sum of the two input values. Notice that
the method never defines what kind of data it works on, simply that the data is contained in an
instance of the
Calcs
class.
Creating a unary operator is different because you’re working with a single instance of the
Calcs
class in this instance. To create a unary operator, you simply declare the method as a non-static
member of the class, as shown for the
Inc()
and
Dec()
methods. In this case, because you’re
working with a single value, the code uses
this.Data
(the internal representation of the data value
of the single value) to perform the math. You may wonder why the code simply doesn’t create a
++
operator method. A
++
operator method would look like this and wouldn’t work in a unary man-
ner within IronPython.
public static Calcs operator ++(Calcs Value1)
{
return new Calcs(Value1.Data + 1);
}
If you compiled the class now, you could view it in the IronPython console. The following code
provides the steps for loading the extension into memory.
import clr
clr.AddReferenceToFile(‘Calcs.DLL’)
import Calcs
dir(Calcs.Calcs)
Figure 16-3 shows the output of the
dir(Calcs.Calcs)
call. Notice that
Inc()
and
Dec()
appear as you expect. However, there aren’t any entries for
+
,
-
,
*
, and
/
methods. These
operators still work as you expect, but IronPython shows a Python equivalent for the operators
in the form of the
__add__()
,
__radd__()
,
__sub__()
,
__rsub__()
,
__mul__()
,
__rmul__()
,
__div__()
, and
__rdiv__()
. These methods don’t appear unless you define the operators in
your class.
548592c16.indd 332
2/24/10 12:49:22 PM
www.finebook.ir
Do'stlaringiz bilan baham: |