开发者

Equivalent to PHP's include in C#

开发者 https://www.devze.com 2023-01-25 00:30 出处:网络
What is the equivalent command to PHP\'s include() in C# ? For exam开发者_如何学编程ple, PHP\'s include is used as so : include(\"ex.php\");

What is the equivalent command to PHP's include() in C# ?

For exam开发者_如何学编程ple, PHP's include is used as so : include("ex.php");

Can I do the same in C#?


If you mean in ASP.Net using C# you can create a user control (.ascx) and add it in your .aspx page.
If you are doing MVC you can create a partial view.

The closest thing I can think of would be after creating an ascx user control named "MyUserControl"

in your page_load or pre_render :

MyUserControl cont = new MyUserControl();
this.Controls.Add(cont);


There is no such thing in C#. It's not a scripting language, so including a block of script wouldn't make sense.

What are you trying to accomplish? There are ways to do similar things in C#.


There is no direct equivalent. You use references to "link" with other CLR assemblies (access their type information), and the using directive to import namespaces.

For example, the FontCollection class is in the System.Drawing.dll assembly, and the System.Drawing.Text namespace. So you would add System.Drawing as a reference, and add the line:

using System.Drawing.Text;


I'm not sure, if this is what you want to do. But just for the case, maybe you have a look at:

<%         
    Response.WriteFile( "YourFile.whatever" )
%>


In addition to previous answers mentioning the using Directive and adding references to assemblies to your project (or at command line when compiling) there is a way to load other compiled .NET assemblies at runtime.

Assembly.Load will load an assembly (compiled c# file/.dll) into memory, allowing you to find and use types within that assembly. This can be used when building a plugin architecture. You publish an assembly with an interface for a plugin contract. Plugin makers can link to that that assembly and implement your interface. Your application can then load plugin assemblies, check for any types implementing your plugin interface and load and use those types into your application.


The only thing comparable in C# is using, which imports namespaces defined in assemblies referenced from the project. You cannot "include" a file in the sense that you dump the content right into your code.

For example, if your project references the System.Xml assembly, then the following code would allow you to access all of the classes in that namespace without fully qualifying their names:

using System.Xml;

This will let you use the type System.Xml.XmlDocument, for example, by specifying it as XmlDocument instead of its full type name System.Xml.XmlDocument.


There is no such thing in C#.

You're going to want to create an instance of a C# class and use that to invoke methods/attributes from other 'packages' (C# classes).

You can also use a using direction to be able to references assemblies from other projects.


Using is vaguely similar. It references another class that can then be used from that file, but it doesn't include the contents of that file directly inline.

using system;

(right at the beginning of a file)


Use this for c# @RenderPage("header.cshtml")

This is taken from here: http://www.w3schools.com/aspnet/showfile_c.asp?filename=try_webpages_cs_002

Although I know this post is old, but people stubming to this post can refer to it.


Well, here is what I did ... not sure if this is the right way, but it works...

In the .ascx File, specify a div as container to received the contents of included file.

<div id="containerForSomeMarkup" runat="server"></div>

In the .ascx.cs file, initialise this in the onInit() method or other methods as required...

containerForSomeMarkup.InnerHtml = File.ReadAllText("Full path of file to be included");

Coming from PHP world, for me, this approach helps keep the markup organized and intelligble...


The using keyword is what you're looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消