Module.IsDefined(Type, Boolean) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengembalikan nilai yang menunjukkan apakah jenis atribut yang ditentukan telah diterapkan ke modul ini.
public:
virtual bool IsDefined(Type ^ attributeType, bool inherit);
public virtual bool IsDefined (Type attributeType, bool inherit);
abstract member IsDefined : Type * bool -> bool
override this.IsDefined : Type * bool -> bool
Public Overridable Function IsDefined (attributeType As Type, inherit As Boolean) As Boolean
Parameter
- attributeType
- Type
Jenis atribut kustom yang akan diuji.
- inherit
- Boolean
Argumen ini diabaikan untuk objek tipe ini.
Mengembalikan
true
jika satu atau beberapa instans attributeType
telah diterapkan ke modul ini; jika tidak, false
.
Penerapan
Pengecualian
attributeType
adalah null
.
attributeType
bukan objek yang Type disediakan oleh runtime. Misalnya, attributeType
adalah TypeBuilder objek .
Contoh
Contoh berikut menunjukkan penggunaan IsDefined
metode .
using namespace System;
using namespace System::Reflection;
namespace ReflectionModule_Examples
{
//A very simple custom attribute.
[AttributeUsage(AttributeTargets::Class|AttributeTargets::Module)]
public ref class MySimpleAttribute: public Attribute
{
private:
String^ name;
public:
MySimpleAttribute( String^ newName )
{
name = newName;
}
};
}
//Define a module-level attribute.
[module:ReflectionModule_Examples::MySimpleAttribute("module-level")];
int main()
{
array<System::Reflection::Module^>^moduleArray;
moduleArray = ReflectionModule_Examples::MySimpleAttribute::typeid->Assembly->GetModules( false );
//In a simple project with only one module, the module at index
// 0 will be the module containing these classes.
System::Reflection::Module^ myModule = moduleArray[ 0 ];
Type^ myType;
myType = myModule->GetType( "ReflectionModule_Examples.MySimpleAttribute" );
Console::WriteLine( "IsDefined(MySimpleAttribute) = {0}", myModule->IsDefined( myType, false ) );
}
using System;
using System.Reflection;
//Define a module-level attribute.
[module: ReflectionModule_Examples.MySimpleAttribute("module-level")]
namespace ReflectionModule_Examples
{
class MyMainClass
{
static void Main()
{
Module[] moduleArray;
moduleArray = typeof(MyMainClass).Assembly.GetModules(false);
//In a simple project with only one module, the module at index
// 0 will be the module containing these classes.
Module myModule = moduleArray[0];
Type myType;
myType = myModule.GetType("ReflectionModule_Examples.MySimpleAttribute");
Console.WriteLine("IsDefined(MySimpleAttribute) = {0}", myModule.IsDefined(myType, false));
}
}
//A very simple custom attribute.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)]
public class MySimpleAttribute : Attribute
{
private string name;
public MySimpleAttribute(string newName)
{
name = newName;
}
}
}
Imports System.Reflection
'Define a module-level attribute.
<Module: ReflectionModule_Examples.MySimpleAttribute("module-level")>
'Define a module-level attribute.
Namespace ReflectionModule_Examples
Class MyMainClass
Shared Sub Main()
Dim moduleArray() As [Module]
moduleArray = GetType(MyMainClass).Assembly.GetModules(False)
'In a simple project with only one module, the module at index
' 0 will be the module containing these classes.
Dim myModule As [Module] = moduleArray(0)
Dim myType As Type
myType = myModule.GetType("ReflectionModule_Examples.MySimpleAttribute")
Console.WriteLine("IsDefined(MySimpleAttribute) = {0}", myModule.IsDefined(myType, False))
End Sub
End Class
'A very simple custom attribute.
<AttributeUsage(AttributeTargets.Class Or AttributeTargets.Module)> _
Public Class MySimpleAttribute
Inherits Attribute
Private name As String
Public Sub New(ByVal newName As String)
name = newName
End Sub
End Class
End Namespace 'ReflectionModule_Examples