관리 메뉴

심재운 블로그

c# 공통 함수에 좋은 기능들... 본문

닷넷관련/CSharp

c# 공통 함수에 좋은 기능들...

재우니 2017.03.23 21:56



/// <summary>

///  이메일 계정 체크

/// </summary>

/// <param name="email">The email.</param>

/// <returns></returns>

public static string EnsureSubscriberEmailOrThrow(string email)

{

    string output = EnsureNotNull(email);

    output = output.Trim();

    output = EnsureMaximumLength(output, 255);


    if (!IsValidEmail(output))

    {

        output = "Email is not valid.";

    }


    return output;

}


/// <summary>

/// 이메일 계정 체크

/// </summary>

/// <param name="email">Email to verify</param>

/// <returns>true if the string is a valid e-mail address and false if it's not</returns>

public static bool IsValidEmail(string email)

{

    if (String.IsNullOrEmpty(email))

        return false;


    email = email.Trim();

    var result = Regex.IsMatch(email, "^(?:[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\`\\{\\|\\}\\~]+\\.)*[\\w\\!\\#\\$\\%\\&\\'\\*\\+\\-\\/\\=\\?\\^\\`\\{\\|\\}\\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\\-](?!\\.)){0,61}[a-zA-Z0-9]?\\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\\[(?:(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\.){3}(?:[01]?\\d{1,2}|2[0-4]\\d|25[0-5])\\]))$", RegexOptions.IgnoreCase);

    return result;

}


/// <summary>

/// ip주소 체크하기

/// </summary>

/// <param name="ipAddress">IPAddress to verify</param>

/// <returns>true if the string is a valid IpAddress and false if it's not</returns>

public static bool IsValidIpAddress(string ipAddress)

{

    IPAddress ip;

    return IPAddress.TryParse(ipAddress, out ip);

}


/// <summary>

/// 숫자길이 만큼 랜덤값 가져오기

/// </summary>

/// <param name="length">Length</param>

/// <returns>Result string</returns>

public static string GenerateRandomDigitCode(int length)

{

    var random = new Random();

    string str = string.Empty;

    for (int i = 0; i < length; i++)

        str = String.Concat(str, random.Next(10).ToString());

    return str;

}


/// <summary>

/// 범위 내의 숫자 값 랜덤 추출하기

/// </summary>

/// <param name="min">Minimum number</param>

/// <param name="max">Maximum number</param>

/// <returns>Result</returns>

public static int GenerateRandomInteger(int min = 0, int max = int.MaxValue)

{

    var randomNumberBuffer = new byte[10];

    new RNGCryptoServiceProvider().GetBytes(randomNumberBuffer);

    return new Random(BitConverter.ToInt32(randomNumberBuffer, 0)).Next(min, max);

}


/// <summary>

/// 문자열 최대글자수 만큼 제한하기

/// </summary>

/// <param name="str">Input string</param>

/// <param name="maxLength">Maximum length</param>

/// <param name="postfix">A string to add to the end if the original string was shorten</param>

/// <returns>Input string if its lengh is OK; otherwise, truncated input string</returns>

public static string EnsureMaximumLength(string str, int maxLength, string postfix = null)

{

    if (String.IsNullOrEmpty(str))

        return str;


    if (str.Length > maxLength)

    {

        var pLen = postfix == null ? 0 : postfix.Length;


        var result = str.Substring(0, maxLength - pLen);

        if (!String.IsNullOrEmpty(postfix))

        {

            result += postfix;

        }

        return result;

    }


    return str;

}


/// <summary>

/// 문자열에 숫자만 추출하기

/// </summary>

/// <param name="str">Input string</param>

/// <returns>Input string with only numeric values, empty string if input is null/empty</returns>

public static string EnsureNumericOnly(string str)

{

    return string.IsNullOrEmpty(str) ? string.Empty : new string(str.Where(p => char.IsDigit(p)).ToArray());

}


/// <summary>

/// 문자열이 null 이면 empty 처리하기

/// </summary>

/// <param name="str">Input string</param>

/// <returns>Result</returns>

public static string EnsureNotNull(string str)

{

    return str ?? string.Empty;

}


/// <summary>

/// 문자열이 null 또는 empty 인지 체크하기

/// </summary>

/// <param name="stringsToValidate">Array of strings to validate</param>

/// <returns>Boolean</returns>

public static bool AreNullOrEmpty(params string[] stringsToValidate)

{

    return stringsToValidate.Any(p => string.IsNullOrEmpty(p));

}


/// <summary>

/// 2개의 배열 동일한지 비교하기

/// </summary>

/// <typeparam name="T">Type</typeparam>

/// <param name="a1">Array 1</param>

/// <param name="a2">Array 2</param>

/// <returns>Result</returns>

public static bool ArraysEqual<T>(T[] a1, T[] a2)

{

    //also see Enumerable.SequenceEqual(a1, a2);

    if (ReferenceEquals(a1, a2))

        return true;


    if (a1 == null || a2 == null)

        return false;


    if (a1.Length != a2.Length)

        return false;


    var comparer = EqualityComparer<T>.Default;

    for (int i = 0; i < a1.Length; i++)

    {

        if (!comparer.Equals(a1[i], a2[i])) return false;

    }

    return true;

}


private static AspNetHostingPermissionLevel? _trustLevel;

/// <summary>

/// 운영된 사이트가 trust level 인지 체크하기 

// (http://blogs.msdn.com/dmitryr/archive/2007/01/23/finding-out-the-current-trust-level-in-asp-net.aspx)

/// </summary>

/// <returns>The current trust level.</returns>

public static AspNetHostingPermissionLevel GetTrustLevel()

