public bool ValidateEmail(string email) {
string strRegEx = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex regex = new Regex(strRegEx);
if (regex.IsMatch(email)) return (true);
else return (false);
}
}
}
Instead of using Visual Studio 2008 to build the project into an assembly, use the C# compiler to manually compile it into a module.
To use the C# compiler, launch the Visual Studio 2008 Command Prompt (Start→Programs→Microsoft Visual Studio 2008→Visual Studio Tools→Visual Studio 2008 Command Prompt).
Navigate to the folder containing the StringUtil
project, and type in the following command to create a new module:
csc /target:module /out:StringUtil.netmodule Class1.cs
When the compilation is done, the StringUtil.netmodule
file is created (see Figure 15-12).
Figure 15-12
Do the same for the MathUtil
class that you created earlier (see Figure 15-13):
csc /target:module /out:MathUtil.netmodule Class1.cs
Figure 15-13
Copy the two modules that you have just created — StringUtil.netmodule
and MathUtil.netmodule
— into a folder, say C:\Modules\. Now to combine these two modules into an assembly, type the following command:
csc /target:library /addmodule:StringUtil.netmodule /addmodule:MathUtil.netmodule /out:Utils.dll
This creates the Utils.dll
assembly (see Figure 15-14).
Figure 15-14
In the WindowsApp-Utils
project, remove the previous versions of the MathUtil.dll
assembly and add a reference to the Utils.dll
assembly that you just created (see Figure 15-15). You can do so via the Browse tab of the Add Reference dialog (navigate to the directory containing the modules and assembly, C:\Modules). Click OK.
Figure 15-15
In the code-behind of Form1
, modify the following code as shown:
namespace WindowsApp_Util {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
CallMathUtil();
CallStringUtil();
}
private void CallMathUtil() {
MathUtil.Utils util = new MathUtil.Utils();
MessageBox.Show(util.Fibonacci(7).ToString());
}
private void CallStringUtil() {
StringUtil.Utils util = new StringUtil.Utils();
MessageBox.Show(util.ValidateEmail(
"weimenglee@learn2develop.net").ToString());
}
}
}
The CallMathUtil()
function invokes the method defined in the MathUtil
module. The CallStringUtil()
function invokes the method defined in the StringUtil
module.
Set a break point in the Form1_Load
event handler, as shown in Figure 15-16, and press F5 to debug the application.
Figure 15-16
When the breakpoint is reached, view the Modules window (Debug→Windows→Modules), and note that the Utils.dll
assembly has not been loaded yet (see Figure 15-17).
Figure 15-17
Press F11 to step into the CallMathUtil()
function, and observe that the Utils.dll
assembly is now loaded, together with the MathUtil.netmodule
(see Figure 15-18).
Figure 15-18
Press F11 a few times to step out of the CallMathUtil()
function until you step into CallStringUtil()
. See that the StringUtil.netmodule
is now loaded (see Figure 15-19).
Figure 15-19
This example proves that modules in an assembly are loaded only as and when needed. Also, when deploying the application, the Util.dll
assembly and the two modules must be in tandem. If any of the modules is missing during runtime, you will encounter a runtime error, as shown in Figure 15-20.
Figure 15-20
Understanding Namespaces and Assemblies
As you know from Chapter 1, the various class libraries in the .NET Framework are organized using namespaces. So how do namespaces relate to assemblies? To understand the relationship between namespaces and assemblies, it's best to take a look at an example.
Create a new Class Library project in Visual Studio 2008, and name it ClassLibrary1
. In the default Class1.cs
, populate it with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Learn2develop.net {
public class Class1 {
public void DoSomething() {
}
}
}
Observe that the definition of Class1
is enclosed within the Learn2develop.net
namespace. The class also contains the DoSomething()
method.
Читать дальше