创建函数来循环访问 C# 中方法的属性?

发布于 2024-09-15 19:05:00 字数 209 浏览 1 评论 0原文

我想要做的是创建一个函数,该函数将从 .cs 文件中的每个方法调用,然后获取传递给该方法的所有属性名称和值并创建它们的字符串。

这将用于调试日志,以显示错误发生时每个属性的值,作为每次发生错误时手动创建字符串的替代方法。

这可能吗?我查看了反射,我认为这是我所需要的,但我不确定是否将当前方法从自身传递到新函数中,然后在函数内部将方法的属性拉出(我假设这是反射发挥作用。)

What I am trying to do is create a function that will be called from each method within a .cs file to then get all of the property names and values that are passed to the method and create a string of them.

This will be used for a debug log to show what the value of each property was when the error occurred as an alternative to manually creating the string each time an error occurs.

Is this possible? I have looked at reflection and I think it is what I need but I am not sure about passing the current method from itself into the new function, and then inside of the function pulling the properties of the method out (I assume this is where the reflection comes into play.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浴红衣 2024-09-22 19:05:00

一种更简单的方法可能是捕获异常,然后仅记录您感兴趣的属性的值。尝试使用反射来实现您想要做的事情可能是过度设计。

ABC abc = new ABC();
try {
    a.xyz = "Runtime value";
    //Exception thrown here ...
}
catch (Exception ex)
{    
    Log.LogDebugFormatted(
      "Exception caught. abc.xyz value: {0}. Exception: {1}", abc.xyz, ex);
    throw ex;
}

A simpler approach might be to catch the exception, and then only log the values of the properties you're interested in. Trying to achieve what you want to do using reflection is possibly over-engineering.

ABC abc = new ABC();
try {
    a.xyz = "Runtime value";
    //Exception thrown here ...
}
catch (Exception ex)
{    
    Log.LogDebugFormatted(
      "Exception caught. abc.xyz value: {0}. Exception: {1}", abc.xyz, ex);
    throw ex;
}
你不是我要的菜∠ 2024-09-22 19:05:00

怎么样:

void Main()
{
var test = new TestObject();
test.a = "123";
test.b = "456";
var testProperties = (from prop in test.GetType().GetProperties() 
                        select new KeyValuePair<string,string>(prop.Name,prop.GetValue(test,null).ToString()));
foreach (var property in testProperties)
{
    Console.WriteLine(string.Format("Name: {1}{0}Value: {2}",Environment.NewLine,property.Key,property.Value));
}

var testMethods = (from meth in test.GetType().GetMethods() select new { Name = meth.Name, Parameters = 
    (from param in meth.GetParameters() 
        select new {Name = param.Name, ParamType=param.GetType().Name})} );

foreach (var method in testMethods)
{
    Console.WriteLine(string.Format("Method: {0}",method.Name));
    foreach(var param in method.Parameters)
    {
        Console.WriteLine("Param: " + param.Name + " (" + param.ParamType + ")");
    }
}
}

class TestObject
{
public string a { get; set; }
public string b { get; set; }
public string testMethod(string param1,int param2){return string.Empty;}
}

编辑 - 抱歉,我似乎误读了这个问题。我不知道如何做你的要求,或者即使这是可能的。

How about something like:

void Main()
{
var test = new TestObject();
test.a = "123";
test.b = "456";
var testProperties = (from prop in test.GetType().GetProperties() 
                        select new KeyValuePair<string,string>(prop.Name,prop.GetValue(test,null).ToString()));
foreach (var property in testProperties)
{
    Console.WriteLine(string.Format("Name: {1}{0}Value: {2}",Environment.NewLine,property.Key,property.Value));
}

var testMethods = (from meth in test.GetType().GetMethods() select new { Name = meth.Name, Parameters = 
    (from param in meth.GetParameters() 
        select new {Name = param.Name, ParamType=param.GetType().Name})} );

foreach (var method in testMethods)
{
    Console.WriteLine(string.Format("Method: {0}",method.Name));
    foreach(var param in method.Parameters)
    {
        Console.WriteLine("Param: " + param.Name + " (" + param.ParamType + ")");
    }
}
}

class TestObject
{
public string a { get; set; }
public string b { get; set; }
public string testMethod(string param1,int param2){return string.Empty;}
}

edit - Sorry I seem to have misread the question. I have no idea how to do what your asking, or even if it's possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文