{

    if (!_trustLevel.HasValue)

    {

        //set minimum

        _trustLevel = AspNetHostingPermissionLevel.None;


        //determine maximum

        foreach (AspNetHostingPermissionLevel trustLevel in new[] {

                        AspNetHostingPermissionLevel.Unrestricted,

                        AspNetHostingPermissionLevel.High,

                        AspNetHostingPermissionLevel.Medium,

                        AspNetHostingPermissionLevel.Low,

                        AspNetHostingPermissionLevel.Minimal

                    })

        {

            try

            {

                new AspNetHostingPermission(trustLevel).Demand();

                _trustLevel = trustLevel;

                break; //we've set the highest permission we can

            }

            catch (System.Security.SecurityException)

            {

                continue;

            }

        }

    }

    return _trustLevel.Value;

}


/// <summary>

/// 객체에 값을 속성에 설정하기

/// </summary>

/// <param name="instance">The object whose property to set.</param>

/// <param name="propertyName">The name of the property to set.</param>

/// <param name="value">The value to set the property to.</param>

public static void SetProperty(object instance, string propertyName, object value)

{

    if (instance == null) throw new ArgumentNullException("instance");

    if (propertyName == null) throw new ArgumentNullException("propertyName");


    Type instanceType = instance.GetType();

    PropertyInfo pi = instanceType.GetProperty(propertyName);

    if (pi == null)

        throw new NopException("No property '{0}' found on the instance of type '{1}'.", propertyName, instanceType);

    if (!pi.CanWrite)

        throw new NopException("The property '{0}' on the instance of type '{1}' does not have a setter.", propertyName, instanceType);

    if (value != null && !value.GetType().IsAssignableFrom(pi.PropertyType))

        value = To(value, pi.PropertyType);

    pi.SetValue(instance, value, new object[0]);

}


/// <summary>

/// 값을 지정된 타입으로 변환하기

/// </summary>

/// <param name="value">The value to convert.</param>

/// <param name="destinationType">The type to convert the value to.</param>

/// <returns>The converted value.</returns>

public static object To(object value, Type destinationType)

{

    return To(value, destinationType, CultureInfo.InvariantCulture);

}


/// <summary>

/// 값을 지정한 타입으로 변환하기

/// </summary>

/// <param name="value">The value to convert.</param>

/// <param name="destinationType">The type to convert the value to.</param>

/// <param name="culture">Culture</param>

/// <returns>The converted value.</returns>

public static object To(object value, Type destinationType, CultureInfo culture)

{

    if (value != null)

    {

        var sourceType = value.GetType();


        var destinationConverter = TypeDescriptor.GetConverter(destinationType);

        if (destinationConverter != null && destinationConverter.CanConvertFrom(value.GetType()))

            return destinationConverter.ConvertFrom(null, culture, value);


        var sourceConverter = TypeDescriptor.GetConverter(sourceType);

        if (sourceConverter != null && sourceConverter.CanConvertTo(destinationType))

            return sourceConverter.ConvertTo(null, culture, value, destinationType);


        if (destinationType.IsEnum && value is int)

            return Enum.ToObject(destinationType, (int)value);


        if (!destinationType.IsInstanceOfType(value))

            return Convert.ChangeType(value, destinationType, culture);

    }

    return value;

}


/// <summary>

/// 값을 지정된 형식으로 변환하기

/// </summary>

/// <param name="value">The value to convert.</param>

/// <typeparam name="T">The type to convert the value to.</typeparam>

/// <returns>The converted value.</returns>

public static T To<T>(object value)

{

    //return (T)Convert.ChangeType(value, typeof(T), CultureInfo.InvariantCulture);

    return (T)To(value, typeof(T));

}


/// <summary>

/// Convert enum for front-end

/// </summary>

/// <param name="str">Input string</param>

/// <returns>Converted string</returns>

public static string ConvertEnum(string str)

{

    if (string.IsNullOrEmpty(str)) return string.Empty;

    string result = string.Empty;

    foreach (var c in str)

        if (c.ToString() != c.ToString().ToLower())

            result += " " + c.ToString();

        else

            result += c.ToString();


    //ensure no spaces (e.g. when the first letter is upper case)

    result = result.TrimStart();

    return result;

}


/// <summary>

/// Set Telerik (Kendo UI) culture

/// </summary>

public static void SetTelerikCulture()

{

    //little hack here

    //always set culture to 'en-US' (Kendo UI has a bug related to editing decimal values in other cultures). Like currently it's done for admin area in Global.asax.cs


    var culture = new CultureInfo("en-US");

    Thread.CurrentThread.CurrentCulture = culture;

    Thread.CurrentThread.CurrentUICulture = culture;

}


/// <summary>

/// 두 날짜 사이의 년수 구하기

/// </summary>

/// <param name="startDate"></param>

/// <param name="endDate"></param>

/// <returns></returns>

public static int GetDifferenceInYears(DateTime startDate, DateTime endDate)

{

    //source: http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c

    //this assumes you are looking for the western idea of age and not using East Asian reckoning.

    int age = endDate.Year - startDate.Year;

    if (startDate > endDate.AddYears(-age))

        age--;

    return age;

}


/// <summary>

/// 가상경로를 물리경로로 변환하기

/// </summary>

/// <param name="path">The path to map. E.g. "~/bin"</param>

/// <returns>The physical path. E.g. "c:\inetpub\wwwroot\bin"</returns>

public static string MapPath(string path)

{

    if (HostingEnvironment.IsHosted)

    {

        //hosted

        return HostingEnvironment.MapPath(path);

    }


    //not hosted. For example, run in unit tests

    string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

    path = path.Replace("~/", "").TrimStart('/').Replace('/', '\\');

    return Path.Combine(baseDirectory, path);

}  


0 Comments
댓글쓰기 폼