Statik Sınıfları ve Yöntemleri Kullanmak


Tüm .NET Framework sınıfları New-Object kullanılarak oluşturulamaz. Örneğin, New-Object ile bir System.Environment veya System.Math nesnesi oluşturmaya çalışırsanız aşağıdaki hatayı alırsınız mesajlar:

New-Object System.Environment
New-Object : Constructor not found. Cannot find an appropriate constructor for
type System.Environment.
At line:1 char:11
+ New-Object  <<<< System.Environment
New-Object System.Math
New-Object : Constructor not found. Cannot find an appropriate constructor for
type System.Math.
At line:1 char:11
+ New-Object  <<<< System.Math

Bu hatalar, bu sınıflardan yeni bir nesne oluşturmanın bir yolu olmadığından ortaya çıkar. Bu sınıflar, durumu değiştirmeyen yöntem ve özelliklerin referans kitaplıklarıdır. Bunları oluşturmanıza gerek yok, yalnızca kullanmanız yeterli. Bunlar gibi sınıflara ve yöntemlere statik sınıflar denir çünkü bunlar oluşturulmaz, yok edilmez veya değiştirilmez. Bunu açıklığa kavuşturmak için statik sınıfları kullanan örnekler sunacağız.

System.Environment ile ortam verilerini alma

Genellikle Windows PowerShell'de bir nesneyle çalışmanın ilk adımı, nesnenin hangi üyeleri içerdiğini bulmak için Get-Member'ı kullanmaktır. Statik sınıflarda süreç biraz farklıdır çünkü gerçek sınıf bir nesne değildir.

Statik System.Environment sınıfına atıfta bulunarak

Sınıf adını köşeli parantez içine alarak statik bir sınıfa başvurabilirsiniz. Örneğin, adını parantez içine yazarak System.Environment'a başvurabilirsiniz. Bunu yaptığınızda bazı genel tür bilgileri görüntülenir:

[System.Environment]
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    Environment                              System.Object

Not

Daha önce de belirttiğimiz gibi, Windows PowerShell, New-Object kullandığınızda adları yazmanın başına otomatik olarak 'System.' ifadesini ekler. Aynı şey köşeli ayraçlı tür adı kullanıldığında da olur; dolayısıyla [System.Environment]'ı [Environment] olarak belirtebilirsiniz.

System.Environment sınıfı, Windows PowerShell'de çalışırken powershell.exe olan geçerli işlemin çalışma ortamı hakkında genel bilgiler içerir.

[System.Environment] | yazarak bu sınıfın ayrıntılarını görüntülemeye çalışırsanız Get-Member, nesne türünün System.Environment değil, System.RuntimeType olduğu bildirildi:

[System.Environment] | Get-Member
   TypeName: System.RuntimeType

Get-Member ile statik üyeleri görüntülemek için Statik parametresini belirtin:

[System.Environment] | Get-Member -Static
   TypeName: System.Environment

