首页 > 其他分享 >Attribute GetCustomAttribute via method info of type

Attribute GetCustomAttribute via method info of type

时间:2024-03-09 14:36:00浏览次数:27  
标签:info via Console Attribute void TestAttribute WriteLine Foo public

[AttributeUsage(AttributeTargets.Method)]
public sealed class TestAttribute:Attribute
{
    public int Repetitions;
    public string FailureMessage;

    public TestAttribute():this(1)
    { 
    }

    public TestAttribute(int repetitions)
    {
        Repetitions = repetitions;
    }
}

class Foo
{
    [TestAttribute]
    public void Method1()
    {
        Console.WriteLine("Foo.Method1()");
    }

    [Test(20)]
    public void Method2()
    {
        Console.WriteLine("Foo.Method2()");
    }

    [Test(20,FailureMessage ="Debugging Time!")]
    public void Method3()
    {
        Console.WriteLine("Foo.Method3()");
    }
}

internal class Program
{
    static void Main(string[] args)
    {
        GetCustomAttrs();
        LogInfo();
    }

    static void GetCustomAttrs()
    {
        var mis = typeof(Foo).GetMethods();
        foreach (var mi in mis)
        {
            TestAttribute att = (TestAttribute)Attribute.GetCustomAttribute(mi, typeof(TestAttribute));
            if (att != null) 
            {
                Console.WriteLine($"Method:{mi.Name} will be tested,reps={att.Repetitions},msg={att.FailureMessage}");
            }
        }
    }
}

  

 

标签:info,via,Console,Attribute,void,TestAttribute,WriteLine,Foo,public
From: https://www.cnblogs.com/Fred1987/p/18062659

相关文章

  • Memberinfo call generic method System.InvalidOperationException: 'Late bound op
    staticvoidMain(string[]args){GenericMethod();LogInfo();}staticvoidGenericMethod(){MethodInfomi=typeof(Program).GetMethod("Echo");Console.WriteLine(mi.IsGenericMethodDefinition);Console.WriteLine(mi.Invoke(......
  • 【Winform】Button使用汇总
    一、利用委托异步实现Button长按循环执行事件来源:C#Winform利用委托异步实现Button长按,TextBox内值累加累减,弹起停止_c#根据按钮按下和弹起委托-CSDN博客boolisAddMouseDown=false;//加按钮是否按下doublethisSetValue=0D;......
  • C++ Qt开发:QHostInfo主机地址查询组件
    Qt是一个跨平台C++图形界面开发库,利用Qt可以快速开发跨平台窗体应用程序,在Qt中我们可以通过拖拽的方式将不同组件放到指定的位置,实现图形化开发极大的方便了开发效率,本章将重点介绍如何运用QHostInfo组件实现对主机地址查询功能。在Qt网络编程中,QHostInfo是一个强大而灵活的组件......
  • C# implement late binding via type in runtime
    staticvoidRuntimeGetTypeLateBinding(){objects="Hello";PropertyInfopi=s.GetType().GetProperty("Length");Console.WriteLine((int)pi.GetValue(s,null));}  DynamicallycallmethodGetMethod()via reflectionan......
  • fink泛型参数问题和TypeHint TypeInformation Types区别
    TypeHint,TypeInformation,Types区别TypeInformation是flink的类型定义,TypeHint是描述用于描述泛型参数的辅助类,Types是一个封装了常用TypeInformation的工具类描述问题下面一段代码的有两个参数,第一个来自数据流元素,他的本质是入参。第二个是出参,效果和返回值类似第一个参......
  • info-item
     <templatev-for="(item,index)indata1":key="index"><divclass="info-item"><divclass="info-item-left">{{item.time}}</div><divclass="info-item-right">......
  • 在winform中如何嵌入第三方软件窗体✨
    相关win32api的学习✨SetParent[DllImport("user32.dll",EntryPoint="SetParent")]privatestaticexternIntPtrSetParent(IntPtrhWndChild,IntPtrhWndNewParent);//将外部窗体嵌入程序语法:HWNDSetParent([in]HWNDhWndChild,[in,o......
  • 并查集解mex_cf932_B. Informatics in MAC
    目录题目概述思路想法参考代码做题反思题目概述原题参考:B.InformaticsinMAC给出一个长度为n的数组,问是否可以将其分为k个(k>1)mex相同的数组,如果可以的话,作出一种划分思路想法假设一个数组可以被分为k(k>2)个区间,每个区间的mex相同,那么可以确定的是,该数组中一定不存在mex这......
  • Qt QMessageBox::information 自定义按钮
    一.基本简介在使用QT的弹窗提示时,习惯使用QMessageBox::informationQMessageBox::questionQMessageBox::warningQMessageBox::critical一般对于按钮,是使用系统提供的默认按钮例如:QMessageBox::Ok|QMessageBox::Cancel等二.如果要自己定义按钮,使用自定义的按钮文字,该怎么......
  • VSTO:WinForms如何引用Ribbon.Invalidate
    问题描述:近期项目需要在VSTO插件中设计WinForms界面,该界面需要实现一个功能:当WinForms从外部应用中获取数据后,将其传递到editbox显示栏内。项目开发中遇到以下问题:WinForms中实例化Ribbon后,再引用其中的函数或Invalidate功能,在运行时会报错:System.NullReferenceException:“未将......