为什么使用显式强制转换操作符不允许“as”强制转换?

时间:2015-01-19 09:47:48

标签: c# casting

这是我的代码:

    public class GoodSuperDictionary
    {
        private Dictionary<string, string> dicitionary;

        public GoodSuperDictionary(Dictionary<string, string> dic)
        {
            dicitionary = dic;
        }

        public static explicit operator GoodSuperDictionary(Dictionary<string, string> dic)
        {
            return new GoodSuperDictionary(dic);
        }
    }

    Dictionary<string, string> dic = new Dictionary<string, string>();
    GoodSuperDictionary dic2 = dic as GoodSuperDictionary; // compile error
    GoodSuperDictionary dic3 = (GoodSuperDictionary)dic; // good

我希望compile error行能够运行,但似乎explicit operator并没有处理这种投射。

我想知道为什么会这样? 我还想知道operator是否允许as投射进入?

0 个答案:

没有答案