Module.GetCustomAttributes Metodo
In questo articolo
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Restituisce attributi personalizzati.
GetCustomAttributes(Boolean) |
Restituisce tutti gli attributi personalizzati. |
GetCustomAttributes(Type, Boolean) |
Ottiene gli attributi personalizzati del tipo specificato. |
- Origine:
- Module.cs
- Origine:
- Module.cs
- Origine:
- Module.cs
Restituisce tutti gli attributi personalizzati.
public:
virtual cli::array <System::Object ^> ^ GetCustomAttributes(bool inherit);
public virtual object[] GetCustomAttributes (bool inherit);
abstract member GetCustomAttributes : bool -> obj[]
override this.GetCustomAttributes : bool -> obj[]
Public Overridable Function GetCustomAttributes (inherit As Boolean) As Object()
Parametri
- inherit
- Boolean
Questo argomento viene ignorato per gli oggetti di questo tipo.
Restituisce
Matrice di tipo Object
contenente tutti gli attributi personalizzati.
Implementazioni
Esempio
Nell'esempio seguente vengono visualizzati i nomi dei moduli corrispondenti ai criteri di ricerca specificati.
using namespace System;
using namespace System::Reflection;
using namespace System::Collections;
namespace ReflectionModule_Examples
{
//Define a module-level attribute.
//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;
}
};
[module:MySimpleAttribute("module-level")];
ref class MyMainClass{};
}
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 ];
array<Object^>^attributes;
attributes = myModule->GetCustomAttributes( true );
IEnumerator^ myEnum = attributes->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ o = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( "Found this attribute on myModule: {0}.", o );
}
}
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];
object[] attributes;
attributes = myModule.GetCustomAttributes(true);
foreach(Object o in attributes)
{
Console.WriteLine("Found this attribute on myModule: {0}.", o.ToString());
}
}
}
//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")>
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 attributes() As Object
attributes = myModule.GetCustomAttributes(True)
Dim o As [Object]
For Each o In attributes
Console.WriteLine("Found this attribute on myModule: {0}", o.ToString())
Next o
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
Si applica a
.NET 9 e altre versioni
Prodotto | Versioni |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
- Origine:
- Module.cs
- Origine:
- Module.cs
- Origine:
- Module.cs
Ottiene gli attributi personalizzati del tipo specificato.
public:
virtual cli::array <System::Object ^> ^ GetCustomAttributes(Type ^ attributeType, bool inherit);
public virtual object[] GetCustomAttributes (Type attributeType, bool inherit);
abstract member GetCustomAttributes : Type * bool -> obj[]
override this.GetCustomAttributes : Type * bool -> obj[]
Public Overridable Function GetCustomAttributes (attributeType As Type, inherit As Boolean) As Object()
Parametri
- attributeType
- Type
Tipo di attributo da ottenere.
- inherit
- Boolean
Questo argomento viene ignorato per gli oggetti di questo tipo.
Restituisce
Matrice di tipo Object
contenente tutti gli attributi personalizzati del tipo specificato.
Implementazioni
Eccezioni
attributeType
è null
.
attributeType
non è un oggetto Type fornito dal runtime. Ad esempio, attributeType
è un oggetto TypeBuilder.
Esempio
Nell'esempio seguente vengono visualizzati i nomi dei moduli del tipo specificato che corrispondono ai criteri di ricerca specificati.
using namespace System;
using namespace System::Reflection;
using namespace System::Collections;
namespace ReflectionModule_Examples
{
// Define 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 ];
array<Object^>^attributes;
//Get only MySimpleAttribute attributes for this module.
attributes = myModule->GetCustomAttributes( myModule->GetType( "ReflectionModule_Examples.MySimpleAttribute", false, false ), true );
IEnumerator^ myEnum = attributes->GetEnumerator();
while ( myEnum->MoveNext() )
{
Object^ o = safe_cast<Object^>(myEnum->Current);
Console::WriteLine( "Found this attribute on myModule: {0}", o );
}
}
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];
object[] attributes;
//Get only MySimpleAttribute attributes for this module.
attributes = myModule.GetCustomAttributes(
myModule.GetType("ReflectionModule_Examples.MySimpleAttribute", false, false),
true);
foreach(Object o in attributes)
{
Console.WriteLine("Found this attribute on myModule: {0}", o.ToString());
}
}
}
// Define 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")>
' This code assumes that the root namespace is set to empty("").
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 attributes() As Object
' Get only MySimpleAttribute attributes for this module.
attributes = myModule.GetCustomAttributes( _
myModule.GetType("ReflectionModule_Examples.MySimpleAttribute", _
False, False), True)
Dim o As [Object]
For Each o In attributes
Console.WriteLine("Found this attribute on myModule: {0}", o.ToString())
Next o
End Sub
End Class
' Define 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
Si applica a
.NET 9 e altre versioni
Prodotto | Versioni |
---|---|
.NET | Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: