简体   繁体   English

可公开访问的功能

[英]Publicly accessible functions

I'm creating an httphandler.我正在创建一个 httphandler。 The file type is probably irrelevant.文件类型可能无关紧要。

My.ashx file:我的.ashx 文件:

<%@ webhandler class="MyHandler" %>
<%@ assembly src="Functions.vb" %>
<%@ assembly src="Classes.vb" %>

Public Class MyHandler
  ...

End Class

My Functions.vb file:我的 Functions.vb 文件:

Module PublicFunctions

Function SayHello(greeting As String)
  ...

End Function
...

End Module

My Classes.vb file:我的 Classes.vb 文件:

Imports This
Imports That
Imports TheOther
...

Enum AnEnum
End Enum
...

Class AClass
End Class
...

In my classes file, I'd like to be able to use the functions in Functions.vb without wrapping them in a class so I can call them directly like在我的类文件中,我希望能够使用 Functions.vb 中的函数,而无需将它们包装在 class 中,因此我可以像这样直接调用它们

SayHello("Hello World")

instead of代替

MyPublicFunctions.SayHello("Hello World")

Is it possible?可能吗?

This is a fundamental idea of object oriented programming.这是 object 面向编程的基本思想。 Every function, every bit of code belongs somewhere and needs to be organized.每一个 function,每一位代码都属于某个地方,需要组织起来。 One way to do this is through the use of objects (classes).一种方法是通过使用对象(类)。 If you have a function you would like to preform, conciser what that function is doing and what is using it, and place it in the appropriate place.如果你有一个 function 你想预制,简明 function 正在做什么以及正在使用它,并将其放置在适当的位置。

I would also suggest not implementing an HttpHandler.我还建议不要实现 HttpHandler。 In the Asp.Net framework, just about anything you would ever want a web app to do is already handled by something somewhere.在 Asp.Net 框架中,几乎所有您希望 web 应用程序执行的操作都已由某个地方处理。 I've been on a lot of web projects, and every time I see someone trying to write one, their is always another (usually out of the box) way to do it.我参与了很多 web 项目,每次我看到有人试图写一个,他们总是用另一种(通常是开箱即用的)方式来做。

You could turn "SayHello" into an extension method with the type of the handler as the parameter type but you'll probably still have to specify Me.SayHello() .您可以将“SayHello”转换为以处理程序类型作为参数类型的扩展方法,但您可能仍需要指定Me.SayHello()

Does

Imports PublicFunctions

help at all?有帮助吗?

EDIT - since that didn't work, try converting PublicFunctions to a class, make its members Shared (so that they work without an instance of the class, the equivalent of c# static), and do the import.编辑- 由于这不起作用,请尝试将 PublicFunctions 转换为 class,使其成员共享(以便它们在没有 class 实例的情况下工作,相当于 c#,并进行静态导入)。 This is working for me, although I am not in an ASP environment.这对我有用,虽然我不在 ASP 环境中。

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

相关问题 注入的依赖项是公共可访问还是私有? - Should injected dependencies be publicly accessible or private? 如何检查可公开访问的网址? - How can I check a url is publicly accessible? 是否可以创建一个只能作为另一个类的成员实例但仍可以通过VB.NET公开访问的类? - Is it possible to create a class that can only be instanced as member of another class, but still be publicly accessible through this in VB.NET? 为什么 AWS App runner 在可公开访问时无法连接到 RDS Postgres 数据库? - Why AWS App runner can't connect to RDS Postgres database when it is publicly accessible? 是否有任何公共可用的SAML身份提供程序? - Are there any publicly available SAML Identity Providers? 是否可以通过互联网公开托管RabbitMQ服务器? - Is it possible to host a RabbitMQ Server publicly over the internet? 聚合根类是否仅保存公开的方法 - Does Aggregate root class holds only publicly exposed methods 为什么`Assembly`和`Module`没有公开定义的构造函数? - Why do `Assembly` and `Module` have no publicly defined constructors? 如何公开公开控件的属性? - How do I expose a control's property publicly? 无法将X汇总到Count() - Aggregate X Into Count() is not accessible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM