当前位置: 首页>>代码示例>>C#>>正文


C# IPEndPoint.GetType方法代码示例

本文整理汇总了C#中System.Net.IPEndPoint.GetType方法的典型用法代码示例。如果您正苦于以下问题:C# IPEndPoint.GetType方法的具体用法?C# IPEndPoint.GetType怎么用?C# IPEndPoint.GetType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.IPEndPoint的用法示例。


在下文中一共展示了IPEndPoint.GetType方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: NetTest5_IPEndPointBasic

        public MFTestResults NetTest5_IPEndPointBasic()
        {
            /// <summary>
            /// 1. Creates 30 Random IPs between 0.0.0.0 and 255.255.255.127
            /// 2. Verifies that they can be constructed as IPEndPoints with both ctors
            /// 3. Verifies that their data, ToString and GetHashCode funcs return normally
            /// 4. Clones one with Create and verifies the above funcs again
            /// </summary>
            ///
            bool testResult = true;
            try
            {
                Random random = new Random();
                for (int i = 0; i <= 30; i++)
                {
                    int[] IPInts = { random.Next(256), random.Next(256), 
                        random.Next(256), random.Next(128) };
                    int portInt = random.Next(65535) + 1;
                    long addressLong = (long)(
                        IPInts[0]
                        + IPInts[1] * 256
                        + IPInts[2] * 256 * 256
                        + IPInts[3] * 256 * 256 * 256);
                    Log.Comment("Random IP " + IPInts[0] + "." + IPInts[1]
                        + "." + IPInts[2] + "." + IPInts[3] + ":" + portInt);
                    IPAddress address = new IPAddress(addressLong);

                    Log.Comment("EndPoint1 created with IPAddress and int");
                    IPEndPoint endPoint1 = new IPEndPoint(address,portInt);
                    Log.Comment("EndPoint2 created with long and int"); 
                    IPEndPoint endPoint2 = new IPEndPoint(addressLong, portInt);
                    if (endPoint1 == null)
                        throw new Exception("EndPoint1 is null");
                    if (endPoint2 == null)
                        throw new Exception("EndPoint2 is null");

                    Type typeOfEndPoint = endPoint1.GetType();
                    if (typeOfEndPoint != Type.GetType("System.Net.IPEndPoint"))
                        throw new Exception("EndPoint1 Type is incorrect");
                    typeOfEndPoint = endPoint2.GetType();
                    if (typeOfEndPoint != Type.GetType("System.Net.IPEndPoint"))
                        throw new Exception("EndPoint2 Type is incorrect");

                    if (endPoint1.ToString() != endPoint2.ToString())
                        throw new Exception("ToString returns differently for same data");

                    if (!endPoint1.Equals(endPoint2))
                    {
                        throw new Exception("Equals returns false for same data");
                    }


                    int hashCode1 = endPoint1.GetHashCode();
                    int hashCode2 = endPoint2.GetHashCode();


                    if (hashCode1 != hashCode2)
                        throw new Exception("GetHasCode returns differently for same data");

                    if (endPoint1.Address.ToString() != endPoint2.Address.ToString()
                        || endPoint1.Address.ToString() != address.ToString()
                        || endPoint2.Address.ToString() != address.ToString())
                        throw new Exception("Address returns wrong data");

                    if (endPoint1.Port != endPoint2.Port
                        || endPoint1.Port != portInt
                        || endPoint2.Port != portInt)
                        throw new Exception("Port returns wrong data");
                    
                    Log.Comment("Cloning Enpoint1 into EndPoint2");
                    endPoint2 = (IPEndPoint)endPoint2.Create(endPoint1.Serialize());
                    typeOfEndPoint = endPoint2.GetType();
                    if (typeOfEndPoint != Type.GetType("System.Net.IPEndPoint"))
                        throw new Exception("EndPoint2 Type is incorrect after clone");

                    if (endPoint1.ToString() != endPoint2.ToString())
                        throw new Exception("ToString returns differently for cloned data");


                    //21295	GetHashCode returns differently for cloned data
                    if (endPoint1.GetHashCode() != endPoint2.GetHashCode())
                        throw new Exception("GetHashCode returns differently for cloned data");

                    if (endPoint1.Address.ToString() != endPoint2.Address.ToString()
                        || endPoint1.Address.ToString() != address.ToString()
                        || endPoint2.Address.ToString() != address.ToString())
                        throw new Exception("Address returns wrong data after clone");

                    if (endPoint1.Port != endPoint2.Port
                        || endPoint1.Port != portInt
                        || endPoint2.Port != portInt)
                        throw new Exception("Port returns wrong data after clone");

                    Log.Comment("Recreating EndPoint2 with new data");
                    int portInt2 = portInt % 2 + 1;
                    long addressLong2 = (long)(
                        (IPInts[0] % 2 + 1)
                        + (IPInts[1] % 2 + 1 )* 256
                        + (IPInts[2] % 2 + 1 )* 256 * 256
                        + (IPInts[3] % 2 + 1 )* 256 * 256 * 256);
//.........这里部分代码省略.........
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:101,代码来源:NetTests.cs


注:本文中的System.Net.IPEndPoint.GetType方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。