让我们一起为asp.net构建一个通用的实用程序类

时间:2010-09-01 10:31:09

标签: asp.net class

您希望将哪种方法放在asp.net应用程序的tyour公共类中?几乎所有asp.net项目问题中都使用的常用函数。

5 个答案:

答案 0 :(得分:3)

这些应该总是在那里IMO:

/// <summary>
/// Answers true if this String is not null or empty
/// </summary>
public static bool HasValue(this string s)
{
  return !string.IsNullOrEmpty(s);
}

/// <summary>
/// Answers true if this String is either null or empty.
/// </summary>
public static bool IsNullOrEmpty(this string s)
{
  return string.IsNullOrEmpty(s);
}

因为返回来输入string.IsNullOrEmpty(value)而不是继续,感觉非常不自然,至少对我来说是这样。爱它或恨它,给我(我就是那个使用它的人......)很清楚阅读并节省了大量的时间,击键和挫折。

是的,这很可能很快就会关闭,但是,我觉得对于缺乏基本字符串函数感到半咆哮:)

答案 1 :(得分:1)

public static bool TryFindControl<T>(this Control control, string id, out T foundControl) where T : class
{
   return (foundControl = control.FindControl(id) as T) != null;
}

答案 2 :(得分:0)

首先让我开始,这是我在几乎所有asp.net项目中使用的函数 功能:检查字符串是否为数字

 public bool IsNumeric(string str)
{
    if (str == null || str.Length == 0)
        return false;
    System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
    byte[] bytestr = ascii.GetBytes(str);
    foreach (byte c in bytestr)
    {
        if (c < 48 || c > 57)
        {
            return false;
        }
    }
    return true;
}

答案 3 :(得分:0)

具有功能的“通用”类通常不是好的设计。方法应该属于封装特定功能的类。

但是,一个例外是包含扩展方法的静态类,我通常有这种模式

public static class EnumerableExtensions{} // all extensions for IEnumerable<T>
public static class StringExtensions{} // all extensions for String.

答案 4 :(得分:0)

很长一段时间我从互联网上得到这个功能,我仍然使用它,并不总是生成随机密码; - )

 public static string GeneratePassword(int passwordLength)
    {

        int iZero = 0, iNine = 0, iA = 0, iZ = 0, iCount = 0, iRandNum = 0;
        string sRandomString = string.Empty;

        //' we'll need random characters, so a Random object 
        //' should probably be created...
        Random rRandom = new Random(System.DateTime.Now.Millisecond);

        //' convert characters into their integer equivalents (their ASCII values)
        iZero = Convert.ToInt32("0");
        iNine = Convert.ToInt32("9");
        iA = Convert.ToInt32('A');
        iZ = Convert.ToInt32('Z');

        //' initialize our return string for use in the following loop
        sRandomString = string.Empty;


        //' now we loop as many times as is necessary to build the string 
        //' length we want
        while (iCount < passwordLength)
        {
            //' we fetch a random number between our high and low values
            iRandNum = rRandom.Next(iZero, iZ);

            // ' here's the cool part: we inspect the value of the random number, 
            // ' and if it matches one of the legal values that we've decided upon,  
            // ' we convert the number to a character and add it to our string
            if ((iRandNum >= iZero) && (iRandNum <= iNine) || (iRandNum >= iA) && (iRandNum <= iZ))
            {
                if (iRandNum >= iZero && iRandNum <= iNine)
                    sRandomString = sRandomString + iRandNum.ToString();
                else
                    sRandomString = sRandomString + Convert.ToChar(iRandNum);

                iCount = iCount + 1;
            }

        }
        //' finally, our random character string should be built, so we return it
        return sRandomString;


    }