473,221 Members | 2,108 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

@import vs. @assembly

All,

I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good
content but am confused about the difference. From the text:

----------------------------------------------------------------------------------------------------------------------------------------------------------
The @ Import Directive
Next to @ Page, the directive that ASP.NET programmers use the most is @
Import. The @ Import directive is ASP.NET's equivalent of C#'s using
directive. Its purpose is to import a namespace so that the types in that
namespace are known to the compiler. You need @ Import any time you use an
FCL data type that's defined in a namespace that ASP.NET doesn't import by
default. For example, the statement

<%@ Import Namespace="System.Data" %>makes all the data types defined in
System.Data available to a Web form.

What namespaces does ASP.NET import by default? Here's a complete list:

a.. System

b.. System.Collections

c.. System.Collections.Specialized

d.. System.Configuration

e.. System.IO

f.. System.Text

g.. System.Text.RegularExpressions

h.. System.Web

i.. System.Web.Caching

j.. System.Web.Security

k.. System.Web.SessionState

l.. System.Web.UI

m.. System.Web.UI.HtmlControls

n.. System.Web.UI.WebControls

Because System.Data isn't imported automatically, you must import it
yourself if you want to use System.Data types (for example, DataSet) in a
Web form. Otherwise, you'll receive an error message the first time ASP.NET
attempts to compile the page. System.Web.Mail is another example of a
commonly used namespace that isn't imported automatically. Look back at
Chapter 3's SendMail program (Figure 3-7), and you'll see an @ Import
statement importing System.Web.Mail on the very first line of the ASPX file.

Unlike @ Page, @ Import can appear multiple times in a Web page. The
following statements import three namespaces and are often used together in
ASPX files that access SQL Server databases:

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.SqlTypes" %>The @ Assembly Directive
The @ Import directive identifies namespaces containing an application's
data types; @ Assembly identifies assemblies. The .NET Framework class
library is implemented in a series of single-file assemblies: Mscorlib.dll,
System.dll, and others. If ASP.NET is to compile your page, it must know
which assemblies the page references so that it can provide that information
to the compiler. The following assembly names are provided to the compiler
by default and therefore require no @ Assembly directive:

a.. Mscorlib.dll

b.. System.dll

c.. System.Data.dll

d.. System.Drawing.dll

e.. System.EnterpriseServices.dll

f.. System.Web.dll

g.. System.Web.Services.dll

h.. System.Xml.dll

These assemblies include the data types that Web forms are most likely to
use. But suppose you want to use the FCL's
System.DirectoryServices.DirectorySearcher class in a <script> block to
perform a query against Active Directory. Because DirectorySearcher lives in
an assembly (System.DirectoryServices.dll) that ASP.NET doesn't reference by
default, its use requires an @ Assembly directive. In the following example,
@ Import is required also because DirectorySearcher is defined in a
nondefault namespace:

<%@ Import Namespace="System.DirectoryServices" %>
<%@ Assembly Name="System.DirectoryServices" %>It's coincidental that the
namespace name and assembly name are one and the same; that's not always the
case. Note that an assembly name passed to @ Assembly must not include the
filename extension (.dll). In addition, the list of "default" assemblies can
be changed by editing a machine-wide configuration file named Machine.config
or augmented by dropping a Web.config file containing an <assemblies>
section into an application root. Like @ Import, @ Assembly can appear
multiple times in a Web page.

----------------------------------------------------------------------------------------------------------------------------------------------------------
My question - what's the point of having the two directives? It seems that
@ Import provides the information needed to reference the FCL classes. Why
do you, then, need to specify an Assembly, too? Isn't that redundant?

TIA,

-bruce


Nov 18 '05 #1
4 6037
Hi Bruce:

You need the @ Assembly attribute to tell the compiler where the types
live. The compiler must know where the compiled code exists (what dll)
for the types you need to use.

@ Import is not required. You could reference a class like:

System.Data.SqlClient.SqlConnection connection;

But by importing the System.Data.SqlClient namespace, you can save
some typing and use:

SqlConnection connection;

The SqlConnection class (from the System.Data.SqlClient namespace)
could live in an assembly called foo.dll (it doesn't, but it could).

Since there is no relation between assembly name and the namespace(s)
it contains, the @ Assembly directive is still required to point the
compiler to the right assembly.

I'm not Petzold, so I hope this helps :)

--
Scott
http://www.OdeToCode.com

----------------------------------------------------------------------------------------------------------------------------------------------------------
My question - what's the point of having the two directives? It seems that
@ Import provides the information needed to reference the FCL classes. Why
do you, then, need to specify an Assembly, too? Isn't that redundant?

TIA,

-bruce


Nov 18 '05 #2
Hi Scott,

Ahh ... OK, that makes a little more sense. I'm just now starting to learn
about ASP.Net. So far I've been doing mostly WinForms development in C#
where you don't have to do the dual-reference like that; you can just say
#using <whatever> and the fixups are taken care of. Apparently (according
to what you and Petzold are saying) the #using (that is @imports) do
essentially the same thing as far as coding is concerned but ASP.Net can't
infer which DLL to load from the namespace alone. I guess the C# compiler
for WinForms understands by itself what DLL to pull in based only on the
namespace and reference in your code - right?

Thanks,

-bruce

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:pn********************************@4ax.com...
Hi Bruce:

You need the @ Assembly attribute to tell the compiler where the types
live. The compiler must know where the compiled code exists (what dll)
for the types you need to use.

@ Import is not required. You could reference a class like:

System.Data.SqlClient.SqlConnection connection;

But by importing the System.Data.SqlClient namespace, you can save
some typing and use:

SqlConnection connection;

