如何获得IDictionary的参数以前的方法调用c# ?

时间:2022-04-27 02:23:58

I would like to be able to get a list of parameters in the form of a IDictionary<string, object> of the previous method called. There is one catch: I cannot make use of third-party Aspect Oriented Programming framework even when it's free.

我希望能够以IDictionary 的形式获得一个参数列表。有一个问题:我不能使用第三方面向方面的编程框架,即使它是免费的。 ,>

For example:

例如:

using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Question {
    internal class Program {
        public static void Main(string[] args) {
            var impl = new Implementation();
            impl.MethodA(1, "two", new OtherClass { Name = "John", Age = 100 });
        }
    }

    internal class Implementation {
        public void MethodA(int param1, string param2, OtherClass param3) {
            Logger.LogParameters();
        }
    }

    internal class OtherClass {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    internal class Logger {
        public static void LogParameters() {
            var parameters = GetParametersFromPreviousMethodCall();
            foreach (var keyValuePair in parameters)
                Console.WriteLine(keyValuePair.Key + "=" + keyValuePair.Value);
                // keyValuePair.Value may return a object that maybe required to
                // inspect to get a representation as a string.
        }

        private static IDictionary<string, object> GetParametersFromPreviousMethodCall() {
            throw new NotImplementedException("I need help here!");
        }
    }
}

Any suggestion or ideas? Feel free to use reflection if necessary.

任何建议或想法吗?如果有必要,可以使用反射。

2 个解决方案

#1


2  

You could use StackTrace to get all you need:

您可以使用StackTrace来获取所需的所有信息:

var trace = new System.Diagnostics.StackTrace();
var frame = trace.GetFrame(1); //previous
var method = frame.GetMethod();

Now you have a MethodBase instance.

现在有了一个MethodBase实例。

You could get the Name by:

你可以通过:

var method = method.Name;

and the parameters by MethodBase.GetParameters.

以及MethodBase.GetParameters。

For example:

例如:

var dict = new Dictionary<string, object>();
foreach (var param in method.GetParameters())
{
    dict.Add(param.Name, param.DefaultValue);
}

#2


2  

I think the best you can do without AOP is use the StackFrame and get the method that was called.

我认为,在没有AOP的情况下,最好的办法就是使用StackFrame并获得所调用的方法。

I imagine this would require way too much overhead. What if you passed in a variable that you modified? You would have to allocate extra space just to store the original value before it was modified within the method. That could get out of hand really quick

我想这需要太多的开销。如果你传递一个你修改过的变量呢?在方法中修改原始值之前,您必须分配额外的空间来存储原始值。这可能很快就会失控

#1


2  

You could use StackTrace to get all you need:

您可以使用StackTrace来获取所需的所有信息:

var trace = new System.Diagnostics.StackTrace();
var frame = trace.GetFrame(1); //previous
var method = frame.GetMethod();

Now you have a MethodBase instance.

现在有了一个MethodBase实例。

You could get the Name by:

你可以通过:

var method = method.Name;

and the parameters by MethodBase.GetParameters.

以及MethodBase.GetParameters。

For example:

例如:

var dict = new Dictionary<string, object>();
foreach (var param in method.GetParameters())
{
    dict.Add(param.Name, param.DefaultValue);
}

#2


2  

I think the best you can do without AOP is use the StackFrame and get the method that was called.

我认为,在没有AOP的情况下,最好的办法就是使用StackFrame并获得所调用的方法。

I imagine this would require way too much overhead. What if you passed in a variable that you modified? You would have to allocate extra space just to store the original value before it was modified within the method. That could get out of hand really quick

我想这需要太多的开销。如果你传递一个你修改过的变量呢?在方法中修改原始值之前,您必须分配额外的空间来存储原始值。这可能很快就会失控