下面这个类该如何调用?为什么提示非静态方法或属性要求被引用?

liyifei123 2012-05-19 02:50:12
我想在主程序里面引用 links这里该怎么写是WebPage.links对吗?可是为什么提示非静态方法或属性要求被引用,是下面的类写的不用吗?
using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;

namespace Web
{
public class WebPage
{
public IList<string> m_links;

public IList<string> Links
{
get
{
if (m_links.Count == 0) getLinks();
return m_links;
}
}
public IList<string> getLinks()
{
m_links = new List<string>();
string result = @"<dd><a href=""http://www.baidu.com"" title=百度 target=_blank>百度</a></dd><dd><a href=""http://www.sohu.com"" title=搜狐 target=_blank>搜狐</a></dd>";
if (m_links.Count == 0)
{
Regex[] regex = new Regex[2];
regex[0] = new Regex(@"<a\shref\s*=""(?<URL>[^""]*).*?>(?<title>[^<]*)</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline);
regex[1] = new Regex("<[i]*frame[^><]+src=(\"|')?(?<url>([^>\"'\\s)])+)(\"|')?[^>]*>", RegexOptions.IgnoreCase);

for (int i = 0; i < 2; i++)
{
Match match = regex[i].Match(result);
while (match.Success)
{
try
{
string url = HttpUtility.UrlDecode(new Uri(match.Groups["URL"].Value).AbsoluteUri);
m_links.Add(url);
}
catch (Exception ex) { Console.WriteLine(ex.Message); };
match = match.NextMatch();
}
}
}
return m_links;
}
}

}
...全文
81 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyifei123 2012-05-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
只有static的定义才能直接调用。
WebPage mypage = new WebPage();
调用mypage.links
[/Quote]

我是这样调用的,IList<string> ttt = WebPage.Links;
请问您输出的代码是否是下面格式的?
List<string> ttt = new List<string>();
ttt.Add("http://www.baidu.com");
ttt.Add("http://www.sohu.com");
liyifei123 2012-05-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
因为不是声明为静态的在调用时都必须能过类的对象来调用。

如果是当前类内部的方法,你可以用过this来调用。


this是类的隐藏实例。
[/Quote]

我是这样调用的,IList<string> ttt = WebPage.Links;
请问您输出的代码是否是下面格式的?
List<string> ttt = new List<string>();
ttt.Add("http://www.baidu.com");
ttt.Add("http://www.sohu.com");
test2050 2012-05-19
  • 打赏
  • 举报
回复
只有static的定义才能直接调用。
WebPage mypage = new WebPage();
调用mypage.links
风之影子 2012-05-19
  • 打赏
  • 举报
回复
因为不是声明为静态的在调用时都必须能过类的对象来调用。

如果是当前类内部的方法,你可以用过this来调用。


this是类的隐藏实例。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