- C#,一个对象引用是所必需的非静态字段,方法或财产 [英] C# - An object reference is required for the non-static field, method, or property

查看:629
本文介绍了 - C#,一个对象引用是所必需的非静态字段,方法或财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有两个功能,我发现了一个有趣的问题。基本上我的目标,使我的code在容易includeable CS文件更便于携带。

下面的说CS文件:

 命名空间basicFunctions {
公共部分类phpPort:System.Web.UI.Page {
    公共字符串includer(字符串文件名){
        字符串路径=使用Server.Mappath(./+文件名);
        字符串内容= System.IO.File.ReadAllText(路径);
        返回的内容;
    }
    公共无效returnError(){
        的Response.Write(< H2>!发生了错误< / H>);
        的Response.Write(< P>您遵循了不正确的链接,请仔细检查,然后再试一次< / P>中);
        的Response.Write(includer(footer.html));
        到Response.End();
    }
}
}

下面是引用它的页面:

 <%@Page LANGUAGE =C#调试=真正的继承=basicFunctions.phpPortcodeFILE =basicfunctions.cs%GT;
<%@Import命名空间=System.Web.Configuration%GT;< SCRIPT LANGUAGE =C#=服务器>
无效的Page_Load(对象发件人,EventArgs的发送){
    的Response.Write(basicFunctions.phpPort.includer(header.html中));
    //无关code
    如果('的事情发生'){
        basicFunctions.phpPort.returnError();
    }
    的Response.Write(basicFunctions.phpPort.includer(footer.html));
}
< / SCRIPT>

我得到的错误是上面列出的一个,即:

 编译器错误信息:CS0120:一个对象引用所需的非静态字段,方法或属性basicFunctions.phpPort.includer(字符串)


解决方案

您需要为您的 phpPort 类的一个实例,并已在其上定义的所有方法都不是一成不变的。

既然你在 ASPX 页的继承的这一类,在加载时它已经的的一个类的实例,你可以直接调用它的方法。

您需要修改code直接使用功能:

 无效的Page_Load(对象发件人,EventArgs的发送){
    的Response.Write(includer(header.html中));
    //无关code
    如果('的事情发生'){
        returnError();
    }
    的Response.Write(includer(footer.html));
}

So I have two functions and I'm getting an interesting problem. Essentially I'm aiming to make my code more portable in an easily includeable cs file.

Here's said cs file:

namespace basicFunctions {
public partial class phpPort : System.Web.UI.Page {
    public string includer(string filename) {
        string path = Server.MapPath("./" + filename);
        string content = System.IO.File.ReadAllText(path);
        return content;
    }
    public void returnError() {
        Response.Write("<h2>An error has occurred!</h2>");
        Response.Write("<p>You have followed an incorrect link. Please double check and try again.</p>");
        Response.Write(includer("footer.html"));
        Response.End();
    }
}
}

Here is the page that is referencing it:

<% @Page Language="C#" Debug="true" Inherits="basicFunctions.phpPort" CodeFile="basicfunctions.cs" %>
<% @Import Namespace="System.Web.Configuration" %>

<script language="C#" runat="server">
void Page_Load(object sender,EventArgs e) {
    Response.Write(basicFunctions.phpPort.includer("header.html"));
    //irrelevant code
    if ('stuff happens') {
        basicFunctions.phpPort.returnError();
    }
    Response.Write(basicFunctions.phpPort.includer("footer.html"));
}
</script>

The error I'm getting is the one listed above, namely:

Compiler Error Message: CS0120: An object reference is required for the non-static field, method, or property 'basicFunctions.phpPort.includer(string)'

解决方案

You need an instance of your phpPort class as it and all the methods you have defined on it are not static.

Since you are on the aspx page that inherits from this class, when it loads it already is an instance of the class and you can call the methods on it directly.

You need to modify your code to use the functions directly:

void Page_Load(object sender,EventArgs e) {
    Response.Write(includer("header.html"));
    //irrelevant code
    if ('stuff happens') {
        returnError();
    }
    Response.Write(includer("footer.html"));
}

这篇关于 - C#,一个对象引用是所必需的非静态字段,方法或财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