4

例えば、

group.SupportedProductsが["test"、 "hello"、"world"]であるとしましょう

var products = (string[]) group.SupportedProducts; 

結果として、「products」は上記の3つの要素(「test」、「hello」、「world」)を含む文字列配列になります。

でも、

var products= group.SupportedProducts as string[]; 

その結果、製品はnullになります。

4

1 に答える 1

9

おそらく実際にgroup.SupportedProductsはではありませんが、へのカスタム変換をサポートするものです。string[]string[]

as カスタム変換を呼び出すことはありませんが、キャストは呼び出します。

これを示すサンプル:

using System;

class Foo
{
    private readonly string name;

    public Foo(string name)
    {
        this.name = name;
    }

    public static explicit operator string(Foo input)
    {
        return input.name;
    }
}

class Test
{
    static void Main()
    {
        dynamic foo = new Foo("name");
        Console.WriteLine("Casting: {0}", (string) foo);
        Console.WriteLine("As: {0}", foo as string);
    }
}
于 2013-02-15T23:16:39.717 に答える