ClassAssistance.Common/Utility.cs

(开头部分) 1KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Net;

namespace ClassAssistance.Common
{
    /// <summary>
    /// 
    /// </summary>
    public static class Utility
    {
        static readonly string IPIFY = "https://api.ipify.org/";

        /// <summary>
        /// Wrapper method to simplify such operation.
        /// First, it try to retrieve the object from the "operation" function.
        /// If it suceed, the returned object is passed to the "onSuccess" function, else, the Exception is given to the "onError" function.
        /// 
        /// </summary>
        /// <param name="operation"></param>
        /// <param name="onSuccess"></param>
        /// <param name="onError"></param>
        public static void ExecuteComplexOperation<T>(Func<T> operation, Action<T> onSuccess, Action<Exception> onError)
        {
            try
            {
                onSuccess(operation());
            }
            catch (Exception e)
            {
                onError(e);
            }
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetWanIp()
        {
            try
            {
                using (var client = new WebClient())
                    return client.DownloadString(IPIFY);
            }
            catch
            {
                return "?.?.?.?";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="length"></param>
        public static string FormatFileSize(double length)
        {
            int order = 0;
            string[] sizes = { "B", "KB", "MB", "GB" };
            while (length >= 1024 && order + 1 < sizes.Length)
            {
后面还有 7 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。