AssemblyBuilder.DefineVersionInfoResource Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Definiuje niezarządzany zasób informacji o wersji dla tego zestawu.
Przeciążenia
DefineVersionInfoResource() |
Definiuje niezarządzany zasób informacji o wersji przy użyciu informacji określonych w obiekcie AssemblyName zestawu i atrybutów niestandardowych zestawu. |
DefineVersionInfoResource(String, String, String, String, String) |
Definiuje niezarządzany zasób informacji o wersji dla tego zestawu z podanymi specyfikacjami. |
DefineVersionInfoResource()
Definiuje niezarządzany zasób informacji o wersji przy użyciu informacji określonych w obiekcie AssemblyName zestawu i atrybutów niestandardowych zestawu.
public:
void DefineVersionInfoResource();
public void DefineVersionInfoResource ();
member this.DefineVersionInfoResource : unit -> unit
Public Sub DefineVersionInfoResource ()
Wyjątki
Zasób informacji o wersji niezarządzanej został wcześniej zdefiniowany.
-lub-
Informacje o wersji niezarządzanej są zbyt duże, aby utrwały.
Obiekt wywołujący nie posiada wymaganych uprawnień.
Przykłady
W poniższym przykładzie pokazano użycie obiektu DefineVersionInfoResource
.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
/*
// Create the callee transient dynamic assembly.
static Type^ CreateAssembly( AppDomain^ myDomain )
{
AssemblyName^ myAssemblyName = gcnew AssemblyName;
myAssemblyName->Name = "MyEmittedAssembly";
AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Save );
// Set Company Attribute to the assembly.
Type^ companyAttribute = AssemblyCompanyAttribute::typeid;
array<Type^>^types1 = {String::typeid};
ConstructorInfo^ myConstructorInfo1 = companyAttribute->GetConstructor( types1 );
array<Object^>^obj1 = {"Microsoft Corporation"};
CustomAttributeBuilder^ attributeBuilder1 = gcnew CustomAttributeBuilder( myConstructorInfo1,obj1 );
myAssembly->SetCustomAttribute( attributeBuilder1 );
// Set Copyright Attribute to the assembly.
Type^ copyrightAttribute = AssemblyCopyrightAttribute::typeid;
array<Type^>^types2 = {String::typeid};
ConstructorInfo^ myConstructorInfo2 = copyrightAttribute->GetConstructor( types2 );
array<Object^>^obj2 = {"@Copyright Microsoft Corp. 1990-2001"};
CustomAttributeBuilder^ attributeBuilder2 = gcnew CustomAttributeBuilder( myConstructorInfo2,obj2 );
myAssembly->SetCustomAttribute( attributeBuilder2 );
ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule", "EmittedModule.mod" );
// Define a public class named S"HelloWorld" in the assembly.
TypeBuilder^ helloWorldClass = myModule->DefineType( "HelloWorld", TypeAttributes::Public );
// Define the Display method.
MethodBuilder^ myMethod = helloWorldClass->DefineMethod( "Display", MethodAttributes::Public, String::typeid, nullptr );
// Generate IL for GetGreeting.
ILGenerator^ methodIL = myMethod->GetILGenerator();
methodIL->Emit( OpCodes::Ldstr, "Display method get called." );
methodIL->Emit( OpCodes::Ret );
// Returns the type HelloWorld.
return (helloWorldClass->CreateType());
}
*/
int main()
{
AssemblyName^ assemName = gcnew AssemblyName();
assemName->Name = "EmittedAssembly";
// Create a dynamic assembly in the current application domain,
// specifying that the assembly is to be saved.
//
AssemblyBuilder^ myAssembly =
AppDomain::CurrentDomain->DefineDynamicAssembly(assemName,
AssemblyBuilderAccess::Save);
// To apply an attribute to a dynamic assembly, first get the
// attribute type. The AssemblyFileVersionAttribute sets the
// File Version field on the Version tab of the Windows file
// properties dialog.
//
Type^ attributeType = AssemblyFileVersionAttribute::typeid;
// To identify the constructor, use an array of types representing
// the constructor's parameter types. This ctor takes a string.
//
array<Type^>^ ctorParameters = { String::typeid };
// Get the constructor for the attribute.
//
ConstructorInfo^ ctor = attributeType->GetConstructor(ctorParameters);
// Pass the constructor and an array of arguments (in this case,
// an array containing a single string) to the
// CustomAttributeBuilder constructor.
//
array<Object^>^ ctorArgs = { "2.0.3033.0" };
CustomAttributeBuilder^ attribute =
gcnew CustomAttributeBuilder(ctor, ctorArgs);
// Finally, apply the attribute to the assembly.
//
myAssembly->SetCustomAttribute(attribute);
// The pattern described above is used to create and apply
// several more attributes. As it happens, all these attributes
// have a constructor that takes a string, so the same ctorArgs
// variable works for all of them.
// The AssemblyTitleAttribute sets the Description field on
// the General tab and the Version tab of the Windows file
// properties dialog.
//
attributeType = AssemblyTitleAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
ctorArgs = gcnew array<Object^> { "The Application Title" };
attribute = gcnew CustomAttributeBuilder(ctor, ctorArgs);
myAssembly->SetCustomAttribute(attribute);
// The AssemblyCopyrightAttribute sets the Copyright field on
// the Version tab.
//
attributeType = AssemblyCopyrightAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
ctorArgs = gcnew array<Object^> { "� My Example Company 1991-2005" };
attribute = gcnew CustomAttributeBuilder(ctor, ctorArgs);
myAssembly->SetCustomAttribute(attribute);
// The AssemblyDescriptionAttribute sets the Comment item.
//
attributeType = AssemblyDescriptionAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
attribute = gcnew CustomAttributeBuilder(ctor,
gcnew array<Object^> { "This is a comment." });
myAssembly->SetCustomAttribute(attribute);
// The AssemblyCompanyAttribute sets the Company item.
//
attributeType = AssemblyCompanyAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
attribute = gcnew CustomAttributeBuilder(ctor,
gcnew array<Object^> { "My Example Company" });
myAssembly->SetCustomAttribute(attribute);
// The AssemblyProductAttribute sets the Product Name item.
//
attributeType = AssemblyProductAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
attribute = gcnew CustomAttributeBuilder(ctor,
gcnew array<Object^> { "My Product Name" });
myAssembly->SetCustomAttribute(attribute);
// Define the assembly's only module. For a single-file assembly,
// the module name is the assembly name.
//
ModuleBuilder^ myModule =
myAssembly->DefineDynamicModule(assemName->Name,
assemName->Name + ".exe");
// No types or methods are created for this example.
// Define the unmanaged version information resource, which
// contains the attribute informaion applied earlier, and save
// the assembly. Use the Windows Explorer to examine the properties
// of the .exe file.
//
myAssembly->DefineVersionInfoResource();
myAssembly->Save(assemName->Name + ".exe");
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class Example
{
public static void Main()
{
AssemblyName assemName = new AssemblyName();
assemName.Name = "EmittedAssembly";
// Create a dynamic assembly in the current application domain,
// specifying that the assembly is to be saved.
//
AssemblyBuilder myAssembly =
AppDomain.CurrentDomain.DefineDynamicAssembly(assemName,
AssemblyBuilderAccess.Save);
// To apply an attribute to a dynamic assembly, first get the
// attribute type. The AssemblyFileVersionAttribute sets the
// File Version field on the Version tab of the Windows file
// properties dialog.
//
Type attributeType = typeof(AssemblyFileVersionAttribute);
// To identify the constructor, use an array of types representing
// the constructor's parameter types. This ctor takes a string.
//
Type[] ctorParameters = { typeof(string) };
// Get the constructor for the attribute.
//
ConstructorInfo ctor = attributeType.GetConstructor(ctorParameters);
// Pass the constructor and an array of arguments (in this case,
// an array containing a single string) to the
// CustomAttributeBuilder constructor.
//
object[] ctorArgs = { "2.0.3033.0" };
CustomAttributeBuilder attribute =
new CustomAttributeBuilder(ctor, ctorArgs);
// Finally, apply the attribute to the assembly.
//
myAssembly.SetCustomAttribute(attribute);
// The pattern described above is used to create and apply
// several more attributes. As it happens, all these attributes
// have a constructor that takes a string, so the same ctorArgs
// variable works for all of them.
// The AssemblyTitleAttribute sets the Description field on
// the General tab and the Version tab of the Windows file
// properties dialog.
//
attributeType = typeof(AssemblyTitleAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
ctorArgs = new object[] { "The Application Title" };
attribute = new CustomAttributeBuilder(ctor, ctorArgs);
myAssembly.SetCustomAttribute(attribute);
// The AssemblyCopyrightAttribute sets the Copyright field on
// the Version tab.
//
attributeType = typeof(AssemblyCopyrightAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
ctorArgs = new object[] { "© My Example Company 1991-2005" };
attribute = new CustomAttributeBuilder(ctor, ctorArgs);
myAssembly.SetCustomAttribute(attribute);
// The AssemblyDescriptionAttribute sets the Comment item.
//
attributeType = typeof(AssemblyDescriptionAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
attribute = new CustomAttributeBuilder(ctor,
new object[] { "This is a comment." });
myAssembly.SetCustomAttribute(attribute);
// The AssemblyCompanyAttribute sets the Company item.
//
attributeType = typeof(AssemblyCompanyAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
attribute = new CustomAttributeBuilder(ctor,
new object[] { "My Example Company" });
myAssembly.SetCustomAttribute(attribute);
// The AssemblyProductAttribute sets the Product Name item.
//
attributeType = typeof(AssemblyProductAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
attribute = new CustomAttributeBuilder(ctor,
new object[] { "My Product Name" });
myAssembly.SetCustomAttribute(attribute);
// Define the assembly's only module. For a single-file assembly,
// the module name is the assembly name.
//
ModuleBuilder myModule =
myAssembly.DefineDynamicModule(assemName.Name,
assemName.Name + ".exe");
// No types or methods are created for this example.
// Define the unmanaged version information resource, which
// contains the attribute informaion applied earlier, and save
// the assembly. Use the Windows Explorer to examine the properties
// of the .exe file.
//
myAssembly.DefineVersionInfoResource();
myAssembly.Save(assemName.Name + ".exe");
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Module Example
Sub Main()
Dim assemName As New AssemblyName()
assemName.Name = "EmittedAssembly"
' Create a dynamic assembly in the current application domain,
' specifying that the assembly is to be saved.
'
Dim myAssembly As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly(assemName, _
AssemblyBuilderAccess.Save)
' To apply an attribute to a dynamic assembly, first get the
' attribute type. The AssemblyFileVersionAttribute sets the
' File Version field on the Version tab of the Windows file
' properties dialog.
'
Dim attributeType As Type = GetType(AssemblyFileVersionAttribute)
' To identify the constructor, use an array of types representing
' the constructor's parameter types. This ctor takes a string.
'
Dim ctorParameters() As Type = { GetType(String) }
' Get the constructor for the attribute.
'
Dim ctor As ConstructorInfo = _
attributeType.GetConstructor(ctorParameters)
' Pass the constructor and an array of arguments (in this case,
' an array containing a single string) to the
' CustomAttributeBuilder constructor.
'
Dim ctorArgs() As Object = { "2.0.3033.0" }
Dim attribute As New CustomAttributeBuilder(ctor, ctorArgs)
' Finally, apply the attribute to the assembly.
'
myAssembly.SetCustomAttribute(attribute)
' The pattern described above is used to create and apply
' several more attributes. As it happens, all these attributes
' have a constructor that takes a string, so the same ctorArgs
' variable works for all of them.
' The AssemblyTitleAttribute sets the Description field on
' the General tab and the Version tab of the Windows file
' properties dialog.
'
attributeType = GetType(AssemblyTitleAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
ctorArgs = New Object() { "The Application Title" }
attribute = New CustomAttributeBuilder(ctor, ctorArgs)
myAssembly.SetCustomAttribute(attribute)
' The AssemblyCopyrightAttribute sets the Copyright field on
' the Version tab.
'
attributeType = GetType(AssemblyCopyrightAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
ctorArgs = New Object() { "© My Example Company 1991-2005" }
attribute = New CustomAttributeBuilder(ctor, ctorArgs)
myAssembly.SetCustomAttribute(attribute)
' The AssemblyDescriptionAttribute sets the Comment item.
'
attributeType = GetType(AssemblyDescriptionAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
attribute = New CustomAttributeBuilder(ctor, _
New Object() { "This is a comment." })
myAssembly.SetCustomAttribute(attribute)
' The AssemblyCompanyAttribute sets the Company item.
'
attributeType = GetType(AssemblyCompanyAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
attribute = New CustomAttributeBuilder(ctor, _
New Object() { "My Example Company" })
myAssembly.SetCustomAttribute(attribute)
' The AssemblyProductAttribute sets the Product Name item.
'
attributeType = GetType(AssemblyProductAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
attribute = New CustomAttributeBuilder(ctor, _
New Object() { "My Product Name" })
myAssembly.SetCustomAttribute(attribute)
' Define the assembly's only module. For a single-file assembly,
' the module name is the assembly name.
'
Dim myModule As ModuleBuilder = _
myAssembly.DefineDynamicModule(assemName.Name, _
assemName.Name & ".exe")
' No types or methods are created for this example.
' Define the unmanaged version information resource, which
' contains the attribute informaion applied earlier, and save
' the assembly. Use the Windows Explorer to examine the properties
' of the .exe file.
'
myAssembly.DefineVersionInfoResource()
myAssembly.Save(assemName.Name & ".exe")
End Sub
End Module
Uwagi
Zestaw może być skojarzony tylko z jednym niezarządzanym zasobem. Oznacza to, że wywołanie metody lub DefineUnmanagedResource po wywołaniu DefineVersionInfoResource jednej z metod wcześniej spowoduje zgłoszenie wyjątku System.ArgumentException. Wiele niezarządzanych zasobów należy scalić z narzędziem, takim jak narzędzie Microsoft ResMerge (nie jest dostarczane z zestawem SDK środowiska uruchomieniowego języka wspólnego).
Puste ciągi argumentów są zapisywane jako pojedyncza spacja. Spacje są zastępowane znakami null w ciągach argumentów.
Informacje są wnioskowane z obiektu używanego AssemblyName
do definiowania tego zestawu dynamicznego. Atrybuty niestandardowe tego zestawu zastępują informacje określone w AssemblyName
obiekcie .
Uwaga
Począwszy od .NET Framework 2.0 z dodatkiem Service Pack 1, ten element członkowski nie wymaga ReflectionPermission już flagi ReflectionPermissionFlag.ReflectionEmit . (Zobacz Problemy z zabezpieczeniami w emisji odbicia). Aby korzystać z tej funkcji, aplikacja powinna być docelowa dla .NET Framework 3.5 lub nowszej.
Dotyczy
DefineVersionInfoResource(String, String, String, String, String)
Definiuje niezarządzany zasób informacji o wersji dla tego zestawu z podanymi specyfikacjami.
public:
void DefineVersionInfoResource(System::String ^ product, System::String ^ productVersion, System::String ^ company, System::String ^ copyright, System::String ^ trademark);
public void DefineVersionInfoResource (string product, string productVersion, string company, string copyright, string trademark);
member this.DefineVersionInfoResource : string * string * string * string * string -> unit
Public Sub DefineVersionInfoResource (product As String, productVersion As String, company As String, copyright As String, trademark As String)
Parametry
- product
- String
Nazwa produktu, z którym jest dystrybuowany ten zestaw.
- productVersion
- String
Wersja produktu, z którym jest dystrybuowany ten zestaw.
- company
- String
Nazwa firmy, która wyprodukowała ten zestaw.
- copyright
- String
Opisuje wszystkie powiadomienia o prawach autorskich, znaki towarowe i zarejestrowane znaki towarowe, które mają zastosowanie do tego zestawu. Powinno to zawierać pełny tekst wszystkich powiadomień, symboli prawnych, dat praw autorskich, numerów znaków towarowych itd. W języku angielskim ten ciąg powinien mieć format "Copyright Microsoft Corp. 1990-2001".
- trademark
- String
Opisuje wszystkie znaki towarowe i zarejestrowane znaki towarowe, które mają zastosowanie do tego zestawu. Powinno to obejmować pełny tekst wszystkich powiadomień, symboli prawnych, numerów znaków towarowych itd. W języku angielskim ten ciąg powinien mieć format "Windows jest znakiem towarowym firmy Microsoft Corporation".
Wyjątki
Zasób informacji o wersji niezarządzanej został wcześniej zdefiniowany.
-lub-
Informacje o wersji niezarządzanej są zbyt duże, aby utrwały.
Obiekt wywołujący nie posiada wymaganych uprawnień.
Przykłady
W poniższym przykładzie pokazano użycie obiektu DefineVersionInfoResource
.
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
/*
// Create the callee transient dynamic assembly.
static Type^ CreateAssembly( AppDomain^ myDomain )
{
AssemblyName^ myAssemblyName = gcnew AssemblyName;
myAssemblyName->Name = "MyEmittedAssembly";
AssemblyBuilder^ myAssembly = myDomain->DefineDynamicAssembly( myAssemblyName, AssemblyBuilderAccess::Save );
// Set Company Attribute to the assembly.
Type^ companyAttribute = AssemblyCompanyAttribute::typeid;
array<Type^>^types1 = {String::typeid};
ConstructorInfo^ myConstructorInfo1 = companyAttribute->GetConstructor( types1 );
array<Object^>^obj1 = {"Microsoft Corporation"};
CustomAttributeBuilder^ attributeBuilder1 = gcnew CustomAttributeBuilder( myConstructorInfo1,obj1 );
myAssembly->SetCustomAttribute( attributeBuilder1 );
// Set Copyright Attribute to the assembly.
Type^ copyrightAttribute = AssemblyCopyrightAttribute::typeid;
array<Type^>^types2 = {String::typeid};
ConstructorInfo^ myConstructorInfo2 = copyrightAttribute->GetConstructor( types2 );
array<Object^>^obj2 = {"@Copyright Microsoft Corp. 1990-2001"};
CustomAttributeBuilder^ attributeBuilder2 = gcnew CustomAttributeBuilder( myConstructorInfo2,obj2 );
myAssembly->SetCustomAttribute( attributeBuilder2 );
ModuleBuilder^ myModule = myAssembly->DefineDynamicModule( "EmittedModule", "EmittedModule.mod" );
// Define a public class named S"HelloWorld" in the assembly.
TypeBuilder^ helloWorldClass = myModule->DefineType( "HelloWorld", TypeAttributes::Public );
// Define the Display method.
MethodBuilder^ myMethod = helloWorldClass->DefineMethod( "Display", MethodAttributes::Public, String::typeid, nullptr );
// Generate IL for GetGreeting.
ILGenerator^ methodIL = myMethod->GetILGenerator();
methodIL->Emit( OpCodes::Ldstr, "Display method get called." );
methodIL->Emit( OpCodes::Ret );
// Returns the type HelloWorld.
return (helloWorldClass->CreateType());
}
*/
int main()
{
AssemblyName^ assemName = gcnew AssemblyName();
assemName->Name = "EmittedAssembly";
// Create a dynamic assembly in the current application domain,
// specifying that the assembly is to be saved.
//
AssemblyBuilder^ myAssembly =
AppDomain::CurrentDomain->DefineDynamicAssembly(assemName,
AssemblyBuilderAccess::Save);
// To apply an attribute to a dynamic assembly, first get the
// attribute type. The AssemblyFileVersionAttribute sets the
// File Version field on the Version tab of the Windows file
// properties dialog.
//
Type^ attributeType = AssemblyFileVersionAttribute::typeid;
// To identify the constructor, use an array of types representing
// the constructor's parameter types. This ctor takes a string.
//
array<Type^>^ ctorParameters = { String::typeid };
// Get the constructor for the attribute.
//
ConstructorInfo^ ctor = attributeType->GetConstructor(ctorParameters);
// Pass the constructor and an array of arguments (in this case,
// an array containing a single string) to the
// CustomAttributeBuilder constructor.
//
array<Object^>^ ctorArgs = { "2.0.3033.0" };
CustomAttributeBuilder^ attribute =
gcnew CustomAttributeBuilder(ctor, ctorArgs);
// Finally, apply the attribute to the assembly.
//
myAssembly->SetCustomAttribute(attribute);
// The pattern described above is used to create and apply
// several more attributes. As it happens, all these attributes
// have a constructor that takes a string, so the same ctorArgs
// variable works for all of them.
// The AssemblyTitleAttribute sets the Description field on
// the General tab and the Version tab of the Windows file
// properties dialog.
//
attributeType = AssemblyTitleAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
ctorArgs = gcnew array<Object^> { "The Application Title" };
attribute = gcnew CustomAttributeBuilder(ctor, ctorArgs);
myAssembly->SetCustomAttribute(attribute);
// The AssemblyCopyrightAttribute sets the Copyright field on
// the Version tab.
//
attributeType = AssemblyCopyrightAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
ctorArgs = gcnew array<Object^> { "� My Example Company 1991-2005" };
attribute = gcnew CustomAttributeBuilder(ctor, ctorArgs);
myAssembly->SetCustomAttribute(attribute);
// The AssemblyDescriptionAttribute sets the Comment item.
//
attributeType = AssemblyDescriptionAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
attribute = gcnew CustomAttributeBuilder(ctor,
gcnew array<Object^> { "This is a comment." });
myAssembly->SetCustomAttribute(attribute);
// The AssemblyCompanyAttribute sets the Company item.
//
attributeType = AssemblyCompanyAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
attribute = gcnew CustomAttributeBuilder(ctor,
gcnew array<Object^> { "My Example Company" });
myAssembly->SetCustomAttribute(attribute);
// The AssemblyProductAttribute sets the Product Name item.
//
attributeType = AssemblyProductAttribute::typeid;
ctor = attributeType->GetConstructor(ctorParameters);
attribute = gcnew CustomAttributeBuilder(ctor,
gcnew array<Object^> { "My Product Name" });
myAssembly->SetCustomAttribute(attribute);
// Define the assembly's only module. For a single-file assembly,
// the module name is the assembly name.
//
ModuleBuilder^ myModule =
myAssembly->DefineDynamicModule(assemName->Name,
assemName->Name + ".exe");
// No types or methods are created for this example.
// Define the unmanaged version information resource, which
// contains the attribute informaion applied earlier, and save
// the assembly. Use the Windows Explorer to examine the properties
// of the .exe file.
//
myAssembly->DefineVersionInfoResource();
myAssembly->Save(assemName->Name + ".exe");
}
using System;
using System.Reflection;
using System.Reflection.Emit;
class Example
{
public static void Main()
{
AssemblyName assemName = new AssemblyName();
assemName.Name = "EmittedAssembly";
// Create a dynamic assembly in the current application domain,
// specifying that the assembly is to be saved.
//
AssemblyBuilder myAssembly =
AppDomain.CurrentDomain.DefineDynamicAssembly(assemName,
AssemblyBuilderAccess.Save);
// To apply an attribute to a dynamic assembly, first get the
// attribute type. The AssemblyFileVersionAttribute sets the
// File Version field on the Version tab of the Windows file
// properties dialog.
//
Type attributeType = typeof(AssemblyFileVersionAttribute);
// To identify the constructor, use an array of types representing
// the constructor's parameter types. This ctor takes a string.
//
Type[] ctorParameters = { typeof(string) };
// Get the constructor for the attribute.
//
ConstructorInfo ctor = attributeType.GetConstructor(ctorParameters);
// Pass the constructor and an array of arguments (in this case,
// an array containing a single string) to the
// CustomAttributeBuilder constructor.
//
object[] ctorArgs = { "2.0.3033.0" };
CustomAttributeBuilder attribute =
new CustomAttributeBuilder(ctor, ctorArgs);
// Finally, apply the attribute to the assembly.
//
myAssembly.SetCustomAttribute(attribute);
// The pattern described above is used to create and apply
// several more attributes. As it happens, all these attributes
// have a constructor that takes a string, so the same ctorArgs
// variable works for all of them.
// The AssemblyTitleAttribute sets the Description field on
// the General tab and the Version tab of the Windows file
// properties dialog.
//
attributeType = typeof(AssemblyTitleAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
ctorArgs = new object[] { "The Application Title" };
attribute = new CustomAttributeBuilder(ctor, ctorArgs);
myAssembly.SetCustomAttribute(attribute);
// The AssemblyCopyrightAttribute sets the Copyright field on
// the Version tab.
//
attributeType = typeof(AssemblyCopyrightAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
ctorArgs = new object[] { "© My Example Company 1991-2005" };
attribute = new CustomAttributeBuilder(ctor, ctorArgs);
myAssembly.SetCustomAttribute(attribute);
// The AssemblyDescriptionAttribute sets the Comment item.
//
attributeType = typeof(AssemblyDescriptionAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
attribute = new CustomAttributeBuilder(ctor,
new object[] { "This is a comment." });
myAssembly.SetCustomAttribute(attribute);
// The AssemblyCompanyAttribute sets the Company item.
//
attributeType = typeof(AssemblyCompanyAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
attribute = new CustomAttributeBuilder(ctor,
new object[] { "My Example Company" });
myAssembly.SetCustomAttribute(attribute);
// The AssemblyProductAttribute sets the Product Name item.
//
attributeType = typeof(AssemblyProductAttribute);
ctor = attributeType.GetConstructor(ctorParameters);
attribute = new CustomAttributeBuilder(ctor,
new object[] { "My Product Name" });
myAssembly.SetCustomAttribute(attribute);
// Define the assembly's only module. For a single-file assembly,
// the module name is the assembly name.
//
ModuleBuilder myModule =
myAssembly.DefineDynamicModule(assemName.Name,
assemName.Name + ".exe");
// No types or methods are created for this example.
// Define the unmanaged version information resource, which
// contains the attribute informaion applied earlier, and save
// the assembly. Use the Windows Explorer to examine the properties
// of the .exe file.
//
myAssembly.DefineVersionInfoResource();
myAssembly.Save(assemName.Name + ".exe");
}
}
Imports System.Reflection
Imports System.Reflection.Emit
Module Example
Sub Main()
Dim assemName As New AssemblyName()
assemName.Name = "EmittedAssembly"
' Create a dynamic assembly in the current application domain,
' specifying that the assembly is to be saved.
'
Dim myAssembly As AssemblyBuilder = _
AppDomain.CurrentDomain.DefineDynamicAssembly(assemName, _
AssemblyBuilderAccess.Save)
' To apply an attribute to a dynamic assembly, first get the
' attribute type. The AssemblyFileVersionAttribute sets the
' File Version field on the Version tab of the Windows file
' properties dialog.
'
Dim attributeType As Type = GetType(AssemblyFileVersionAttribute)
' To identify the constructor, use an array of types representing
' the constructor's parameter types. This ctor takes a string.
'
Dim ctorParameters() As Type = { GetType(String) }
' Get the constructor for the attribute.
'
Dim ctor As ConstructorInfo = _
attributeType.GetConstructor(ctorParameters)
' Pass the constructor and an array of arguments (in this case,
' an array containing a single string) to the
' CustomAttributeBuilder constructor.
'
Dim ctorArgs() As Object = { "2.0.3033.0" }
Dim attribute As New CustomAttributeBuilder(ctor, ctorArgs)
' Finally, apply the attribute to the assembly.
'
myAssembly.SetCustomAttribute(attribute)
' The pattern described above is used to create and apply
' several more attributes. As it happens, all these attributes
' have a constructor that takes a string, so the same ctorArgs
' variable works for all of them.
' The AssemblyTitleAttribute sets the Description field on
' the General tab and the Version tab of the Windows file
' properties dialog.
'
attributeType = GetType(AssemblyTitleAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
ctorArgs = New Object() { "The Application Title" }
attribute = New CustomAttributeBuilder(ctor, ctorArgs)
myAssembly.SetCustomAttribute(attribute)
' The AssemblyCopyrightAttribute sets the Copyright field on
' the Version tab.
'
attributeType = GetType(AssemblyCopyrightAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
ctorArgs = New Object() { "© My Example Company 1991-2005" }
attribute = New CustomAttributeBuilder(ctor, ctorArgs)
myAssembly.SetCustomAttribute(attribute)
' The AssemblyDescriptionAttribute sets the Comment item.
'
attributeType = GetType(AssemblyDescriptionAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
attribute = New CustomAttributeBuilder(ctor, _
New Object() { "This is a comment." })
myAssembly.SetCustomAttribute(attribute)
' The AssemblyCompanyAttribute sets the Company item.
'
attributeType = GetType(AssemblyCompanyAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
attribute = New CustomAttributeBuilder(ctor, _
New Object() { "My Example Company" })
myAssembly.SetCustomAttribute(attribute)
' The AssemblyProductAttribute sets the Product Name item.
'
attributeType = GetType(AssemblyProductAttribute)
ctor = attributeType.GetConstructor(ctorParameters)
attribute = New CustomAttributeBuilder(ctor, _
New Object() { "My Product Name" })
myAssembly.SetCustomAttribute(attribute)
' Define the assembly's only module. For a single-file assembly,
' the module name is the assembly name.
'
Dim myModule As ModuleBuilder = _
myAssembly.DefineDynamicModule(assemName.Name, _
assemName.Name & ".exe")
' No types or methods are created for this example.
' Define the unmanaged version information resource, which
' contains the attribute informaion applied earlier, and save
' the assembly. Use the Windows Explorer to examine the properties
' of the .exe file.
'
myAssembly.DefineVersionInfoResource()
myAssembly.Save(assemName.Name & ".exe")
End Sub
End Module
Uwagi
Zestaw może być skojarzony tylko z jednym niezarządzanym zasobem. Oznacza to, że wywołanie metody lub DefineUnmanagedResource po wywołaniu DefineVersionInfoResource jednej z metod wcześniej spowoduje zgłoszenie wyjątku System.ArgumentException. Wiele niezarządzanych zasobów należy scalić z narzędziem, takim jak narzędzie firmy Microsoft ResMerge
(nie jest dostarczane z zestawem SDK środowiska uruchomieniowego języka wspólnego).
Puste ciągi argumentów są zapisywane jako pojedyncza spacja. Spacje są zastępowane znakami null w ciągach argumentów.
Struktura zasobu wersji obejmuje dane identyfikujące wersję, język i dystrybucję pliku. Programy instalacyjne używają funkcji w bibliotece instalacyjnej plików (VER.DLL), aby pobrać zasób informacji o wersji z pliku i wyodrębnić bloki informacji o wersji z zasobu.
Uwaga
Począwszy od .NET Framework 2.0 z dodatkiem Service Pack 1, ten element członkowski nie wymaga ReflectionPermission już flagi ReflectionPermissionFlag.ReflectionEmit . (Zobacz Problemy z zabezpieczeniami w emisji odbicia). Aby korzystać z tej funkcji, aplikacja powinna być docelowa dla .NET Framework 3.5 lub nowszej.