none
不理解proxy为什么要写成using(proxy as IDisposable) RRS feed

  • 问题

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using Contracts;
    namespace Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice"))
                {
                    ICalculator proxy = channelFactory.CreateChannel();
                    using (proxy as IDisposable)
                    {
                        Console.WriteLine("x+y={2} when x={0} and y={1}",1,2,proxy.Add(1,2));
                    }
                }
            }
        }
    }

    请问为什么要把proxy写成proxy as IDisposable,using不是有自动释放资源的功能吗?请指教,谢谢。


    孙伟安静
    2011年12月21日 9:11

答案