gpt4 book ai didi

c# - 将 ContextAttribute 与方法一起使用

转载 作者:行者123 更新时间:2023-11-30 21:24:51 24 4
gpt4 key购买 nike

目标方法的属性不起作用。代码如下。可能是什么问题?

using System;
namespace AttributeProgram
{

class Program:ContextBoundObject
{
[TestAttribute("Hello")]
public void Print()
{
Console.WriteLine("How are you?");
}
static void Main(string[] args)
{
Program obj = new Program();
obj.Print();
}
}

[AttributeUsage(AttributeTargets.Method)]
class TestAttribute : System.Runtime.Remoting.Contexts.ContextAttribute
{
public TestAttribute(string Name) : base("Test")
{
Console.WriteLine(Name);
}
}
}

最佳答案

因为您继承自 ContextAttribute,根据文档,它只能应用于类:

[SerializableAttribute]
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.Class)]
[SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure)]
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.Infrastructure)]
public class ContextAttribute : Attribute,
IContextAttribute, IContextProperty

关于c# - 将 ContextAttribute 与方法一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/990139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com