AssemblyBuilder.SetCustomAttribute メソッド
このアセンブリのカスタム属性を設定します。
オーバーロードの一覧
カスタム属性ビルダを使用して、このアセンブリのカスタム属性を設定します。
[Visual Basic] Overloads Public Sub SetCustomAttribute(CustomAttributeBuilder)
[C#] public void SetCustomAttribute(CustomAttributeBuilder);
[C++] public: void SetCustomAttribute(CustomAttributeBuilder*);
[JScript] public function SetCustomAttribute(CustomAttributeBuilder);
指定したカスタム属性 BLOB を使用して、このアセンブリのカスタム属性を設定します。
[Visual Basic] Overloads Public Sub SetCustomAttribute(ConstructorInfo, Byte())
[C#] public void SetCustomAttribute(ConstructorInfo, byte[]);
[C++] public: void SetCustomAttribute(ConstructorInfo*, unsigned char __gc[]);
[JScript] public function SetCustomAttribute(ConstructorInfo, Byte[]);
使用例
[Visual Basic, C#, C++] 次のコード例は、 SetCustomAttribute を使用して、動的に生成するアセンブリにカスタム属性を結び付ける方法を示しています。
[Visual Basic, C#, C++] メモ ここでは、SetCustomAttribute のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。
Imports System
Imports System.Threading
Imports System.Reflection
Imports System.Reflection.Emit
<AttributeUsage(AttributeTargets.All, AllowMultiple := False)> _
Public Class MyAttribute
Inherits Attribute
Public s As Boolean
Public Sub New(s As Boolean)
Me.s = s
End Sub 'New
End Class 'MyAttribute
Class MyApplication
Public Shared Sub Main()
Dim customAttribute As Type = CreateCallee(Thread.GetDomain())
Dim attributes As Object() = customAttribute.Assembly.GetCustomAttributes(True)
Console.WriteLine("MyAttribute custom attribute contains : ")
Dim index As Integer
For index = 0 To attributes.Length - 1
If TypeOf attributes(index) Is MyAttribute Then
Console.WriteLine("s : " + CType(attributes(index), MyAttribute).s.ToString())
Exit For
End If
Next index
End Sub 'Main
Private Shared Function CreateCallee(domain As AppDomain) As Type
Dim myAssemblyName As New AssemblyName()
myAssemblyName.Name = "EmittedAssembly"
Dim myAssembly As AssemblyBuilder = domain.DefineDynamicAssembly(myAssemblyName, _
AssemblyBuilderAccess.Run)
Dim myType As Type = GetType(MyAttribute)
Dim infoConstructor As ConstructorInfo = myType.GetConstructor(New Type() {GetType(Boolean)})
myAssembly.SetCustomAttribute(infoConstructor, New Byte() {01, 00, 01})
Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule("EmittedModule")
' Define a public class named "HelloWorld" in the assembly.
Dim helloWorldClass As TypeBuilder = myModule.DefineType("HelloWorld", TypeAttributes.Public)
Return helloWorldClass.CreateType()
End Function 'CreateCallee
End Class 'MyApplication
[C#]
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
public bool s;
public MyAttribute(bool s)
{
this.s = s;
}
}
class MyApplication
{
public static void Main()
{
Type customAttribute = CreateCallee(Thread.GetDomain());
object[] attributes = customAttribute.Assembly.GetCustomAttributes(true);
Console.WriteLine("MyAttribute custom attribute contains : ");
for(int index=0; index < attributes.Length; index++)
{
if(attributes[index] is MyAttribute)
{
Console.WriteLine("s : " + ((MyAttribute)attributes[index]).s);
break;
}
}
}
private static Type CreateCallee(AppDomain domain)
{
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "EmittedAssembly";
AssemblyBuilder myAssembly = domain.DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess.Run);
Type myType = typeof(MyAttribute);
ConstructorInfo infoConstructor = myType.GetConstructor(new Type[]{typeof(bool)});
myAssembly.SetCustomAttribute(infoConstructor, new byte[]{01,00,01});
ModuleBuilder myModule = myAssembly.DefineDynamicModule("EmittedModule");
// Define a public class named "HelloWorld" in the assembly.
TypeBuilder helloWorldClass = myModule.DefineType("HelloWorld", TypeAttributes.Public);
return(helloWorldClass.CreateType());
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
[AttributeUsage(AttributeTargets::All, AllowMultiple = false)]
public __gc class MyAttribute : public Attribute
{
public:
bool s;
MyAttribute(bool s)
{
this->s = s;
}
};
Type* CreateCallee(AppDomain* domain)
{
AssemblyName* myAssemblyName = new AssemblyName();
myAssemblyName->Name = S"EmittedAssembly";
AssemblyBuilder* myAssembly = domain->DefineDynamicAssembly(myAssemblyName,
AssemblyBuilderAccess::Run);
Type* myType = __typeof(MyAttribute);
Type* temp0 [] = {__typeof(bool)};
ConstructorInfo* infoConstructor = myType->GetConstructor(temp0);
Byte temp1 [] = {01,00,01};
myAssembly->SetCustomAttribute(infoConstructor, temp1);
ModuleBuilder* myModule = myAssembly->DefineDynamicModule(S"EmittedModule");
// Define a public class named "HelloWorld" in the assembly.
TypeBuilder* helloWorldClass = myModule->DefineType(S"HelloWorld", TypeAttributes::Public);
return(helloWorldClass->CreateType());
}
int main()
{
Type* customAttribute = CreateCallee(Thread::GetDomain());
Object* attributes[] = customAttribute->Assembly->GetCustomAttributes(true);
Console::WriteLine(S"MyAttribute custom attribute contains : ");
for(int index=0; index < attributes->Length; index++)
{
if(dynamic_cast<MyAttribute*>(attributes[index]))
{
Console::WriteLine(S"s : {0}", __box((dynamic_cast<MyAttribute*>(attributes[index]))->s));
break;
}
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
参照
AssemblyBuilder クラス | AssemblyBuilder メンバ | System.Reflection.Emit 名前空間