简体   繁体   English

我可以在ASP.NET网站的CodeFile之外的App_Code的C#文件中使用类/函数吗?

[英]Can I use class/function in C# file in App_Code besides the CodeFile for an asp.net website?

In an asp.net website, we can create a "CodeFile" for each .aspx file. 在asp.net网站上,我们可以为每个.aspx文件创建一个“ CodeFile”。 which contains a C# partial class. 其中包含一个C#局部类。 And this .aspx web can activate functions like 这个.aspx网站可以激活以下功能

protected void Create_Click(object sender, EventArgs e)

in this CodeFile for clicking a button. 在此CodeFile中单击按钮。

I have another C# project, which has about 20 C# files. 我还有另一个C#项目,其中包含大约20个C#文件。 What I`m doing now is call the .exe of the project to do some processing. 我现在正在做的是调用项目的.exe进行一些处理。 I was wondering is it possible that I can put the 20 C# files in the App_Code folder (or any other folder); 我想知道是否可以将20个C#文件放入App_Code文件夹(或任何其他文件夹)中; then in the codeFile, call the functions inside all the C# files to do the processing instead using a compiled .exe file in my website? 然后在codeFile中,调用所有C#文件内的函数进行处理,而不是使用我网站上的已编译.exe文件?

All the 20 C# files is in namespace xx, at least they have 所有20个C#文件都位于命名空间xx中,至少它们具有

namespace xx
{
    class example1
    {

in every files. 在每个文件中。 and I have have a "using xx" in the Codefile. 我在代码文件中有一个“ using xx”。 The VS2012 can find the namespace, but the intellicense cannot pickup the class or functions in my C# project. VS2012可以找到命名空间,但是智能感知无法在我的C#项目中获取类或函数。 So I was wondering if it`s doable or not. 所以我想知道它是否可行。

Yes, this is perfectly acceptable. 是的,这是完全可以接受的。

Alternatively, you could put them all in a Class Library (.dll) and reference that library in your website and call the classes that way. 或者,您可以将它们全部放在类库(.dll)中,并在您的网站中引用该库并以这种方式调用类。 You might need to change the classes to be public if they're not already (the example is internal because it is not explicitly declared as public). 如果尚未将这些类更改为公共类,则可能需要将其更改为公共类(该示例是内部的,因为未明确将其声明为公共类)。

To me, this would be preferable if the code here is likely to change, or you don't own it, or it might be reused else where. 对我来说,如果此处的代码可能会更改,或者您不拥有它,或者可能在其他地方重用,则这将是更好的选择。

Yes, You can add as many files as you want in the App_Code folder. 是的,您可以在App_Code文件夹中添加任意数量的文件。 This folder is for that purpose only. 该文件夹仅用于此目的。 And about Intelligence, try compiling your project and may be from the next run, It may add in the intelligence, Since Intelligence take some time to load all functions and stuffs. 关于Intelligence,请尝试编译您的项目,并且可能会在下一次运行中进行编译,这可能会增加Intelligence,因为Intelligence需要一些时间来加载所有功能和内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM