473,396 Members | 1,758 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,396 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 6062
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.