130
❘
CHAPTER 7
Accessing the .net FrAmework
FIguRE 7-11:
IronPython doesn’t limit the information you receive about assemblies.
Working with the attributes can prove a little tricky because they each contain different values.
When working with the
AssemblyCompanyAttribute
, for example, you can access the
Company
property that contains the name of the company that created the attribute. Of course, nothing dif-
fers from any other .NET language in this case. You need to know precisely which attributes you
want to query and the properties within those attributes that contain the values you need in order to
interact with attributes successfully.
making Static method Calls
Many of the tasks you perform using .NET require use of static methods. Static methods
work the same in IronPython as they do in any .NET language. Listing 7-2 shows some static
method calls that work with the current date and time. The techniques shown work with any
static method.
548592c07.indd 130
2/25/10 9:44:29 AM
www.finebook.ir
Interacting with .NET Framework Elements
❘
131
lISTINg 7-2:
Performing tasks using static methods
# Add the .NET Framework 2.0 to the path.
import sys
sys.path.append(‘C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727’)
# Make clr accessible.
import clr
# Import the System assembly.
import System
# Get the system date and time.
CurrDateTime = System.DateTime.Now
# Display the date and time in a number of formats.
print ‘Short date and time:’
print System.DateTime.Now.ToShortDateString(),
print System.DateTime.Now.ToShortTimeString()
print ‘\nLong date and time:’
print CurrDateTime.ToLongDateString(),
print CurrDateTime.ToLongTimeString()
# Display a few statistics.
print ‘\n Date Statistics:’
print ‘Days in Month:’,
print CurrDateTime.DaysInMonth(CurrDateTime.Year, CurrDateTime.Month)
print ‘Daylight Savings?’, CurrDateTime.IsDaylightSavingTime()
print ‘Leap Year?’,
print CurrDateTime.IsLeapYear(CurrDateTime.Year)
# Manipulate the date.
print ‘\nAdding a Day, Month, and Year:’
CurrDateTime = CurrDateTime.AddDays(1)
CurrDateTime = CurrDateTime.AddMonths(1)
CurrDateTime = CurrDateTime.AddYears(1)
print CurrDateTime.ToLongDateString()
# Pause after the debug session.
raw_input(‘Press any key to continue...’)
The code begins by adding the required
sys.path
entry and importing the necessary modules and
assemblies. It then creates a variable named
CurrDateTime
, which is only in place for convenience.
The code sets
CurrDateTime
to reference
System.DateTime.Now
. You can do the same thing with-
out relying on the variable.
The first outputs are the short and long date and time. Notice that the short date and time
rely on
System.DateTime.Now
, while the long date and time rely on
CurrDateTime
. In both
cases, the code calls on static methods to output the date and time in a specific format using
ToShortDateString()
,
ToShortTimeString()
,
ToLongDateString()
, and
ToLongTimeString()
.
Figure 7-12 shows the output from these calls.
548592c07.indd 131
2/25/10 9:44:29 AM
www.finebook.ir
Do'stlaringiz bilan baham: |