AssemblyBuilder.DefineVersionInfoResource 方法

定义

定义此程序集的非托管版本信息资源。

重载

DefineVersionInfoResource()

使用在程序集的 AssemblyName 对象中指定的信息和程序集的自定义特性,定义一个非托管的版本信息资源。

DefineVersionInfoResource(String, String, String, String, String)

对此具有给定规范的程序集,定义一个非托管的版本信息资源。

DefineVersionInfoResource()

使用在程序集的 AssemblyName 对象中指定的信息和程序集的自定义特性,定义一个非托管的版本信息资源。

public:
 void DefineVersionInfoResource();
public void DefineVersionInfoResource ();
member this.DefineVersionInfoResource : unit -> unit
Public Sub DefineVersionInfoResource ()

例外

之前已定义了一个非托管版本信息资源。

- 或 -

非托管版本信息太大,无法保留。

调用方没有所要求的权限。

示例

以下示例演示 了 的 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

注解

一个程序集只能与一个非托管资源相关联。 这意味着,调用 DefineVersionInfoResourceDefineUnmanagedResource 之前调用任一方法后,将引发 System.ArgumentException。 多个非托管资源需要与 Microsoft ResMerge 实用工具等工具合并, (公共语言运行时 SDK) 未提供。

空参数字符串将编写为单个空格。 参数字符串中的空字符将用空格替换。

该信息是从 AssemblyName 用于定义此动态程序集的 对象推断出来的。 此程序集的自定义属性将替代 对象中指定的 AssemblyName 信息。

注意

从 .NET Framework 2.0 Service Pack 1 开始,此成员不再需要ReflectionPermission标志ReflectionPermissionFlag.ReflectionEmit。 (请参阅反射发出中的安全问题.) 若要使用此功能,应用程序应面向.NET Framework 3.5 或更高版本。

适用于

DefineVersionInfoResource(String, String, String, String, String)

对此具有给定规范的程序集,定义一个非托管的版本信息资源。

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)

参数

product
String

此程序集随其一起分配的产品的名称。

productVersion
String

此程序集随其一起分配的产品的版本。

company
String

生成此程序集的公司的名称。

copyright
String

描述所有适用于此程序集的版权声明、商标和注册商标。 这应包括所有声明的完整文本、合法符号、版权日期、商标号等。 在英语中,此字符串应采用格式“Copyright Microsoft Corp.1990-2001”。

trademark
String

描述所有适用于此程序集的商标和注册商标。 这应包括所有声明的完整文本、合法符号、商标号等。 在英语中,此字符串应采用格式”Windows is a trademark of Microsoft Corporation”。

例外

之前已定义了一个非托管版本信息资源。

- 或 -

非托管版本信息太大,无法保留。

调用方没有所要求的权限。

示例

以下示例演示 了 的 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

注解

一个程序集只能与一个非托管资源相关联。 这意味着,调用 DefineVersionInfoResourceDefineUnmanagedResource 之前调用任一方法后,将引发 System.ArgumentException。 需要将多个非托管资源与 Microsoft 实用工具等 ResMerge 工具合并, (公共语言运行时 SDK) 未提供。

空参数字符串将编写为单个空格。 参数字符串中的空字符将用空格替换。

版本资源的结构包括标识文件的版本、语言和分发的数据。 安装程序使用文件安装库 (VER.DLL) 中的函数从文件检索版本信息资源,并从资源中提取版本信息块。

注意

从 .NET Framework 2.0 Service Pack 1 开始,此成员不再需要ReflectionPermission标志ReflectionPermissionFlag.ReflectionEmit。 (请参阅反射发出中的安全问题.) 若要使用此功能,应用程序应面向.NET Framework 3.5 或更高版本。

适用于