小编Meh*_*jia的帖子

在调用方法之前处理信息的自定义属性

我已经创建了一个属性来处理一些信息,然后调用一个方法,但它没有被调用.

我想记录一些处理的值并存储在类中的静态字段中,这是我的类的其他方法调用的结果.

所以有人可以指导它.

[AttributeUsage(AttributeTargets.Method)]
internal class MyAttrib : Attribute
{
    public MyAttrib()
    {
        //This is not getting called. what am i missing
        Console.WriteLine("My Attrib called!!");
    }
}

class MyClass
{
    public MyClass()
    {
        Console.WriteLine("Constructor Created");
    }

    [MyAttrib]
    public int Opt1()
    {
        Console.WriteLine("Op1 Performed");
        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

}

static void Main(string[] args)
{
        MyClass cla = new MyClass();
        cla.Opt1();
        cla.Opt2();
        Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)

.net c# attributes

3
推荐指数
1
解决办法
5101
查看次数

标签 统计

.net ×1

attributes ×1

c# ×1