The SqlConnection class (from the System.Data.SqlClient namespace)
could live in an assembly called foo.dll (it doesn't, but it could).

Since there is no relation between assembly name and the namespace(s)
it contains, the @ Assembly directive is still required to point the
compiler to the right assembly.

I'm not Petzold, so I hope this helps :)

--
Scott
http://www.OdeToCode.com

----------------------------------------------------------------------------------------------------------------------------------------------------------
My question - what's the point of having the two directives? It seems
that
@ Import provides the information needed to reference the FCL classes.
Why
do you, then, need to specify an Assembly, too? Isn't that redundant?

TIA,

-bruce

Nov 18 '05 #3
Hi Paul:
For WinForms there is Project -> Add Reference to tell the compiler to
add another assembly. You can see the referenced assembly in the
Solution Explorer window underneth the project references node. the
#using directive and @ Imports do the same job, yes.

--
Scott
On Sun, 22 Aug 2004 07:25:57 -0400, "Bruce W. Roeser"
<br*****@cfl.rr.com> wrote:
Hi Scott,

Ahh ... OK, that makes a little more sense. I'm just now starting to learn
about ASP.Net. So far I've been doing mostly WinForms development in C#
where you don't have to do the dual-reference like that; you can just say
#using <whatever> and the fixups are taken care of. Apparently (according
to what you and Petzold are saying) the #using (that is @imports) do
essentially the same thing as far as coding is concerned but ASP.Net can't
infer which DLL to load from the namespace alone. I guess the C# compiler
for WinForms understands by itself what DLL to pull in based only on the
namespace and reference in your code - right?

Thanks,

-bruce

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:pn********************************@4ax.com.. .
Hi Bruce:

You need the @ Assembly attribute to tell the compiler where the types
live. The compiler must know where the compiled code exists (what dll)
for the types you need to use.

@ Import is not required. You could reference a class like:

System.Data.SqlClient.SqlConnection connection;

But by importing the System.Data.SqlClient namespace, you can save
some typing and use:

SqlConnection connection;

The SqlConnection class (from the System.Data.SqlClient namespace)
could live in an assembly called foo.dll (it doesn't, but it could).

Since there is no relation between assembly name and the namespace(s)
it contains, the @ Assembly directive is still required to point the
compiler to the right assembly.

I'm not Petzold, so I hope this helps :)

--
Scott
http://www.OdeToCode.com

----------------------------------------------------------------------------------------------------------------------------------------------------------
My question - what's the point of having the two directives? It seems
that
@ Import provides the information needed to reference the FCL classes.
Why
do you, then, need to specify an Assembly, too? Isn't that redundant?

TIA,

-bruce


--
Scott
http://www.OdeToCode.com
Nov 18 '05 #4
Many thanks, Scott. Yeah, I think I get it. I guess I expected it to be a
little more automatic - I.E. if the namespace reference works to allow me to
reference an object I would have thought this would also describe (lower
level, perhaps) to the compiler where to find the code associated with it.

-bruce

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:pn********************************@4ax.com...
Hi Bruce:

You need the @ Assembly attribute to tell the compiler where the types
live. The compiler must know where the compiled code exists (what dll)
for the types you need to use.

@ Import is not required. You could reference a class like:

System.Data.SqlClient.SqlConnection connection;

But by importing the System.Data.SqlClient namespace, you can save
some typing and use:

SqlConnection connection;

The SqlConnection class (from the System.Data.SqlClient namespace)
could live in an assembly called foo.dll (it doesn't, but it could).

Since there is no relation between assembly name and the namespace(s)
it contains, the @ Assembly directive is still required to point the
compiler to the right assembly.

I'm not Petzold, so I hope this helps :)

--
Scott
http://www.OdeToCode.com

----------------------------------------------------------------------------------------------------------------------------------------------------------
My question - what's the point of having the two directives? It seems
that
@ Import provides the information needed to reference the FCL classes.
Why
do you, then, need to specify an Assembly, too? Isn't that redundant?

TIA,

-bruce

Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason Robertson | last post by:
Hi, I am using Web Matrix Project as .NET programming environment. Can you show me detailed steps how to import the .ocx library into the Web Matrix Project. I can't seem to be able to import...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
6
by: SteveS | last post by:
Hello All. I have an asp.net application with 3 different assemblies. They are like this: 1) Assembly: PublicSite (This contains the website UI) Root namespace: PublicSite 2) Assembly:...
1
by: | last post by:
Hi all I can't import some namespace with non-codebehind page.. for example. <%@ Import Namespace="System.ServiceProcess"%> In code-behind,we have to reference System.ServiceProcess.dll...
5
by: Greg Collins [MVP] | last post by:
I have an ASP.NET page that uses a tag: <asp:Xml id="foo" runat="server" DocumentSource="rss.xml" TransformSource="rss20.xsl" /> This creates a Web page from an XML file that was generated by...
5
by: Sharon Tal | last post by:
Compiler Error Message: CS0234: The type or namespace name 'Http' does not exist in the class or namespace 'System.Runtime.Remoting.Channels' (are you missing an assembly reference?) Source...
7
by: Steve Richter | last post by:
I am attempting to use the IBM DB2Connection class in my .aspx web page. Using Visual Web Developer 2005 express, I cant figure out how to add a reference to a project. heck, I cant figure out...
3
by: Lee | last post by:
If Assembly A references Assembly B and I include Assembly A in my project, does it implicitly import Assembly B? -- Warm Regards, Lee "Upon further investigation it appears that your...
2
by: Ebbe Kristensen | last post by:
Hi All I am trying to get access to an assembly in an ASPX project. I have added the assembly to GAC using gacutil and added an "%@Import namespace = "MyNamespace" %" to the .aspx file but this...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.