现在的位置: 首页 > 综合 > 正文

C#如何写类库,并调用类库中的函数

2017年05月25日 ⁄ 综合 ⁄ 共 1697字 ⁄ 字号 评论关闭

创建一个新的项目,

选择类库,然后写举例如下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace publicfunction
{
    public class PathIsUrl
    {
        public static bool IsUrl(string url)
        {
            return Regex.IsMatch(url, @"^(((file|gopher|news|nntp|telnet|http|ftp|https|ftps|sftp)://)|(www\.))+(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(/[a-zA-Z0-9\&%_\./-~-]*)?$", RegexOptions.IgnoreCase);
        }

        public static bool SuffixIsMatch(string url)
        {
            string[] suffixs = new string[] {"gif","GIF","jpg","JPG","png","PNG","ico","ICO","css","CSS","sit","SIT","eps","EPS","wmf","WMF",
                "zip", "ZIP","ppt","PPT","mpg","MPG","xls","XLS","gz","GZ","rpm","RPM","tgz","TGZ","mov","MOV","exe","EXE","jpeg","JPEG",
                "bmp","BMP","js","JS"};
            Uri matchurl = new Uri(url);
            string pagename = matchurl.AbsolutePath;
            int count = 0;
            bool flag = false;
            for (int i = pagename.Length - 1; i >= 0; i--)
            {
                count++;
                if (pagename[i] == '.')
                {
                    flag = true;
                    break;
                }
            }
            if (flag == true)
            {
                string a = pagename.Substring(pagename.Length - (count - 1));
                for (int i = 0; i < suffixs.Length; i++)
                {
                    if (a.IndexOf(suffixs[i]) != -1)
                    {
                        return false;
                    }
                }
                return true;
            }
            else
            {
                return true;
            }
        }
    }
}

然后生成项目。便得到dll。

然后就是在测试程序中的引用中调用这个dll,

然后就可以使用了。

抱歉!评论已关闭.