简体   繁体   English

如何在方法内部获取传递参数的nameof()?

[英]How to get nameof() of the passed parameter inside of the method?

I am searching for solution for my project, where I have a Dictionary<string, object> and a helper method witch is trying to get some specific value from this dictionary.我正在为我的项目寻找解决方案,其中我有一个Dictionary<string, object>和一个辅助方法,女巫正试图从这个字典中获取一些特定的值。

As you can see below, if I don't have a stringKey , I am trying to get that stringKey from the @object (now it will return "object" ).正如您在下面看到的,如果我没有stringKey ,我正在尝试从@object获取该stringKey (现在它将返回"object" )。 Surely, I can do it in calling, but it looks too bad for me.当然,我可以在打电话时做到这一点,但对我来说这看起来太糟糕了。

Is there any way to do that in this method?有没有办法在这种方法中做到这一点?

public static bool GetValue<T>(Dictionary<string, object> dict, ref T @object, 
    string stringKey = "") 
{
    if (string.IsNullOrEmpty(stringKey))
    {
       stringKey = nameof(@object);
    }

    dict.TryGetValue(stringKey, out var Object);
    if (Object != null)
    {
        @object = (T)Object;
        return true;
    }

    return false;
}

Example of calling:调用示例:

DPH.GetValue(dictParam, ref Browser);

Expectations in method is that stringKey will be "Browser" , as it will be in this case:方法中的期望是stringKey将是"Browser" ,因为它在这种情况下:

DPH.GetValue(dictParam, ref Browser, nameof(Browser));

There are specific needs and we are not able to refactor that dictionary to our object or our class at this moment, calling with nameof() is possible, but not a solution.有特定需求,我们目前无法将该字典重构为我们的 object 或 class,可以使用nameof()调用,但不是解决方案。

You can't.你不能。

Sorry.对不起。 There are some nice workarounds, but they all include completely changing what you do right now.有一些很好的解决方法,但它们都包括完全改变你现在所做的事情。 You cannot get the name of a variable that way.您无法以这种方式获取变量的名称。

The only thing remotely similar to what you want is System.Runtime.CompilerServices.CallerMemberName and that only works to get the name of the function that called your function.唯一与您想要的远程相似的是System.Runtime.CompilerServices.CallerMemberName并且仅用于获取调用您的 function 的 function 的名称。

This is not possible in C#.这在 C# 中是不可能的。

By calling通过调用

DPH.GetValue(dictParam, ref Browser);

you are providing object references to the called method.您正在提供对被调用方法的object 引用 Browser is a named variable in this case (or a property, or a class field), but behind this named variable there is a reference to an object.在这种情况下, Browser是一个命名变量(或属性,或 class 字段),但在此命名变量后面有对 object 的引用。

Think of an object reference as of a number that describes the object location somewhere in the memory.将 object 参考视为描述 memory 某处的 object 位置的数字。

The GetValue method gets two these numbers (references) and can access the objects using these numbers (references). GetValue方法获取两个这些数字(引用),并且可以使用这些数字(引用)访问对象。

The method doesn't get any variable names.该方法没有得到任何变量名。

So you have to provide the name of the variable using the nameof(Browser) value as the third argument.因此,您必须使用nameof(Browser)值作为第三个参数来提供变量的名称。 Otherwise, it's not possible to get the name.否则,无法获得名称。

As others have already said, this doesn't appear possible to achieve because there is no way to obtain the name of the reference parameter from the caller.正如其他人已经说过的那样,这似乎无法实现,因为无法从调用者那里获取引用参数的名称。

The reason why it's not possible is because the variable name is not included in the compiled IL output.不可能的原因是编译的IL output中没有包含变量名。

Consider the following:考虑以下:

int Fred = 0;
string FredName = nameof(Fred);

This declares Fred and FredName .这声明了FredFredName However, in IL this results in:但是,在 IL 中,这会导致:

.maxstack 2
.locals init (
    [0] int32,
    [1] string
)

You will see that Fred and FredName are indeed declared but as arguments 0 and 1 .您将看到FredFredName确实被声明为 arguments 01 The variable names are not stored in the IL.变量名不存储在 IL 中。

Furthermore, the code for nameof(...) simply does this:此外, nameof(...)的代码只是这样做:

IL_0003: ldstr "Fred"
IL_0008: stloc.1

Load the hardcoded value "Fred" into local variable 1.将硬编码值"Fred"加载到局部变量 1 中。

Sadly, given this circumstance it seems impossible that you will achieve what you want.可悲的是,在这种情况下,你似乎不可能实现你想要的。

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

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