ParamArrayAttribute Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Azt jelzi, hogy egy metódus változó számú argumentumot engedélyez a meghívásban. Ez az osztály nem örökölhető.
public ref class ParamArrayAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter, AllowMultiple=false, Inherited=true)]
public sealed class ParamArrayAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Parameter, AllowMultiple=false, Inherited=true)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class ParamArrayAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Parameter, AllowMultiple=false, Inherited=true)>]
type ParamArrayAttribute = class
inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Parameter, AllowMultiple=false, Inherited=true)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type ParamArrayAttribute = class
inherit Attribute
Public NotInheritable Class ParamArrayAttribute
Inherits Attribute
- Öröklődés
- Attribútumok
Példák
Az alábbi példa egy Temperature metódust Display tartalmazó osztályt határoz meg, amely egy vagy több formázott hőmérsékleti érték megjelenítésére szolgál. A metódus egyetlen paraméterrel rendelkezik, formatsamely paramétertömbként van definiálva.
using System;
public class Temperature
{
private decimal temp;
public Temperature(decimal temperature)
{
this.temp = temperature;
}
public override string ToString()
{
return ToString("C");
}
public string ToString(string format)
{
if (String.IsNullOrEmpty(format))
format = "G";
switch (format.ToUpper())
{
case "G":
case "C":
return temp.ToString("N") + " °C";
case "F":
return (9 * temp / 5 + 32).ToString("N") + " °F";
case "K":
return (temp + 273.15m).ToString("N") + " °K";
default:
throw new FormatException(String.Format("The '{0}' format specifier is not supported",
format));
}
}
public void Display(params string []formats)
{
if (formats.Length == 0)
{
Console.WriteLine(this.ToString("G"));
}
else
{
foreach (string format in formats)
{
try {
Console.WriteLine(this.ToString(format));
}
// If there is an exception, do nothing.
catch { }
}
}
}
}
open System
type Temperature(temperature) =
override this.ToString() =
this.ToString "C"
member _.ToString(format) =
let format =
if String.IsNullOrEmpty format then "G"
else format
match format.ToUpper() with
| "G" | "C" ->
$"{temperature:N} °C"
| "F" ->
$"{9. * temperature / 5. + 32.:N} °F"
| "K" ->
$"{temperature + 273.15:N} °K"
| _ ->
raise (FormatException $"The '{format}' format specifier is not supported")
member this.Display([<ParamArray>]formats: string[]) =
if formats.Length = 0 then
printfn $"""{this.ToString "G"}"""
else
for format in formats do
try
printfn $"{this.ToString format}"
// If there is an exception, do nothing.
with _ -> ()
Public Class Temperature
Private temp As Decimal
Public Sub New(temperature As Decimal)
Me.temp = temperature
End Sub
Public Overrides Function ToString() As String
Return ToString("C")
End Function
Public Overloads Function ToString(format As String) As String
If String.IsNullOrEmpty(format) Then format = "G"
Select Case format
Case "G", "C"
Return temp.ToString("N") + " °C"
Case "F"
Return (9 * temp / 5 + 32).ToString("N") + " °F"
Case "K"
Return (temp + 273.15d).ToString("N") + " °K"
Case Else
Throw New FormatException(String.Format("The '{0}' format specifier is not supported", _
format))
End Select
End Function
Public Sub Display(<[ParamArray]()> formats() As String)
If formats.Length = 0 Then
Console.WriteLine(Me.ToString("G"))
Else
For Each format As String In formats
Try
Console.WriteLine(Me.ToString(format))
' If there is an exception, do nothing.
Catch
End Try
Next
End If
End Sub
End Class
Az alábbi példa a metódus három különböző hívását szemlélteti Temperature.Display . Az első lépésben a metódus formátumsztringek tömbje lesz átadva. A másodikban a metódus négy egyéni formázási sztringet ad át argumentumként. A harmadikban a metódus argumentumok nélkül van meghívva. Ahogy a példa kimenete is szemlélteti, a Visual Basic és a C#-fordítók ezt egy üres sztringtömböt tartalmazó Display metódus hívásává fordítják.
public class Class1
{
public static void Main()
{
Temperature temp1 = new Temperature(100);
string[] formats = { "C", "G", "F", "K" };
// Call Display method with a string array.
Console.WriteLine("Calling Display with a string array:");
temp1.Display(formats);
Console.WriteLine();
// Call Display method with individual string arguments.
Console.WriteLine("Calling Display with individual arguments:");
temp1.Display("C", "F", "K", "G");
Console.WriteLine();
// Call parameterless Display method.
Console.WriteLine("Calling Display with an implicit parameter array:");
temp1.Display();
}
}
// The example displays the following output:
// Calling Display with a string array:
// 100.00 °C
// 100.00 °C
// 212.00 °F
// 373.15 °K
//
// Calling Display with individual arguments:
// 100.00 °C
// 212.00 °F
// 373.15 °K
// 100.00 °C
//
// Calling Display with an implicit parameter array:
// 100.00 °C
let temp1 = Temperature 100.
let formats = [| "C"; "G"; "F"; "K" |]
// Call Display method with a string array.
printfn "Calling Display with a string array:"
temp1.Display formats
// Call Display method with individual string arguments.
printfn "\nCalling Display with individual arguments:"
temp1.Display("C", "F", "K", "G")
// Call parameterless Display method.
printfn "\nCalling Display with an implicit parameter array:"
temp1.Display()
// The example displays the following output:
// Calling Display with a string array:
// 100.00 °C
// 100.00 °C
// 212.00 °F
// 373.15 °K
//
// Calling Display with individual arguments:
// 100.00 °C
// 212.00 °F
// 373.15 °K
// 100.00 °C
//
// Calling Display with an implicit parameter array:
// 100.00 °C
Public Module Example
Public Sub Main()
Dim temp1 As New Temperature(100)
Dim formats() As String = { "C", "G", "F", "K" }
' Call Display method with a string array.
Console.WriteLine("Calling Display with a string array:")
temp1.Display(formats)
Console.WriteLine()
' Call Display method with individual string arguments.
Console.WriteLine("Calling Display with individual arguments:")
temp1.Display("C", "F", "K", "G")
Console.WriteLine()
' Call parameterless Display method.
Console.WriteLine("Calling Display with an implicit parameter array:")
temp1.Display()
End Sub
End Module
' The example displays the following output:
' Calling Display with a string array:
' 100.00 °C
' 100.00 °C
' 212.00 °F
' 373.15 °K
'
' Calling Display with individual arguments:
' 100.00 °C
' 212.00 °F
' 373.15 °K
' 100.00 °C
'
' Calling Display with an implicit parameter array:
' 100.00 °C
Megjegyzések
Ez ParamArrayAttribute azt jelzi, hogy a metódusparaméter egy paramétertömb. A paramétertömbök ismeretlen számú argumentum specifikációját teszik lehetővé. A paramétertömbnek egy formális paraméterlistában az utolsó paraméternek kell lennie, és egydimenziós tömbnek kell lennie. A metódus meghívásakor a paramétertömb lehetővé teszi, hogy egy metódus argumentumai kétféleképpen legyenek megadva:
Egy olyan típus egyetlen kifejezése, amely implicit módon átalakítható a paramétertömb típusára. A paramétertömb értékparaméterként működik.
Nulla vagy több argumentumként, ahol minden argumentum egy olyan típusú kifejezés, amely implicit módon konvertálható a paramétertömbelem típusára.
A következő szakaszban látható példa mindkét hívási konvencióciót szemlélteti.
Note
ParamArrayAttribute A kód általában nem használja közvetlenül a kódot. Ehelyett az egyes nyelvi kulcsszavakat, például ParamArray Visual Basic és params C#-ban, a ParamArrayAttribute osztály burkolójaként használják. Egyes nyelvek, például a C#, akár a nyelvi kulcsszó használatát is megkövetelhetik, és megtilthatják a használatát ParamArrayAttribute.
A túlterhelés feloldása során, ha a paramétertömböket támogató fordítók olyan metódusterheléssel találkoznak, amely nem létezik, de egy paramétertömböt tartalmazó túlterhelésnél eggyel kevesebb paraméterrel rendelkezik, a metódust a paramétertömböt tartalmazó túlterhelésre cserélik. Például a String.Split() példánymetódus hívása (amely nem létezik az String osztályban) a metódus hívásaként String.Split(Char[]) lesz feloldva. A fordító a szükséges típusú üres tömböt is átadja a metódusnak. Ez azt jelenti, hogy a metódusnak mindig készen kell állnia egy nulla hosszúságú tömb kezelésére, amikor a paramétertömb elemeit dolgozza fel. A példa egy illusztrációt tartalmaz.
Az attribútumok használatáról további információt az Attribútumok című témakörben talál.
Konstruktorok
| Name | Description |
|---|---|
| ParamArrayAttribute() |
Inicializálja az osztály új példányát ParamArrayAttribute alapértelmezett tulajdonságokkal. |
Tulajdonságok
| Name | Description |
|---|---|
| TypeId |
Ha származtatott osztályban implementálják, ehhez egy egyedi azonosítót Attributekap. (Öröklődés forrása Attribute) |
Metódusok
| Name | Description |
|---|---|
| Equals(Object) |
Olyan értéket ad vissza, amely jelzi, hogy ez a példány egyenlő-e egy adott objektummal. (Öröklődés forrása Attribute) |
| GetHashCode() |
A példány kivonatkódját adja vissza. (Öröklődés forrása Attribute) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| IsDefaultAttribute() |
Ha egy származtatott osztályban felül van bírálva, azt jelzi, hogy a példány értéke-e a származtatott osztály alapértelmezett értéke. (Öröklődés forrása Attribute) |
| Match(Object) |
Származtatott osztály felülírásakor egy olyan értéket ad vissza, amely jelzi, hogy ez a példány egy adott objektummal egyenlő-e. (Öröklődés forrása Attribute) |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| ToString() |
Az aktuális objektumot jelképező sztringet ad vissza. (Öröklődés forrása Object) |
Explicit interfész-implementációk
| Name | Description |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Névkészletet képez le a küldési azonosítók megfelelő készletére. (Öröklődés forrása Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Lekéri egy objektum típusadatait, amelyek a felület típusadatainak lekérésére használhatók. (Öröklődés forrása Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Lekéri az objektumok által biztosított típusinformációs felületek számát (0 vagy 1). (Öröklődés forrása Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Hozzáférést biztosít az objektumok által közzétett tulajdonságokhoz és metódusokhoz. (Öröklődés forrása Attribute) |