-2

これは私のコードです:

List<string> deviceScreenshot=new List<string>(); 
List<string> fiddlerScreenshot=new List<string>();

if(string.IsNullOrEmpty(summary[3].ToString())==false)
    deviceScreenshot=summary[3];
else
    deviceScreenshot="Screenshot not found";

if(string.IsNullOrEmpty(summary[4].ToString())==false)
    fiddlerScreenshot=summary[4];
else
    fiddlerScreenshot="Screenshot not found";

次のエラー メッセージが表示されます。

タイプ 'string' を 'System.Collections.Generic.List' に暗黙的に変換できません (CS0029) - D:\automation\OmnitureStatistics\OmnitureStatistics\TeardownUserCode.cs:144,23

これの解決策を教えてください!!

4

4 に答える 4

1

ロジックをヘルパー メソッドに移動し、静的文字列をリソース ファイルに保存できます。

summary がリストまたは配列で、文字列の場合、ToString() を呼び出す必要はありません。

class Program
{
    static void Main(string[] args)
    {    
        new Program().AddStringToCollection();
    }             

    private void AddStringToCollection()
    {
        var summary = new string[] {"A", "B", "C", "", "D"};

        var deviceScreenshot = new List<string>();
        var fiddlerScreenshot = new List<string>();

        AppendExceptWhiteSpace(deviceScreenshot, summary[3]);
        AppendExceptWhiteSpace(fiddlerScreenshot, summary[4]);
    }

    //move to a resource file if possible
    const string NotFoundText = "Screenshot not found";

    //in a utility class this could also be an extension method
    private void AppendExceptWhiteSpace(List<string> list, string value)
    {
        //not sure if you want empty strings, otherwise change back to IsNullOrEmpty
        string text = string.IsNullOrWhiteSpace(value) 
                ? NotFoundText 
                : value;

        list.Add(text);
    }    
}
于 2015-05-07T07:22:33.300 に答える