让我用下面的例子来解释我的问题:
public string ExampleFunction(string Variable) {
return something;
}
string WhatIsMyName = "Hello World"';
string Hello = ExampleFunction(WhatIsMyName);
Run Code Online (Sandbox Code Playgroud)
当我将变量"WhatIsMyName"传递给示例函数时,我希望能够获取原始变量名称的字符串.也许是这样的:
Variable.OriginalName.ToString()
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点?
可能重复:
查找传递给C#中函数的变量名称
下面的课程包含现场城市.
我需要动态确定字段名称,因为它是在类声明中输入的,即我需要从对象城市的实例中获取字符串"city".
我试图通过检查其在DoSomething()中的类型来做到这一点,但在检查调试器中的Type的内容时找不到它.
可能吗?
public class Person
{
public string city = "New York";
public Person()
{
}
public void DoSomething()
{
Type t = city.GetType();
string field_name = t.SomeUnkownFunction();
//would return the string "city" if it existed!
}
}
Run Code Online (Sandbox Code Playgroud)
下面他们的答案中的一些人问我为什么要这样做.这就是原因.
在我的真实世界中,城市上方有一个自定义属性.
[MyCustomAttribute("param1", "param2", etc)]
public string city = "New York";
Run Code Online (Sandbox Code Playgroud)
我需要在其他代码中使用此属性.要获取属性,我使用反射.在反射代码中我需要输入字符串"city"
MyCustomAttribute attr;
Type t = typeof(Person);
foreach (FieldInfo field in t.GetFields())
{
if (field.Name == "city")
{
//do stuff when we find the field that has …Run Code Online (Sandbox Code Playgroud) 我有变数
public string MyVariable;
Run Code Online (Sandbox Code Playgroud)
我需要使用变量名作为字符串.例:
var a = MyVariable.NameVariable();
// a = "MyVariable"
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
我想创建你的miniORM(NHibernate太重了,需要一个单独的库).目前问题是我必须指出一堆常量.例如:
public const string FieldIdRules = "idRules"
Run Code Online (Sandbox Code Playgroud)
然后在samonapisannom profiler中进行交易.我看到Hibernate不需要指定文本值(对于字段的比例).我想实现同样的目标.
对不起我的英文不好
我试图做出类似的东西:
// int counter; - this is changing in ther other parts of program
bool abc1; bool abc2; bool abc3; bool abc4;
if("abc" + counter == true)
{
//some code
}
Run Code Online (Sandbox Code Playgroud)
无论如何,我需要将string和int转换为bool名称.我怎样才能做到这一点?