Current location - Quotes Website - Personality signature - . Net: What is strong assembly and what is weak assembly?
. Net: What is strong assembly and what is weak assembly?
You can assign an encrypted signature called a strong name to an assembly, which provides a unique name for the assembly and prevents others from borrowing your assembly name (name spoofing). If you deploy an assembly that will be shared by multiple applications on the same computer, the assembly must have a strong name. Even if you only use an assembly in your application, using a strong name can ensure that the correct version of the assembly is loaded.

The first step in generating an assembly with a strong name is to obtain an encryption key pair. Yes. The. NET framework SDK includes a Strong Name tool (Sn.exe) that can be used to generate key pairs. The key pair generated by the strong name tool can be stored in a file or in the cryptographic service provider (CSP) of the local computer. The following command uses the strong name tool to generate a new key pair and store it in a file named TestKey.snk:

sn -k Testkey.snk

After obtaining the key pair, you need to add the correct custom attributes to the source file so that the compiler can emit assemblies with strong names. According to whether the key pair used for signature is contained in the file or in the key container of CSP, select the attribute correctly. For keys stored in files, use the system. For keys stored in CSP, use system. Interoperability between reflection. assemblykeynameattribute. net components and com components

Yes. NET technology is the next generation platform technology introduced by Microsoft. Since the official release of Beta2 version of. NET technology architecture, this technology has gradually matured and stabilized. According to the market share of Microsoft platform system, we can easily imagine. NET technology will be on the mainstream technology platform in the next year or two, and the most important premise for a new technology platform to develop rapidly is not to completely abandon the previous technology, which refers to COM/COM+ technology. NET technology.

Generally speaking, in the field of IT technology and hardware industry, the speed of technology upgrading is amazing. It is customary that all new technologies will follow the principle of backward compatibility, but. NET technology has not only achieved this, but also realized their respective calls, which is very valuable. In other words, we can not only. NET component, but we can also call. NET COMponents are usually located in com components. The benefits of doing so are obvious. On the one hand, existing technical resources can be retained; On the other hand, we can take advantage of all kinds of new technologies. Existing resources in. NET.

Before we begin, let's have a look. NET COMponents and com components that we often use at present.

NET components can be divided into two categories: * * sharing. NET components and private. Net composition.

Yes. The. NET components enjoyed by * * need to be guaranteed to be unique through standard public key cryptography technology, which is similar to the globally unique ID number GUID of COM. However, we should try to avoid using it. NET components * * * as much as possible, because under the Windows system, it is possible to fall into the "DLL hell" that has plagued developers for a long time.

Personal. NET component is. NET component mode, which we will use frequently. So, what we need to do at the time of release is. NET components simply copy them. We seem to have returned to the ancient DOS era, and now we don't have to care about the complicated system registry, and we don't have to worry about the version of DLL being overwritten.

COM component is a code reuse technology framework promoted by Microsoft for many years, which has been greatly developed and applied in recent years, but its shortcomings are becoming increasingly obvious. We have to face version control between many COM components and terrible DLL hell, as well as registry, GUID and so on. When we install a software, we also bring a large number of COM components with unknown versions into our operating system.

However, the technical advantages of COM components are also obvious, and code reuse under Windows platform has been realized to a great extent, so we put forward such a topic, how to protect and utilize a large number of existing software and products using COM technology with the maturity of. NET technology?

Let's discuss how to realize the call and operation between meetings. NET COMponent and com component.

First, let's look at how to call the written component. Existing COM components in. NET:

Here we write a simplest component in C#, and only return a string. The detailed code is as follows:

Up there. NET component, we implement a class member: say. His purpose is only to return a string.

Switch to the MS-DOS command line and run:

c:\ & gt; CSC/t:library/out class library 1。 DLLClassLibrary 1.cs

The compiler parameter /t:library above tells the C# compiler that we are now building a library application. In this way, we get a. NET component named ClassLibrary 1.DLL.

But if we want to use this component in the existing COM component, we have the following steps:

Switch to the MS-DOS command line and run:

c:\ & gt; Regasm out class library 1. DLL/regfile:class library 1 . reg

The command line above is to register us. NET components and generate backup registry files. You should remember that under our previous win9x/NT/2000, the command used to register COM components was:

regsvr32 c:\test.dll

Down there. NET, registering. NET components need the regasm command mentioned above. It is worth noting that this method is only called by COM components, and. NET itself can call each other's components without any registration!

It's not over yet. Next we need:

Switch to the MS-DOS command line and run:

c:\ & gt; tlbExp class library 1 . dll/out:class library 1 . TLB

The command line above indicates. NET components so that we can bind COM components in advance.

Ok, then we can use it easily. NET component, based on the current COM technology, is written in C# with our own code. Here we use VB6.0 to write a small test code. Before we begin, we need to use the "reference" option in the menu in the integrated environment of VB, and select the type library file ClassLibrary 1.tlb that we just generated.

The code of VB test is as follows:

Private Sub-Form _Load ()

Dim test as a new class library 1.hello

Dim string as string

str = test.say("dddd ")

MsgBox string

End joint

Next, let's look at how to use existing COM components. Net composition.

Because. NET, it is easier to use COM components. At the beginning of design, NET considers how to make convenient use of the existing technical resources, which is also the consistent style of Microsoft, as well. The net can also be seen. . NET provides a large number of class libraries to facilitate interoperability with COM, and one of the most important namespaces is: System.Runtime.InteropServices. Through the name of this namespace, we can literally see "interoperability services". Namespace system. InteropServices provides a series of classes that operate on COM objects.

In the following example, let's call the Win32 function MessageBoxA that comes with the system. Located in user32.dll, this function is a com component of the system. The code we call is as follows:

Use the system;

Use the system. Runtime . InteropServices

Classification test

{

[DllImport("user32.dll")]

public static extern int messagebox a(in thnd,string strMsg,string strCaption,intn type);

Public static void Main ()

{

int myMsg

MyMsg=MessageBoxA(0, "Hello!" , "Test", 0);

}

}

Switch to the MS-DOS command line and run:

c:\ & gt; Csc test. cs

After compiling our C# application, you can see the dialog box by running it directly!

It should be noted that before calling COM components, we need to refer to the namespace: System. Object in the control. NET program. Because we need to use a method provided by this namespace: DllImport.

Anyway, it is very convenient to use COM components in. Net. The above is the DLL component of the system we called. Similarly, we can also use this method to call our own COM components written in VB/VC.

We've finished discussing how to. NET and COM, we have clearly seen them. NET technology. The sooner we master it. NET technology, the easier it will be to take the lead in the future. Let's work together to master the powerful. NET platform technology.

(The above program passed the test under Windows2000 server+VS.NET Beta2).