- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C# 的新手,我在类里面遇到这个错误。
Extension method must be defined in a non-generic static class
我刚刚搜索并发现了很多可能导致此错误的原因..例如将静态添加到类名和其他..你能帮我找到为什么这个错误出现在类上吗?
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace LandManagment
{
class weather
{
private string xml;
private string FullTextXML;
private string PartTextXML;
public List<Forcast> forcastlist;
public Wind wind;
public Astronomy astronomy;
public Atmosphere atmosphere;
public Condition condition;
public struct Forcast
{
public Forcast(string day, string date, string low, string high, string text, string code)
{
Day = day;
Date = date;
Low = low;
High = high;
Text = text;
Code = code;
}
public string Day { get; private set; }
public string Date { get; private set; }
public string Low { get; private set; }
public string High { get; private set; }
public string Text { get; private set; }
public string Code { get; private set; }
}
public struct Wind
{
public Wind(string chill, string direction, string speed)
{
Chill = chill;
Direction = direction;
Speed = speed;
}
public string Chill { get; private set; }
public string Direction { get; private set; }
public string Speed { get; private set; }
}
public struct Astronomy
{
public Astronomy(string sunrise, string sunset)
{
Sunrise = sunrise;
Sunset = sunset;
}
public string Sunrise { get; private set; }
public string Sunset { get; private set; }
}
public struct Atmosphere
{
public Atmosphere(string humidity, string visibility, string pressure, string rising)
{
Humidity = humidity;
Visibility = visibility;
Pressure = pressure;
Rising = rising;
}
public string Humidity { get; private set; }
public string Visibility { get; private set; }
public string Pressure { get; private set; }
public string Rising { get; private set; }
}
public struct Condition
{
public Condition(string text, string code, string temp)
{
Text = text;
Code = code;
Temp = temp;
}
public string Text { get; private set; }
public string Code { get; private set; }
public string Temp { get; private set; }
}
private string GetStr(string str) {
try {
int index1;
int index2;
index1 = (str.IndexOf("<![CDATA[") + "<![CDATA[".Length);
index2 = str.IndexOf("]]>");
string str1 = str.Substring(index1, (index2 - index1));
// MsgBox(str1)
return str1;
}
catch (Exception ex) {
return "Error";
}
}
private string getXML() {
return FullTextXML;
}
private void GetWind() {
int index1;
int index2;
string NewString;
index1 = (PartTextXML.IndexOf("<yweather:wind") + "<yweather:wind".Length);
NewString = PartTextXML.Substring(index1);
index2 = NewString.IndexOf("/>");
string TodayWind= NewString.Substring(0, index2).ToLower();
//speed
index1 = TodayWind.IndexOf("speed");
string NewStr = TodayWind.Substring(index1);
index2 = NewStr.IndexOf(" ");
string ss = NewStr.Substring(0, index2);
string[] arr = ss.Split('=');
string speed = FormatString(arr[1].Replace("\"", ""));
//direction
index1 = TodayWind.IndexOf("direction");
NewStr = TodayWind.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
string direction = FormatString(arr[1].Replace("\"", ""));
//chill
index1 = TodayWind.IndexOf("chill");
NewStr = TodayWind.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
string chill = FormatString(arr[1].Replace("\"", ""));
wind = new Wind(speed, direction, chill);
}
public void GetAstronomy() {
int index1;
int index2;
string NewString;
index1 = (PartTextXML.IndexOf("<yweather:astronomy") + "<yweather:astronomy".Length);
NewString = PartTextXML.Substring(index1);
index2 = NewString.IndexOf("/>");
String TodayAstronomy = NewString.Substring(0, index2).ToLower();
//sunrise
index1 = TodayAstronomy.IndexOf("sunrise");
string NewStr = TodayAstronomy.Substring(index1);
index2 = NewStr.IndexOf(" am");
string ss = NewStr.Substring(0, index2);
string[] arr = ss.Split('=');
String sunrise = (FormatString(arr[1].Replace("\"", "")) + "am");
//sunset
index1 = TodayAstronomy.IndexOf("sunset");
NewStr = TodayAstronomy.Substring(index1);
index2 = NewStr.IndexOf(" pm");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
String sunset = (FormatString(arr[1].Replace("\"", "")) + "pm");
astronomy = new Astronomy(sunrise, sunset);
}
private void GetAtmosphere()
{
int index1;
int index2;
string NewString;
index1 = (PartTextXML.IndexOf("<yweather:atmosphere") + "<yweather:atmosphere".Length);
NewString = PartTextXML.Substring(index1);
index2 = NewString.IndexOf("/>");
string TodayAtmosphere = NewString.Substring(0, index2).ToLower();
//humidity
index1 = TodayAtmosphere.IndexOf("humidity");
string NewStr = TodayAtmosphere.Substring(index1);
index2 = NewStr.IndexOf(" ");
string ss = NewStr.Substring(0, index2);
string[] arr = ss.Split('=');
string humidity = FormatString(arr[1].Replace("\"", ""));
//visibility
index1 = TodayAtmosphere.IndexOf("visibility");
NewStr = TodayAtmosphere.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
string visibility = FormatString(arr[1].Replace("\"", ""));
//pressure
index1 = TodayAtmosphere.IndexOf("pressure");
NewStr = TodayAtmosphere.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
string pressure = FormatString(arr[1].Replace("\"", ""));
//rising
index1 = TodayAtmosphere.IndexOf("rising");
NewStr = TodayAtmosphere.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
string rising = FormatString(arr[1].Replace("\"", ""));
atmosphere = new Atmosphere(humidity, visibility, pressure, rising);
}
private void GetCondition()
{
int index1;
int index2;
string NewString;
index1 = (PartTextXML.IndexOf("<yweather:condition") + "<yweather:condition".Length);
NewString = PartTextXML.Substring(index1);
index2 = NewString.IndexOf("/>");
String TodayCondition = NewString.Substring(0, index2).ToLower();
//temp
index1 = TodayCondition.IndexOf("temp");
string NewStr = TodayCondition.Substring(index1);
index2 = NewStr.IndexOf(" ");
string ss = NewStr.Substring(0, index2);
string[] arr = ss.Split('=');
string temp = FormatString(arr[1].Replace("\"", ""));
//code
index1 = TodayCondition.IndexOf("code");
NewStr = TodayCondition.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
string code = FormatString(arr[1].Replace("\"", ""));
//text
index1 = TodayCondition.IndexOf("text");
NewStr = TodayCondition.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
string text = FormatString(arr[1].Replace("\"", ""));
condition = new Condition(text, code, temp);
}
public void GetForecast()
{
for (int i = 0; i < 2; i++)
{
int index1;
int index2;
string NewString;
index1 = (NthIndexOf(PartTextXML, "<yweather:astronomy", i+1) + "<yweather:astronomy".Length);
NewString = PartTextXML.Substring(index1);
index2 = NewString.IndexOf("/>");
string forc=NewString.Substring(0, index2).ToLower();
//low
index1 = forc.IndexOf("low");
string NewStr = forc.Substring(index1);
index2 = NewStr.IndexOf(" ");
string ss = NewStr.Substring(0, index2);
string[] arr = ss.Split('=');
String Low = arr[1].Replace("\"", "");
//high
index1 = forc.IndexOf("high");
NewStr = forc.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
String High = arr[1].Replace("\"", "");
//text
index1 = forc.IndexOf("text");
NewStr = forc.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
String text = arr[1].Replace("\"", "");
//text
index1 = forc.IndexOf("code");
NewStr = forc.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
String code = arr[1].Replace("\"", "");
//day
index1 = forc.IndexOf("day");
NewStr = forc.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
String day = arr[1].Replace("\"", "");
//date
index1 = forc.IndexOf("date");
NewStr = forc.Substring(index1);
index2 = NewStr.IndexOf(" ");
ss = NewStr.Substring(0, index2);
arr = ss.Split('=');
String date = arr[1].Replace("\"", "");
forcastlist.Insert(i,new Forcast(day,date,Low,High,text,code));
}
}
private static int NthIndexOf(this string target, string value, int n)
{
Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}");
if (m.Success)
return m.Groups[2].Captures[n - 1].Index;
else
return -1;
}
public weather(string w, string U) {
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
string str = ("http://xml.weather.yahoo.com/forecastrss?w="
+ (w + ("&u=" + U)));
try {
// Load data
doc.Load(str);
str = doc.OuterXml;
FullTextXML = doc.OuterXml;
PartTextXML = GetStr(str);
this.GetAstronomy();
this.GetAtmosphere();
this.GetCondition();
this.GetForecast();
}
catch (Exception ex) {
str = "null";
FullTextXML = "null";
throw new Exception("Null XML");
}
}
private string FormatString(String sss) {
char c = sss[0];
c = c.ToString().ToUpper()[0];
string str="";
str = (c + sss.Substring(1));
return str;
}
}
}
我尝试将public static class
更改为Extension methods must be defined in a non-generic static class说但得到错误:(谢谢你的帮助
最佳答案
由于NthIndexOf
是一个扩展方法,所以需要定义在public static class
中。我怀疑您已将 weather
设为 public static class
例如:
public static class weather
{
}
但是,这不会起作用,因为 weather
包含非静态实例成员,例如
private string xml;
private string FullTextXML;
private string PartTextXML;
public List<Forcast> forcastlist;
// etc...
您应该做的是创建一个单独的类来存储您的扩展方法,例如:
public static class StringExtensions
{
public static int NthIndexOf(this string target, string value, int n)
{
Match m = Regex.Match(target, "((" + value + ").*?){" + n + "}");
if (m.Success)
return m.Groups[2].Captures[n - 1].Index;
else
return -1;
}
}
你可以像这样从你的天气
类中调用它:
int y = PartTextXML.NthIndexOf("<yweather:astronomy", i + 1);
关于c# - 方法必须在非泛型静态类中定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24861559/
可以用这种方式转换字符串吗?我们有相同的参数,Java 做出了正确的选择。如果值是整数 - 我们调用 parseInt(value),否则如果值是 double 型 - 我们调用 parseDoubl
如果这段代码中有一个愚蠢的错误,我提前道歉,但我似乎无法解决它。我的问题是这样的,我用GCC-8(通过home-brew安装在Mac上)编译,然后在终端中执行。当使用 int do 定义变量 s &
我用 a-videosphere 制作了一个 a-scene。我尝试使用按钮启用/禁用声音,但有些想法不起作用?这是我的代码: var gargamel = 0; function
我正在使用 ISAAC 实现来生成随机整数。我需要用这些整数创建一个高斯值。首先,我需要将它们从 0 更改为 1 的 double 值。我怎样才能在Java中做到这一点?这是到目前为止我将整数转换为
我将 0x0000 到 0x01c2 范围内的十六进制值从 BLE 获取到我的手机 a 作为字符串。为了将其绘制在图表中,我必须将其转换为 double,我已经尝试过 this method但遗憾的是
我有一个父类(super class) Animal和一个子类 Dog 。在第三节课中,我有一个 List它同时接受子类型和父类(super class)型对象。 public class foo{
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我正在尝试查询我的用户的距离。我可以用这段代码做到这一点 PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint: PFGe
考虑示例:http://jsfiddle.net/KWLu7/16/ 我正在尝试将总体重要性值计算为每个选定选择重要性的总和乘以其父标准重要性: var watch = $scope.$watch("
这个问题在这里已经有了答案: Bounding generics with 'super' keyword (6 个答案) 关闭 2 年前。 我有一个列表装饰器,它应该允许从一个列表转换到另一个列表
为什么下面的代码没有选择最近父类(super class)型的隐式 val? class A class B extends A trait TC[-T] { def show(t: T): Stri
这是我想要做的 def merge[A, B, C](eithers: Either[A,B]*)(implicit ev1: A x, x => x)) 关于scala - 推断常见的父类(s
我正在尝试从具有 double 类型列的Cassandra表中获取 double 值。我已经使用CQL3语法创建了表: CREATE TABLE data_double ( datetime
是否应该在不需要显式类型定义的情况下编译以下 this ? def prepList[B >: A](prefix: PlayList[B]) : PlayList[B] = prefix.fol
我正在查看某人的代码,并且在创建结构时使用了 abstract type AbstractFoo end julia> struct Foo1 struct Foo2 foo_op(x::Abst
一些示例代码: public class Main { class SomeType { } class A { protected T createSome
是否可以只接受类的泛型类型的父类(super class)型? 我正在寻找的是这样的: class MyClass { public void myMethod(TS someObject
在我的代码中,我有许多 ArrayList 被传递到排序方法中。每个 ArrayList 都有不同的泛型类型,但所有这些类型都是 Sorter 的实现。排序方法旨在接受 Sorter 类型的 Arra
如果已经有人问过这个问题,请链接并关闭这个问题。 我目前正在为另一个使用起来复杂得多(并且有潜在危险)的 API 的简化 API 设计原型(prototype)。 考虑到相关的有点复杂的对象创建,我决
我正在尝试构建一个具有某些依赖项的 android 应用程序,但是其中一个导致了此错误: Illegal class file: Class module-info is missing a supe
我是一名优秀的程序员,十分优秀!