カスタム属性ビルダを使用して、このアセンブリのカスタム属性を設定します。
Overloads Public Sub SetCustomAttribute( _
ByVal customBuilder As CustomAttributeBuilder _)
[C#]
public void SetCustomAttribute(CustomAttributeBuildercustomBuilder);
[C++]
public: void SetCustomAttribute(CustomAttributeBuilder* customBuilder);
[JScript]
public function SetCustomAttribute(
customBuilder : CustomAttributeBuilder);
パラメータ
- customBuilder
カスタム属性を定義するためのヘルパ クラスのインスタンス。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | con が null 参照 (Visual Basic では Nothing) です。 |
SecurityException | 呼び出し元に、必要なアクセス許可がありません。 |
使用例
[Visual Basic, C#, C++] 次のコード例は、 CustomAttributeBuilder を使用して、 AssemblyBuilder 内の SetCustomAttribute を使用する方法を示しています。
<AttributeUsage(AttributeTargets.All, AllowMultiple := False)> _
Public Class MyAttribute
Inherits Attribute
Public s As String
Public x As Integer
Public Sub New(s As String, x As Integer)
Me.s = s
Me.x = x
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)
Console.WriteLine("x : " + CType(attributes(index), MyAttribute).x.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(1) {GetType(String), GetType(Integer)})
Dim attributeBuilder As New CustomAttributeBuilder(infoConstructor, New Object(1) {"Hello", 2})
myAssembly.SetCustomAttribute(attributeBuilder)
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#]
[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class MyAttribute : Attribute
{
public String s;
public int x;
public MyAttribute(String s, int x)
{
this.s = s;
this.x = x;
}
}
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);
Console.WriteLine("x : " + ((MyAttribute)attributes[index]).x);
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[2]{typeof(String), typeof(int)});
CustomAttributeBuilder attributeBuilder =
new CustomAttributeBuilder(infoConstructor, new object[2]{"Hello", 2});
myAssembly.SetCustomAttribute(attributeBuilder);
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++]
[AttributeUsage(AttributeTargets::All, AllowMultiple = false)]
public __gc class MyAttribute : public Attribute
{
public:
String* s;
int x;
MyAttribute(String* s, int x)
{
this->s = s;
this->x = x;
}
};
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(String), __typeof(int)};
ConstructorInfo* infoConstructor = myType->GetConstructor(temp0);
Object* temp1 [] = {S"Hello", __box(2)};
CustomAttributeBuilder* attributeBuilder =
new CustomAttributeBuilder(infoConstructor, temp1);
myAssembly->SetCustomAttribute(attributeBuilder);
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}", (dynamic_cast<MyAttribute*>(attributes[index]))->s);
Console::WriteLine(S"x : {0}", __box((dynamic_cast<MyAttribute*>(attributes[index]))->x));
break;
}
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
.NET Framework セキュリティ:
- ReflectionPermission SecurityAction.Demand、ReflectionEmit=true
- ReflectionPermission (Type.InvokeMember などの機構を通じて遅延バインディングで呼び出すときに必要なアクセス許可) ReflectionPermissionFlag.MemberAccess (関連する列挙体)
参照
AssemblyBuilder クラス | AssemblyBuilder メンバ | System.Reflection.Emit 名前空間 | AssemblyBuilder.SetCustomAttribute オーバーロードの一覧