C# 如何使用属性中定义的方法?

C# 如何使用属性中定义的方法?,c#,.net,asp.net-core,C#,.net,Asp.net Core,我在检查.netcore中的filteratAttribute类时,发现它有多个在特定时间调用的方法。例如: OnAuthorization(授权上下文) 当进程请求授权时调用 我想知道,如果我正在编写自定义属性,如何使用其中定义的方法?您共享的链接不是.net core。在.net core中,ActionFilterAttribute类具有创建自定义过滤器所需的所有函数。要创建自定义筛选器,只需创建一个扩展ActionFilterAttribute的类,并覆盖您选择的函数。您可以在此处检查

我在检查
.netcore
中的
filteratAttribute
类时,发现它有多个在特定时间调用的方法。例如:

OnAuthorization(授权上下文) 当进程请求授权时调用


我想知道,如果我正在编写自定义属性,如何使用其中定义的方法?

您共享的链接不是
.net core
。在
.net core
中,
ActionFilterAttribute
类具有创建自定义过滤器所需的所有函数。要创建自定义筛选器,只需创建一个扩展
ActionFilterAttribute
的类,并覆盖您选择的函数。您可以在此处检查何时调用哪个筛选器的时间线:

对于使用此筛选器,假设您创建的类名为
MyCustomFilterAttribute
,您将在控制器中这样使用它:

public class LoginController : Controller
{
    [MyCustomFilter]
    public void Index(){

    }

}
如果要为控制器中的每个操作实现筛选器,可以将属性添加到控制器:

[MyCustomFilter]
public class LoginController : Controller

您共享的链接不是
.net core
。在
.net core
中,
ActionFilterAttribute
类具有创建自定义过滤器所需的所有函数。要创建自定义筛选器,只需创建一个扩展
ActionFilterAttribute
的类,并覆盖您选择的函数。您可以在此处检查何时调用哪个筛选器的时间线:

对于使用此筛选器,假设您创建的类名为
MyCustomFilterAttribute
,您将在控制器中这样使用它:

public class LoginController : Controller
{
    [MyCustomFilter]
    public void Index(){

    }

}
如果要为控制器中的每个操作实现筛选器,可以将属性添加到控制器:

[MyCustomFilter]
public class LoginController : Controller
简单地说

我们制作了一个测试属性:

class TestAttribute : Attribute
{
    public void Greet(string text)
    {
        Console.WriteLine(text);
    }
}
使用
GetCustomAttribute
我们正在检索该属性,并对其调用
Greet