Name                       MemberType Definition
----                       ---------- ----------
Equals                     Method     static System.Boolean Equals(Object ob...
Exit                       Method     static System.Void Exit(Int32 exitCode)
...
CommandLine                Property   static System.String CommandLine {get;}
CurrentDirectory           Property   static System.String CurrentDirectory ...
ExitCode                   Property   static System.Int32 ExitCode {get;set;}
HasShutdownStarted         Property   static System.Boolean HasShutdownStart...
MachineName                Property   static System.String MachineName {get;}
NewLine                    Property   static System.String NewLine {get;}
OSVersion                  Property   static System.OperatingSystem OSVersio...
ProcessorCount             Property   static System.Int32 ProcessorCount {get;}
StackTrace                 Property   static System.String StackTrace {get;}
SystemDirectory            Property   static System.String SystemDirectory {...
TickCount                  Property   static System.Int32 TickCount {get;}
UserDomainName             Property   static System.String UserDomainName {g...
UserInteractive            Property   static System.Boolean UserInteractive ...
UserName                   Property   static System.String UserName {get;}
Version                    Property   static System.Version Version {get;}
WorkingSet                 Property   static System.Int64 WorkingSet {get;}
TickCount                               ExitCode

Artık System.Environment'tan görüntülenecek özellikleri seçebiliriz.

System.Environment'ın statik özelliklerini görüntüleme

System.Environment'ın özellikleri de statiktir ve normal özelliklerden farklı bir şekilde belirtilmelidir. Windows PowerShell'e statik bir yöntem veya özellik ile çalışmak istediğimizi belirtmek için :: kullanırız. Windows PowerShell'i başlatmak için kullanılan komutu görmek için CommandLine özelliğini şunu yazarak kontrol ederiz:

[System.Environment]::Commandline
"C:\Program Files\Windows PowerShell\v1.0\powershell.exe"

İşletim sistemi sürümünü kontrol etmek için şunu yazarak OSVersion özelliğini görüntüleyin:

[System.Environment]::OSVersion
           Platform ServicePack         Version             VersionString
           -------- -----------         -------             -------------
            Win32NT Service Pack 2      5.1.2600.131072     Microsoft Windows...

HasShutdownStarted özelliğini görüntüleyerek bilgisayarın kapanma sürecinde olup olmadığını kontrol edebiliriz:

[System.Environment]::HasShutdownStarted
False

System.Math ile matematik yapmak

System.Math statik sınıfı bazı matematiksel işlemleri gerçekleştirmek için kullanışlıdır. Sınıf, Get-Member kullanarak görüntüleyebileceğimiz birkaç faydalı yöntem içerir.

Not

System.Math'in aynı ada sahip birkaç yöntemi vardır ancak bunlar, parametrelerinin türüne göre farklılık gösterir.

System.Math sınıfının yöntemlerini listelemek için aşağıdaki komutu yazın.

[System.Math] | Get-Member -Static -MemberType Methods
   TypeName: System.Math

Name            MemberType Definition
----            ---------- ----------
Abs             Method     static System.Single Abs(Single value), static Sy...
Acos            Method     static System.Double Acos(Double d)
Asin            Method     static System.Double Asin(Double d)
Atan            Method     static System.Double Atan(Double d)
Atan2           Method     static System.Double Atan2(Double y, Double x)
BigMul          Method     static System.Int64 BigMul(Int32 a, Int32 b)
Ceiling         Method     static System.Double Ceiling(Double a), static Sy...
Cos             Method     static System.Double Cos(Double d)
Cosh            Method     static System.Double Cosh(Double value)
DivRem          Method     static System.Int32 DivRem(Int32 a, Int32 b, Int3...
Equals          Method     static System.Boolean Equals(Object objA, Object ...
Exp             Method     static System.Double Exp(Double d)
Floor           Method     static System.Double Floor(Double d), static Syst...
IEEERemainder   Method     static System.Double IEEERemainder(Double x, Doub...
Log             Method     static System.Double Log(Double d), static System...
Log10           Method     static System.Double Log10(Double d)
Max             Method     static System.SByte Max(SByte val1, SByte val2), ...
Min             Method     static System.SByte Min(SByte val1, SByte val2), ...
Pow             Method     static System.Double Pow(Double x, Double y)
ReferenceEquals Method     static System.Boolean ReferenceEquals(Object objA...
Round           Method     static System.Double Round(Double a), static Syst...
Sign            Method     static System.Int32 Sign(SByte value), static Sys...
Sin             Method     static System.Double Sin(Double a)
Sinh            Method     static System.Double Sinh(Double value)
Sqrt            Method     static System.Double Sqrt(Double d)
Tan             Method     static System.Double Tan(Double a)
Tanh            Method     static System.Double Tanh(Double value)
Truncate        Method     static System.Decimal Truncate(Decimal d), static...

Bu, çeşitli matematiksel yöntemleri görüntüler. Yaygın yöntemlerden bazılarının nasıl çalıştığını gösteren komutların bir listesi aşağıda verilmiştir:

[System.Math]::Sqrt(9)
3
[System.Math]::Pow(2,3)
8
[System.Math]::Floor(3.3)
3
[System.Math]::Floor(-3.3)
-4
[System.Math]::Ceiling(3.3)
4
[System.Math]::Ceiling(-3.3)
-3
[System.Math]::Max(2,7)
7
[System.Math]::Min(2,7)
2
[System.Math]::Truncate(9.3)
9
[System.Math]::Truncate(-9.3)
-9

İlgili makaleler