日期:2014-05-18  浏览次数:20684 次

下面这个类该如何调用?为什么提示非静态方法或属性要求被引用?
我想在主程序里面引用 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;
  }
  }
   
}


------解决方案--------------------
因为不是声明为静态的在调用时都必须能过类的对象来调用。

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


this是类的隐藏实例。
------解决方案--------------------
只有static的定义才能直接调用。
WebPage mypage = new WebPage();
调用mypage.links