Jak cię złapią, to znaczy, że oszukiwałeś. Jak nie, to znaczy, że posłużyłeś się odpowiednią taktyką.
The originator in the manifest of MySharedAssembly.dll
By adding the strong name, you have signed this assembly (your exact values will be different). You now need to get the strong name from the DLL. To do this, navigate to the directory with the DLL and enter the following at a command prompt: sn -T MySharedAssembly.dll Note that sn is case-sensitive. Do not write sn -t. The response should be something like this: Public key token is 01fad8e0f0941a4d This value is an abbreviated version of the assembly's public key, called the public key token . Remove the DLLs from the test program's directory structure and run it again. It should fail again. Although you've given this assembly a strong name, you've not yet registered it in the GAC. 17.8.5.2 Step 2: Put the shared assembly in the GAC The next step is to drag the library into the GAC. To do so, open an Explorer window and navigate to the WinNT directory or its equivalent. When you double-click the Assembly subdirectory, Explorer will turn into a GAC viewer. You can drag and drop into the GAC viewer, or you can invoke this command-line utility: Gacutil -i mySharedAssembly In either case, be sure to check that your assembly was loaded into the GAC, and that the originator value shown in the GAC viewer matches the value you got back from sn: page 363 Programming C# Public key token is 01fad8e0f0941a4d as illustrated in Figure 17-9. Figure 17-9. The GAC Once this is done, you have a shared assembly that can be accessed by any client. Refresh the client by building it again and look at its manifest, as shown in Figure 17-10. Figure 17-10. The manifest There's MySharedAssembly, listed as an external assembly, and the public key now matches the value shown in the GAC. Very nice, time to try it. Close ILDasm and compile and run your code. It should work fine, even though there are no DLLs for this library in its immediate path. You have just created and used a shared assembly. Chapter 18. Attributes and Reflection Throughout this book, I have emphasized that a .NET application contains code, data, and metadata. Metadata is information about the data—that is, information about the types, code, assembly, and so forth—that is stored along with your program. This chapter will explore how some of that metadata is created and used. Attributes are a mechanism for adding metadata, such as compiler instructions and other data about your data, methods, and classes, to the program itself. Attributes are inserted into the metadata and are visible through ILDasm and other metadata-reading tools. Reflection is the process by which a program can read its own metadata. A program is said to reflect on itself, extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior. 18.1 Attributes An attribute is an object that represents data you want to associate with an element in your program. The element to which you attach an attribute is referred to as the target of that attribute. For example, the attribute: [NoIDispatch] page 364 Programming C# is associated with a class or an interface to indicate that the target class should derive from IUnknown rather than IDispatch, when exporting to COM. COM interface programming is discussed in detail in Chapter 22. In Chapter 17, you saw this attribute: [assembly: AssemblyKeyFile("c:\myStrongName.key")] This inserts metadata into the assembly to designate the program's StrongName. 18.2 Intrinsic Attributes Attributes come in two flavors: intrinsic and custom . Intrinsic attributes are supplied as part of the Common Language Runtime (CLR), and they are integrated into .NET. Custom attributes are attributes you create for your own purposes. Most programmers will use only intrinsic attributes, though custom attributes can be a powerful tool when combined with reflection, described later in this chapter. 18.2.1 Attribute Targets If you search through the CLR, you'll find a great many attributes. Some attributes are applied to an assembly, others to a class or interface, and some, such as [WebMethod], to class members. These are called the attribute targets. Possible attribute targets are detailed in Table 18-1. Table 18-1, Possible attribute targets Member Name Usage All Applied to any of the following elements: assembly, class, class member, delegate, enum, event, field, interface, method, module, parameter, property, return value, or struct Assembly Applied to the assembly itself Class Applied to instances of the class ClassMembers Applied to classes, structs, enums, constructors, methods, properties, fields, events, delegates, and interfaces Constructor Applied to a given constructor Delegate Applied to the delegated method Enum Applied to an enumeration Event Applied to an event Field Applied to a field Interface Applied to an interface Method Applied to a method Module Applied to a single module Parameter Applied to a parameter of a method Property Applied to a property (both get and set, if implemented) ReturnValue Applied to a return value Struct Applied to a struct 18.2.2 Applying Attributes You apply attributes to their targets by placing them in square brackets immediately before the target item. You can combine attributes, either by stacking one on top of another: page 365 Programming C# [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile(".\\keyFile.snk")] or by separating the attributes with commas: [assembly: AssemblyDelaySign(false), assembly: AssemblyKeyFile(".\\keyFile.snk")]
|
WÄ…tki
|