[Test]
class Program
{
    static void Main(string[] args)
    {
        var attribute = typeof(Program).GetCustomAttribute<TestAttribute>();
        attribute.Greet("Hello World");
        Console.ReadKey();
    }
}
[测试]
班级计划
{
静态void Main(字符串[]参数)
{
var attribute=typeof(程序).GetCustomAttribute();
attribute.Greet(“你好世界”);
Console.ReadKey();
}
}
相关的:

    • 简单地说

      我们制作了一个测试属性:

      class TestAttribute : Attribute
      {
          public void Greet(string text)
          {
              Console.WriteLine(text);
          }
      }
      
      使用
      GetCustomAttribute
      我们正在检索该属性,并对其调用
      Greet

      [Test]
      class Program
      {
          static void Main(string[] args)
          {
              var attribute = typeof(Program).GetCustomAttribute<TestAttribute>();
              attribute.Greet("Hello World");
              Console.ReadKey();
          }
      }
      
      [测试]
      班级计划
      {
      静态void Main(字符串[]参数)
      {
      var attribute=typeof(程序).GetCustomAttribute();
      attribute.Greet(“你好世界”);
      Console.ReadKey();
      }
      }
      
      相关的:

      属性是哑的 属性本身不做任何事情,除了保存数据。像
      filteratAttribute
      这样的属性做任何事情的唯一原因是MVC框架查找它们,并在调用它们所应用的方法之前或之后调用它们

      MVC如何调用它们 下面是它在中的外观:

      现在我们编写一个类,将该属性应用于它的一个方法:

      public class MyClass
      {
          [HelloWorld("This is a test!!")]
          public void MyClassMethod(string text)
          {
              Console.WriteLine("MyClassMethod says '{0}'", text);
          }
      }
      
      现在,该属性本身在运行时不起任何作用:

      public class Program
      {
          public static void Main()
          {
              //By itself, the attribute does nothing
              var c = new MyClass();
              c.MyClassMethod("Main call");
          }
      }
      
      输出:

      MyClassMethod says 'Main call'
      
      OnBeforeExecute says 'This is a test!!'.
      MyClassMethod says 'Main call'
      
      但是如果我们编写代码来查找属性并调用它,我们就可以看到消息。这是一个非常简单的例子:

      public class Program
      {
          public static void Main()
          {
              var c = new MyClass();
              typeof(MyClass)
                  .GetMethod(nameof(c.MyClassMethod))
                  .GetCustomAttributes(true)
                  .OfType<HelloWorldAttribute>()
                  .First()
                  .OnBeforeExecute();
              c.MyClassMethod("Main call");
          }
      }
      
      .

      属性是哑的 属性本身不做任何事情,除了保存数据。像
      filteratAttribute
      这样的属性做任何事情的唯一原因是MVC框架查找它们,并在调用它们所应用的方法之前或之后调用它们

      MVC如何调用它们 下面是它在中的外观:

      现在我们编写一个类,将该属性应用于它的一个方法:

      public class MyClass
      {
          [HelloWorld("This is a test!!")]
          public void MyClassMethod(string text)
          {
              Console.WriteLine("MyClassMethod says '{0}'", text);
          }
      }
      
      现在,该属性本身在运行时不起任何作用:

      public class Program
      {
          public static void Main()
          {
              //By itself, the attribute does nothing
              var c = new MyClass();
              c.MyClassMethod("Main call");
          }
      }
      
      输出:

      MyClassMethod says 'Main call'
      
      OnBeforeExecute says 'This is a test!!'.
      MyClassMethod says 'Main call'
      
      但是如果我们编写代码来查找属性并调用它,我们就可以看到消息。这是一个非常简单的例子:

      public class Program
      {
          public static void Main()
          {
              var c = new MyClass();
              typeof(MyClass)
                  .GetMethod(nameof(c.MyClassMethod))
                  .GetCustomAttributes(true)
                  .OfType<HelloWorldAttribute>()
                  .First()
                  .OnBeforeExecute();
              c.MyClassMethod("Main call");
          }
      }
      

      .

      不是100%确定你在问什么,但这里有一些超级精心设计的演示代码,可以访问方法和属性化方法

      class Program
      {
          public class Hook : Attribute
          {
              public string Action { get; set; }
      
              public void Talk(string s)
              {
                  var prefix = string.IsNullOrEmpty(Action) ? "" : $"{Action} ";
                  Console.WriteLine($"{prefix}{s}");
              }
          }
      
          public class A
          {
              [Hook] public string Option1()=> "A1";
              public string Option2() => "A2";
          }
      
          public class B
          {
              [Hook(Action = "Blah")] public string Option1() => "B1";
              [Hook] public string Option2() => "B2";
          }
      
          static void Main(string[] args)
          {
              var things = new List<object>() {new A(), new B()};
              things.SelectMany(t => t.GetType().GetMethods()
                      .Select(m => (method: m, attribute: m.GetCustomAttribute(typeof(Hook), true) as Hook))
                      .Where(h => h.attribute != null)
                      .Select(h => (target: t, hook: h)))
                  .ToList()
                  .ForEach(v => v.hook.attribute.Talk(v.hook.method.Invoke(v.target, new object[] { }).ToString()));
          }
      }
      
      类程序
      {
      公共类钩子:属性
      {
      公共字符串操作{get;set;}
      公开演讲(字符串s)
      {
      var prefix=string.IsNullOrEmpty(操作)?“”:$“{Action}”;
      WriteLine($“{prefix}{s}”);
      }
      }
      公共A类
      {
      [Hook]公共字符串选项1()=>“A1”;
      公共字符串选项2()=>“A2”;
      }
      公共B级
      {
      [Hook(Action=“Blah”)]公共字符串选项1()=>“B1”;
      [Hook]公共字符串选项2()=>“B2”;
      }
      静态void Main(字符串[]参数)
      {
      var things=new List(){new A(),new B()};
      things.SelectMany(t=>t.GetType().GetMethods())
      .Select(m=>(方法:m,属性:m.GetCustomAttribute(typeof(Hook),true)作为Hook))
      .Where(h=>h.attribute!=null)
      .选择(h=>(目标:t,钩子:h)))
      托利斯先生()
      .ForEach(v=>v.hook.attribute.Talk(v.hook.method.Invoke(v.target,新对象[]{}).ToString());
      }
      }
      
      不是100%确定你在问什么,但这里有一些超级精心设计的演示代码,可以访问方法和属性化方法

      class Program
      {
          public class Hook : Attribute
          {
              public string Action { get; set; }
      
              public void Talk(string s)
              {
                  var prefix = string.IsNullOrEmpty(Action) ? "" : $"{Action} ";
                  Console.WriteLine($"{prefix}{s}");
              }
          }
      
          public class A
          {
              [Hook] public string Option1()=> "A1";
              public string Option2() => "A2";
          }
      
          public class B
          {
              [Hook(Action = "Blah")] public string Option1() => "B1";
              [Hook] public string Option2() => "B2";
          }
      
          static void Main(string[] args)
          {
              var things = new List<object>() {new A(), new B()};
              things.SelectMany(t => t.GetType().GetMethods()
                      .Select(m => (method: m, attribute: m.GetCustomAttribute(typeof(Hook), true) as Hook))
                      .Where(h => h.attribute != null)
                      .Select(h => (target: t, hook: h)))
                  .ToList()
                  .ForEach(v => v.hook.attribute.Talk(v.hook.method.Invoke(v.target, new object[] { }).ToString()));
          }
      }
      
      类程序
      {
      公共类钩子:属性
      {
      公共字符串操作{get;set;}
      公开演讲(字符串s)
      {
      var prefix=string.IsNullOrEmpty(操作)?“”:$“{Action}”;
      WriteLine($“{prefix}{s}”);
      }
      }
      公共A类
      {
      [Hook]公共字符串选项1()=>“A1”;
      公共字符串选项2()=>“A2”;
      }
      公共B级
      {
      [Hook(Action=“Blah”)]公共字符串选项1()=>“B1”;
      [Hook]公共字符串选项2()=>“B2”;
      }
      静态void Main(字符串[]参数)
      {
      var things=new List(){new A(),new B()};
      things.SelectMany(t=>t.GetType().GetMethods())
      .Select(m=>(方法:m,属性:m.GetCustomAttribute(typeo