Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:从调用堆栈读取特定类型的最后一个自定义属性_C#_Custom Attributes - Fatal编程技术网

C#:从调用堆栈读取特定类型的最后一个自定义属性

C#:从调用堆栈读取特定类型的最后一个自定义属性,c#,custom-attributes,C#,Custom Attributes,我想用自定义属性装饰一些函数。这些函数不会返回特定类型,实际上可以返回任何类型 现在这个函数将调用任意数量的其他函数,然后在操作中的某个时刻,我想知道“调用堆栈中最后一个自定义属性的文本/值是什么” 我该怎么做 例如: [CustomAttribute("Hello World...")] public void SomeFunction { return Func1("myparam") } public void Func1(string xx) { return Func

我想用自定义属性装饰一些函数。这些函数不会返回特定类型,实际上可以返回任何类型

现在这个函数将调用任意数量的其他函数,然后在操作中的某个时刻,我想知道“调用堆栈中最后一个自定义属性的文本/值是什么”

我该怎么做

例如:

[CustomAttribute("Hello World...")]
public void SomeFunction
{
    return Func1("myparam")
}

public void Func1(string xx)
{
    return Func2(xx)
}

public string Func2(string yy)
{
    //I would like to know what is the value\text of the attribute "CustomAttribute".
    //If there are multiples in the call stack, return the last one (which might be in another class and project as Func2)
}

正如@joe Sewell所提到的,总体方法是迭代stackTrace并检查每个方法是否具有属性。与结合会得到以下结果:

下面是一个如何执行此操作的示例:

[System.AttributeUsage(System.AttributeTargets.Method)]
public class MyAttribute : Attribute
{
    public string Value { get; }

    public MyAttribute(string value)
    {
        Value = value;
    }

    public static string GetValueOfFirstInStackTrace()
    {
        StackTrace stackTrace = new StackTrace();           
        StackFrame[] stackFrames = stackTrace.GetFrames(); 

        foreach (var stackFrame in stackFrames)
        {
            var method = stackFrame.GetMethod();
            MyAttribute attr = (MyAttribute)method.GetCustomAttributes(typeof(MyAttribute), true).FirstOrDefault();
            if (attr != null)
            {
                string value = attr.Value;  
                return value;
            }
        }

        return null;
    }
}

正如@joe Sewell所提到的,总体方法是迭代stackTrace并检查每个方法是否具有属性。与结合会得到以下结果:

下面是一个如何执行此操作的示例:

[System.AttributeUsage(System.AttributeTargets.Method)]
public class MyAttribute : Attribute
{
    public string Value { get; }

    public MyAttribute(string value)
    {
        Value = value;
    }

    public static string GetValueOfFirstInStackTrace()
    {
        StackTrace stackTrace = new StackTrace();           
        StackFrame[] stackFrames = stackTrace.GetFrames(); 

        foreach (var stackFrame in stackFrames)
        {
            var method = stackFrame.GetMethod();
            MyAttribute attr = (MyAttribute)method.GetCustomAttributes(typeof(MyAttribute), true).FirstOrDefault();
            if (attr != null)
            {
                string value = attr.Value;  
                return value;
            }
        }

        return null;
    }
}

在运行时处理自定义属性通常涉及
System.Reflection
API。我建议从编写代码开始,获取您所知道的特定硬编码方法的
CustomAttribute
值。然后,您可以修改该代码以搜索
System.Diagnostics.StackTrace
类的实例。在运行时处理自定义属性通常涉及
System.Reflection
API。我建议从编写代码开始,获取您所知道的特定硬编码方法的
CustomAttribute
值。然后,您可以修改该代码以搜索
System.Diagnostics.StackTrace
类的实例。