C# 我可以从另一个部分类调用在一个部分类中定义的函数吗?是否可以?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2005825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can i call a function defined in one partial class from another partial class. Is it possible?
提问by Shantanu Gupta
I have created two partial classes for a web page.
我为一个网页创建了两个部分类。
Now i have given a defination of one function say submit() that i m calling at OnSubmit event of button.
现在我已经定义了一个函数,比如 submit(),我在按钮的 OnSubmit 事件中调用它。
But this function is not being called, the program doesnot compiles as it is not able to search the defination of function, which was defined in another partial class. Is it possible to call this function or i have to give defination of function in same file where i m calling it
但是这个函数没有被调用,程序没有编译,因为它无法搜索在另一个部分类中定义的函数的定义。是否可以调用这个函数,或者我必须在我调用它的同一个文件中给出函数的定义
eg
例如
<%@ Page Language="C#" MasterPageFile="~/Master Pages/LeftMenu.master" AutoEventWireup="true" CodeFile="Registration.aspx.cs" Inherits="Web_Pages_Authentication_Login_Management_Registration" Title="Registration" StylesheetTheme="Default Theme 1" %>
Registration.aspx.cs
注册.aspx.cs
public partial class Web_Pages_Authentication_Login_Management_Registration : System.Web.UI.Page
{
private string firstName;
private string lastName;
private string userName;
private string userPassword;
private string primaryEmail;
protected void btnSubmit_Click(object sender, EventArgs e)
{
Display();
}
}
Registration_Database.cs
注册_Database.cs
public partial class Web_Pages_Authentication_Login_Management_Registration
{
const string dbFirstName = "@FirstName";
const string dbLastName = "@LastName";
const string dbUserName= "@UserName";
const string dbUserPassword = "@UserPassword";
const string dbPrimaryEmail = "@PrimaryEmail";
void Display()
{
firstName="XYZ"; //This variable is not accessible when i put both files in different directories
}
}
I am getting following error
我收到以下错误
Error 1 'Web_Pages_Authentication_Login_Management_Registration' does not contain a definition for 'Display' and no extension method 'Display' accepting a first argument of type 'Web_Pages_Authentication_Login_Management_Registration' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\Administrator\Desktop\Online Test\Web Pages\Authentication\Login Management\Registration.aspx.cs 78 20 C:\...\Online Test\
Registration.aspx, Registration.aspx.cs, Registration_Database.cs are three files that are not in App_Code folder but belongs to one folder only out of which Registration.aspx.cs, Registration_Database.cs are partial classes and Registration.aspx is my design file. Plz let me know if u want to know some more info about my problem
Registration.aspx、Registration.aspx.cs、Registration_Database.cs 是三个不在 App_Code 文件夹中但只属于一个文件夹的文件,其中 Registration.aspx.cs、Registration_Database.cs 是部分类,Registration.aspx 是我设计的文件。如果您想了解有关我的问题的更多信息,请告诉我
I am not using DLL Files. No precompiled code
我没有使用 DLL 文件。没有预编译代码
采纳答案by Thorarin
I think the problem you're experiencing is because your website is dynamically compiled, rather than a precompiled web application.
我认为您遇到的问题是因为您的网站是动态编译的,而不是预编译的 Web 应用程序。
When the ASP.NET page is first accessed, the compiler will only look in the code behind file, and won't search all the other .cs files for possible extra code for the same class. Thus, it will never see the implementation of display()
, which in turn causes the code behind file to fail to compile.
当第一次访问 ASP.NET 页面时,编译器将只查看代码隐藏文件,而不会搜索所有其他 .cs 文件以查找同一类可能的额外代码。因此,它永远不会看到 的实现display()
,从而导致文件背后的代码无法编译。
I would certainly not use a method like this for what you seem to want to do, but you could define a custom class that derives from System.Web.UI.Page
and have your actual page derive from the new class. The class definition would have to be in App_Code
for a dynamically compiled website.
我当然不会将这样的方法用于您似乎想要做的事情,但是您可以定义一个自定义类,该类派生System.Web.UI.Page
自新类,并使您的实际页面从新类派生。类定义必须App_Code
用于动态编译的网站。
Registration.aspx.cs:
注册.aspx.cs:
public partial class Web_Pages_Authentication_Login_Management_Registration : MyPage
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
Display();
}
}
App_Code\MyPage.cs:
App_Code\MyPage.cs:
public class MyPage : System.Web.UI.Page
{
protected void Display()
{
// ...
}
}
In any case: you certainly shouldn't use static variables the way you are using them, this will cause problems if multiple people are using your site at the same time. Frankly, your example doesn't make much sense to me. Maybe if we had more information about what you're trying to accomplish, we could provide a best-practise way to do it.
在任何情况下:您当然不应该以您使用静态变量的方式使用它们,如果多人同时使用您的站点,这将导致问题。坦率地说,你的例子对我来说没有多大意义。也许如果我们有更多关于您要完成什么的信息,我们可以提供一种最佳实践方法来完成它。
Note that in your example, putting the second file in App_Code
directory will not make it work. That would simply result in two classes with the exact same class name (provided the namespaces match of course). It really doesn't like that :)
请注意,在您的示例中,将第二个文件放在App_Code
目录中不会使其工作。这只会导致两个类具有完全相同的类名(当然前提是命名空间匹配)。它真的不喜欢那样:)
回答by Fedor Hajdu
Of course you can. Like every other method in a class (partial or not) you call static methods by ClassName.MethodName() or non-static as creating instance of the class and call instanceName.MethodName()
当然可以。像类中的所有其他方法(部分或非部分)一样,您通过 ClassName.MethodName() 或非静态调用静态方法作为创建类的实例并调用 instanceName.MethodName()
EDIT: Sorry I didn't see your examples at first.
编辑:对不起,我一开始没有看到你的例子。
That should work. Partial class is split in files but it is compiled as one class. So, the second part of the same partial class can access private members defined in the other with no problems.
那应该工作。部分类被拆分成文件,但它被编译为一个类。因此,同一个分部类的第二部分可以毫无问题地访问另一个定义的私有成员。
回答by Mark Seemann
Yes, it is possible to call a method on a (partial) class.
是的,可以在(部分)类上调用方法。
Partial classes are only a feature of some languages (such as C#) - they don't exist in IL, as they are compiled together into a single class.
部分类只是某些语言(如 C#)的一个特性——它们在 IL 中不存在,因为它们被编译成一个单一的类。
However, it can sometimes be tricky since partial class declarations must be totally identical for this merging to happen. That includes the namespace, so this might be your problem.
然而,有时可能会很棘手,因为要进行这种合并,部分类声明必须完全相同。这包括 namespace,所以这可能是你的问题。
As an illustration, this will result in a single class:
例如,这将产生一个类:
namespace MyNamespace
{
public partial class MyClass { }
public partial class MyClass { }
}
Thre resulting class is MyNamespace.MyClass. However, this will result in two different classes:
结果类是 MyNamespace.MyClass。但是,这将导致两个不同的类:
namespace MyNamespace
{
public partial class MyClass
{
public void MyMethod();
}
}
namespace MyOtherNamespace
{
public partial class MyClass { }
}
This will produce two classes: MyNamespace.MyClass and MyOtherNamespace.MyClass. In this case, a call to this.MyMethod
from MyOtherNamespace.MyClass will not work.
这将产生两个类:MyNamespace.MyClass 和 MyOtherNamespace.MyClass。在这种情况下,this.MyMethod
从 MyOtherNamespace.MyClass调用将不起作用。
回答by Rune Grimstad
If I understand your question correctly, you have two files containing partial class definitions for the SAME class. Right? They are not two different classes?
如果我正确理解您的问题,您有两个文件包含 SAME 类的部分类定义。对?他们不是两个不同的类吗?
If both files are for the same class, then you should make sure they are both defined in the same name space and that the class names are spelled exactly the same. If the names match then this should work.
如果两个文件都属于同一个类,那么您应该确保它们都定义在相同的名称空间中,并且类名的拼写完全相同。如果名称匹配,那么这应该可以工作。
If your partial classes define two different classes then the same rules as apply as for regular classes. To reference a metod of another class the method must be public, and you either need a reference to an instance of that class or the method must be static.
如果您的部分类定义了两个不同的类,则适用于常规类的相同规则。要引用另一个类的方法,该方法必须是公共的,并且您要么需要对该类的实例的引用,要么该方法必须是静态的。
回答by Marc Gravell
This should be fine. If you want, you can formalise things using partial methods; declare the partial stub in one file, and implement it in the other. This will quickly identify if you have two classes or one, and will highlight attempts to implement a partial method that isn't defined:
这应该没问题。如果你愿意,你可以使用部分方法来形式化;在一个文件中声明部分存根,并在另一个文件中实现它。这将快速识别您是否有两个类或一个,并将突出显示实现未定义的部分方法的尝试:
partial class Foo
{
partial void Bar(string someArg);
// other code that uses Bar(s)
}
partial class Foo
{
partial void Bar(string someArg)
{
Console.WriteLine(someArg);
}
}
Note that partial
methods must be non-public, and can't have out
or non-void return types.
请注意,partial
方法必须是非公共的,并且不能有out
或非空的返回类型。