簡體   English   中英

創建一個方法,其中一個參數可以是數組或字符串

[英]Creating a method in which one of the parameters could be an array or a string

我正在轉換用於數據記錄的舊經典 ASP 方法。 其中一個參數可以是數組或字符串。 我聲明什么類型的變量? 我嘗試將其聲明為 object,然后測試它是否為數組。 但是,我似乎無法解析出數組內容(應該是一個簡單的字符串數組)。

下面是一段經典的 ASP 代碼:

               Public function security(u,a,d)
' -------------------------------------------------------
' Write to tbl_log_security, returning a 1-Pass or 0-Fail
' -------------------------------------------------------
    u = u + 0
    af = u
    if len(request.Cookies("userid")) > 0 then af = request.Cookies("userid")
    security =                                      1 ' Success
    Dim objCommandLog
    Set objCommandLog =                                Server.CreateObject("ADODB.Connection")   
    objCommandLog.open =                               application("connVRVprimary")
    Err.Clear
    if isarray(d) then
        for I = 0 to ubound(d)
            strDetails = strDetails &               chr(13) & "Detail " & I & ": " & d(i) & " "
        next
    elseif len(d) > 0 then : strDetails = d & " "
    end if

在這里,我認為它可能會轉換為 C#。

public static bool security(string UserID, string Action, object Details )
{ 
    // Apparently, Details can be a string or an array !
    // UserID is passed in as a string, we need to convert it to an int
    Int32 iUserID; //af
    string strDetails;

    if (!Int32.TryParse(UserID, out iUserID))
    {
        //If it doesn't convert, then use the UserID from Application Object
        iUserID = Convert.ToInt32(ApplicationObject.USERID);
    }

    Type valueType = Details.GetType();
    if (valueType.IsArray)
    {
    for(int i = 0, i < Details.Length; i++)
        {
            strDetails += "Detail " + i + ": " + Details(i);
        }
    }
    else
    { // is string
        strDetails = Details;
    }

Intellisense 告訴我,我無法獲得長度屬性或迭代它。 我懷疑即使它可能以數組的形式出現,它也被視為 object。 任何幫助,將不勝感激。

正如 Alexei 在評論中提醒我的那樣,這應該是一對方法重載,其中任何一個都沒有強制轉換:

    public static bool security(string UserID, string Action, string[] details)
    public static bool security(string UserID, string Action, string details)

如果你被允許,你應該這樣寫。

在你擁有的版本中,如果你需要讓它工作,你需要轉換它。 您不能將 object 類型的引用直接視為字符串或數組,即使它所引用的實際 object 實際上是其中之一。 您必須明確告訴編譯器您想要什么。 在這種情況下,我們還想詢問運行時引用的 object 的實際類型是什么。

我不知道你的數組可能是一個數組但任何數組都是非泛型 IEnumerable,所以我們將使用它。

//  Check for string first, because String is IEnumerable.
if (Details is String s)
{
    strDetails = s;
}
else if (Details is System.Collections.IEnumerable ienum)
{
    int i = 0;
    foreach (var s in ienum)
    {
        strDetails += "Detail " + i++ + ": " + s + "\n";
    }
}

“其中一個參數可以是數組或字符串。我聲明什么類型的變量?”

params來救援!

由於details是方法簽名中的最后一個參數,您可以將其定義為params參數,它允許傳遞任意數量的項目。

(旁注:在 C# 中,方法名稱通常是PascalCase ,而 arguments 通常是camelCase

public static bool Security(string userID, string action, params string[] details )
{
    // Other code omitted for brevity

    var strDetails = details == null ? " " 
        : details.Length == 1 ? $"{details[0]} "
        : string.Join(Environment.NewLine,
            details.Select((detail, index) => $"Detail {index}: {detail} "));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM