相关疑难解决方法(0)

获取参数的调用变量名称

关于问题从调用方法获取参数名称查找传递给C#中函数的变量名称,我仍在寻找一种方法来定义WhatDoesTheAnimalSay_WANTED方法:我想知道用作变量的名称一个参数:

public class Farm
{
    public readonly string Cow = "Muuuuhh";

    public string Cat { get; set; }

    public void MainFunction()
    {
        var dog = "WauWau";
        var kiwi = new Bird("QueeeekQueeek");

        Cat = "Miiiaaauuuu";

        // This one works but looks kinda ugly and is cumbersome when used
        WhatDoesTheAnimalSay(nameof(dog), dog);
        WhatDoesTheAnimalSay(nameof(Cow), Cow);
        WhatDoesTheAnimalSay(nameof(Cat), Cat);
        WhatDoesTheAnimalSay(nameof(kiwi), kiwi);

        WhatDoesTheAnimalSay_WRONG1(dog);
        WhatDoesTheAnimalSay_WRONG2(dog);

        WhatDoesTheAnimalSay_WANTED(dog);
        WhatDoesTheAnimalSay_WANTED(Cow);
    }

    public void WhatDoesTheAnimalSay<T>(string name, T says)
    {
        MessageBox.Show("The " + name + " …
Run Code Online (Sandbox Code Playgroud)

.net c# ixmlserializable nameof

4
推荐指数
1
解决办法
1826
查看次数

在VB.NET中将变量名存储在String中

我正在尝试将一些变量的名称存储在字符串中.例如:

Dim Foo1 as Integer
Dim Foo1Name as String

' -- Do something to set Foo1Name to the name of the other variable --

MessageBox.Show(Foo1Name & " is the variable you are looking for.")
' Outputs:
' Foo1 is the variable you are looking for.
Run Code Online (Sandbox Code Playgroud)

这将有助于我正在进行的一些调试.

vb.net string variables

3
推荐指数
1
解决办法
1万
查看次数

获取传递给方法的参数的名称

重复: 确定用作方法参数的变量的名称

有没有办法检索传递给方法的参数的名称,例如

int someParameter = 1;
Method(someParameter);

public void Method(int parameter)
{
    // I want the name of 'parameter' which will be 'someParameter'.
}
Run Code Online (Sandbox Code Playgroud)

c# reflection parameters

3
推荐指数
1
解决办法
2989
查看次数

获取变量(非硬编码)名称?

我正在寻找一种方法来检索变量名称,因此我不需要在需要时使用硬编码声明(对于属性名称等):

我几乎不相信这是可能的; 也许有人有解决方案.注意:即使不是变量,属性也会是一个举动.

'Pseudo:
Module Module1

    Sub Main()
        Dim variable = "asdf"
        Dim contact As New Contact

        Dim v1 = GetVariableName(variable) 'returns variable
        Dim v2 = GetVariableName(contact.Name) 'returns Name

    End Sub

    Class Contact
        Public ReadOnly Property Name()
            Get
                Return Nothing
            End Get
        End Property
    End Class

    Public Function GetVariableName(variable As Object) As String
        ':}
    End Function

End Module
Run Code Online (Sandbox Code Playgroud)

答案在VB或C#中都很受欢迎.

.net reflection variables declaration

3
推荐指数
2
解决办法
2586
查看次数

确定用作方法参数的变量的名称

在C#中,您如何确定调用方法时使用的变量的名称?

例:

public void MyTestMethod1()
{
  string myVar = "Hello World";
  MyTestMethod2(myVar);
}

public void MyMethod2(string parm)
{
  // do some reflection magic here that detects that this method was called using a variable called 'myVar'
}
Run Code Online (Sandbox Code Playgroud)

我知道参数可能并不总是变量,但我使用它的地方是在一些验证代码中,我希望开发人员可以明确说明他们验证的值的友好名称,如果他们不'然后它只是从他们称之为方法的var的名称推断它...

c# reflection parameters

2
推荐指数
1
解决办法
656
查看次数

如何使用Math.Max,Math.Min找到三个最大和三个最小的化学品

我正在使用Math.Max和Math.Min来查找以下代码中的最大和最小小数:

decimal[] numbers = { d1, d2, d3, d4, d5, d6, d7, d8, d9, d10 };
decimal biggestNumber = numbers.Max();
decimal lowestNumber = numbers.Min();
lblS1Val.Text = biggestNumber.ToString();
lblO1Val.Text = lowestNumber.ToString();
Run Code Online (Sandbox Code Playgroud)

我想找到三个最大和三个最小的数字.我可以用Math.Max/Min做到吗?

c# asp.net

2
推荐指数
2
解决办法
429
查看次数