本文整理汇总了C#中System.Attribute.GetCustomAttributes方法的典型用法代码示例。如果您正苦于以下问题:C# Attribute.GetCustomAttributes方法的具体用法?C# Attribute.GetCustomAttributes怎么用?C# Attribute.GetCustomAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Attribute
的用法示例。
在下文中一共展示了Attribute.GetCustomAttributes方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Reflection;
using System.ComponentModel;
// Assign some attributes to the module.
[module:Description("A sample description")]
// Set the module's CLSCompliant attribute to false
// The CLSCompliant attribute is applicable for /target:module.
[module:CLSCompliant(false)]
namespace CustAttrs2CS {
class DemoClass {
static void Main(string[] args) {
Type clsType = typeof(DemoClass);
// Get the Module type to access its metadata.
Module module = clsType.Module;
// Iterate through all the attributes for the module.
foreach(Attribute attr in Attribute.GetCustomAttributes(module)) {
// Check for the Description attribute.
if (attr.GetType() == typeof(DescriptionAttribute))
Console.WriteLine("Module {0} has the description " +
"\"{1}\".", module.Name,
((DescriptionAttribute)attr).Description);
// Check for the CLSCompliant attribute.
else if (attr.GetType() == typeof(CLSCompliantAttribute))
Console.WriteLine("Module {0} {1} CLSCompliant.",
module.Name,
((CLSCompliantAttribute)attr).IsCompliant ?
"is" : "is not");
}
}
}
}
/*
* Output:
* Module CustAttrs2CS.exe is not CLSCompliant.
* Module CustAttrs2CS.exe has the description "A sample description".
*/
示例2: ParamArrayAndDesc
//引入命名空间
using System;
using System.Reflection;
using System.ComponentModel;
namespace CustAttrs5CS {
public class AClass {
public void ParamArrayAndDesc(
// Add ParamArray (with the keyword) and Description attributes.
[Description("This argument is a ParamArray")]
params int[] args)
{}
}
class DemoClass {
static void Main(string[] args) {
// Get the Class type to access its metadata.
Type clsType = typeof(AClass);
// Get the type information for the method.
MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
if (mInfo != null) {
// Get the parameter information.
ParameterInfo[] pInfo = mInfo.GetParameters();
if (pInfo != null) {
// Iterate through all the attributes for the parameter.
foreach(Attribute attr in
Attribute.GetCustomAttributes(pInfo[0])) {
// Check for the ParamArray attribute.
if (attr.GetType() == typeof(ParamArrayAttribute))
Console.WriteLine("Parameter {0} for method {1} " +
"has the ParamArray attribute.",
pInfo[0].Name, mInfo.Name);
// Check for the Description attribute.
else if (attr.GetType() ==
typeof(DescriptionAttribute)) {
Console.WriteLine("Parameter {0} for method {1} " +
"has a description attribute.",
pInfo[0].Name, mInfo.Name);
Console.WriteLine("The description is: \"{0}\"",
((DescriptionAttribute)attr).Description);
}
}
}
}
}
}
}
/*
* Output:
* Parameter args for method ParamArrayAndDesc has a description attribute.
* The description is: "This argument is a ParamArray"
* Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
*/
示例3: UnmanagedAttribute
//引入命名空间
using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;
namespace CustAttrs4CS
{
// Define an enumeration of Win32 unmanaged types
public enum UnmanagedType
{
User,
GDI,
Kernel,
Shell,
Networking,
Multimedia
}
// Define the Unmanaged attribute.
public class UnmanagedAttribute : Attribute
{
// Storage for the UnmanagedType value.
protected UnmanagedType thisType;
// Set the unmanaged type in the constructor.
public UnmanagedAttribute(UnmanagedType type)
{
thisType = type;
}
// Define a property to get and set the UnmanagedType value.
public UnmanagedType Win32Type
{
get { return thisType; }
set { thisType = Win32Type; }
}
}
// Create a class for an imported Win32 unmanaged function.
public class Win32 {
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(int hWnd, String text,
String caption, uint type);
}
public class AClass {
// Add some attributes to Win32CallMethod.
[Obsolete("This method is obsolete. Use managed MsgBox instead.")]
[Unmanaged(UnmanagedType.User)]
public void Win32CallMethod()
{
Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);
}
}
class DemoClass {
static void Main(string[] args)
{
// Get the AClass type to access its metadata.
Type clsType = typeof(AClass);
// Get the type information for Win32CallMethod.
MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
if (mInfo != null)
{
// Iterate through all the attributes of the method.
foreach(Attribute attr in
Attribute.GetCustomAttributes(mInfo)) {
// Check for the Obsolete attribute.
if (attr.GetType() == typeof(ObsoleteAttribute))
{
Console.WriteLine("Method {0} is obsolete. " +
"The message is:",
mInfo.Name);
Console.WriteLine(" \"{0}\"",
((ObsoleteAttribute)attr).Message);
}
// Check for the Unmanaged attribute.
else if (attr.GetType() == typeof(UnmanagedAttribute))
{
Console.WriteLine(
"This method calls unmanaged code.");
Console.WriteLine(
String.Format("The Unmanaged attribute type is {0}.",
((UnmanagedAttribute)attr).Win32Type));
AClass myCls = new AClass();
myCls.Win32CallMethod();
}
}
}
}
}
}
输出:
First, the compilation yields the warning, "...This method is obsolete. Use managed MsgBox instead." Second, execution yields a message box with a title of "Caution!" and message text of "This is an unmanaged call." Third, the following text is displayed in the console window: Method Win32CallMethod is obsolete. The message is: "This method is obsolete. Use managed MsgBox instead." This method calls unmanaged code. The Unmanaged attribute type is User.
示例4: AssemblyTitle
//引入命名空间
using System;
using System.Reflection;
[assembly: AssemblyTitle("CustAttrs1CS")]
[assembly: AssemblyDescription("GetCustomAttributes() Demo")]
[assembly: AssemblyCompany("Microsoft")]
class Example {
static void Main() {
// Get the Assembly object to access its metadata.
Assembly assy = typeof(Example).Assembly;
// Iterate through the attributes for the assembly.
foreach(Attribute attr in Attribute.GetCustomAttributes(assy)) {
// Check for the AssemblyTitle attribute.
if (attr.GetType() == typeof(AssemblyTitleAttribute))
Console.WriteLine("Assembly title is \"{0}\".",
((AssemblyTitleAttribute)attr).Title);
// Check for the AssemblyDescription attribute.
else if (attr.GetType() ==
typeof(AssemblyDescriptionAttribute))
Console.WriteLine("Assembly description is \"{0}\".",
((AssemblyDescriptionAttribute)attr).Description);
// Check for the AssemblyCompany attribute.
else if (attr.GetType() == typeof(AssemblyCompanyAttribute))
Console.WriteLine("Assembly company is {0}.",
((AssemblyCompanyAttribute)attr).Company);
}
}
}
输出:
Assembly title is "CustAttrs1CS". Assembly description is "GetCustomAttributes() Demo". Assembly company is Microsoft.