简体   繁体   English

获取传递给方法C#的变量的声明名称

[英]Getting the declared name of a variable passed into a method C#

I've seen this discussed a few times before, and im not really sure if this is the best thing for me to be doing so any suggestions for improvement would be welcomed. 我已经看过几次这个问题了,我不确定这是否对我来说最好,所以任何改进的建议都会受到欢迎。

I'm trying to build some detailed logging for my Selenium tests. 我正在尝试为Selenium测试构建一些详细的日志记录。

I have a method call which is: 我有一个方法调用是:

Admin_Login.Username.SetText("MyUsername");

Admin_Login is just a standard class. Admin_Login只是一个标准类。 However "Username" is a variable passed into the "SetText" method which is shown below: 但是,“用户名”是传递给“ SetText”方法的变量,如下所示:

public static void SetText(this By identifier, string value)
{
    StackTrace stackTrace = new StackTrace();
    string methodName = stackTrace.GetFrame(1).GetMethod().Name;
    string className = stackTrace.GetFrame(1).GetMethod().DeclaringType.FullName;


    //I do some logging here for the manual testers so they can debug after a run
    TestDriver.ResultsLog.LogComment("Class: " + className);
    TestDriver.ResultsLog.LogComment("Calling Method: " + methodName);
    TestDriver.ResultsLog.LogComment("Calling Element: " + identifier.toString());

    IWebElement element = WebDriver.driver.FindElementSafe(identifier);
    ((IJavaScriptExecutor)WebDriver.driver).ExecuteScript("arguments[0].value = arguments[1]", element, value);
}

For reference, here is the Admin class: 供参考,这是Admin类:

public class Admin_Login
{
    public static By Username = By.Id("ctl00_MainContent_ctlLoginView_ctlLogin_UserName");
}

So I can log the class name and method name fine, but what I need to find out is the declared name of the "identifier" variable thats passed into the method. 因此,我可以很好地记录类名称和方法名称,但是我需要找出的是传递给方法的“标识符”变量的声明名称。

So the value should be "Username" in this instance. 因此,此实例中的值应为“ Username”。

I've tried a couple of suggestions that yield the name of Identifier as "identifier" but that's really not what I want. 我尝试了一些建议,将Identifier的名称显示为“ identifier”,但这实际上并不是我想要的。 I need to know which object the SetText method was called off, if that makes sense. 我需要知道SetText方法被取消了哪个对象,如果可以的话。

As I said maybe i'm not going about this the right way so any suggestions would be helpful. 正如我说的那样,也许我没有采取正确的方法,所以任何建议都将有所帮助。

I can not believe I'm actually suggesting this because of how dirty this solution is... but, it is a solution... sooooo... 我不敢相信我实际上是在建议这样做,因为该解决方案有多脏...但是,这是一个解决方案...太...

class Program
{
    static void Main()
    {
        var foo = new Foo();
        var bar = foo;

        foo.Hit();
        bar.Hit();
    }
}

public static class Extensions
{
    public static void Hit(this Foo foo, [CallerMemberName] string name = null, [CallerFilePath] string path = null, [CallerLineNumber] int lineNumber = -1)
    {
        string callerVariableName;

        using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            StreamReader reader = new StreamReader(stream);
            string csharpFile = reader.ReadToEnd();
            string[] lines = csharpFile.Split('\n');
            string callingLine = lines[lineNumber - 1];

            Match matchVariableName = Regex.Match(callingLine, @"([a-zA-Z]+[^\.]*)\.Hit\(\)");

            callerVariableName = matchVariableName.Groups[1].Value;
        }

        Console.WriteLine("\n///////////////////////////////");
        Console.WriteLine("Caller method name:   {0}", name);
        Console.WriteLine("Caller variable name: {0}", callerVariableName);
        Console.WriteLine("File Path:            {0}", path);
        Console.WriteLine("Line number:          {0}", lineNumber);

    }
}

public class Foo
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM