C# 按propertyname从列表中选择常量
通过使用C# 按propertyname从列表中选择常量,c#,properties,C#,Properties,通过使用var propertyString=constants.Where(=test1).ToString()?您必须通过反射来获取常量字符串 public class Constants { public const string test1 = "This is testvalue1;" public const string test2 = "This is testvalue1;" public const string test3 = "This is te
var propertyString=constants.Where(=test1).ToString()
?您必须通过反射来获取常量字符串
public class Constants
{
public const string test1 = "This is testvalue1;"
public const string test2 = "This is testvalue1;"
public const string test3 = "This is testvalue1;"
public const string test4 = "This is testvalue1;"
public const string test5 = "This is testvalue1;"
}
你必须通过反思来做到这一点
public class Constants
{
public const string test1 = "This is testvalue1;"
public const string test2 = "This is testvalue1;"
public const string test3 = "This is testvalue1;"
public const string test4 = "This is testvalue1;"
public const string test5 = "This is testvalue1;"
}
当然有可能。下面的代码段使用反射来循环一个类的所有常量
string fieldName = "test1";
object fieldValue = typeof(Constants).GetField(fieldName, BindingFlags.Static).GetValue(null);
当然有可能。下面的代码段使用反射来循环一个类的所有常量
string fieldName = "test1";
object fieldValue = typeof(Constants).GetField(fieldName, BindingFlags.Static).GetValue(null);
除了其他面向反思的答案外,您还可以使用字典:
internal class Program
{
public const string ConstExample = "constant value";
private static void Main(string[] args)
{
FieldInfo[] fieldInfos = typeof(Program).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
var constants = fieldInfos.Where(z => z.IsLiteral && !z.IsInitOnly).ToList();
foreach (var constant in constants)
{
string constantName = constant.Name;
object constantValue = constant.GetValue(null);
Console.WriteLine(string.Format("Constante name: {0}, constant value: {1}", constantName, constantValue));
}
Console.ReadKey();
}
}
公共字符串GetConstantValue(字符串键)
{
返回_常量[键];
}
私有字典常量=新字典()
{
{“test1”,“这是testvalue1;”},
{“test2”,“这是testvalue2;”},
{“test3”,“这是testvalue3;”},
{“test4”,“这是testvalue4;”},
{“test5”,“这是testvalue5;”},
};
然后使用
MyClass.GetConstantValue(“test1”)
获取常量值。除了其他面向反射的答案外,您还可以使用字典:
internal class Program
{
public const string ConstExample = "constant value";
private static void Main(string[] args)
{
FieldInfo[] fieldInfos = typeof(Program).GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
var constants = fieldInfos.Where(z => z.IsLiteral && !z.IsInitOnly).ToList();
foreach (var constant in constants)
{
string constantName = constant.Name;
object constantValue = constant.GetValue(null);
Console.WriteLine(string.Format("Constante name: {0}, constant value: {1}", constantName, constantValue));
}
Console.ReadKey();
}
}
公共字符串GetConstantValue(字符串键)
{
返回_常量[键];
}
私有字典常量=新字典()
{
{“test1”,“这是testvalue1;”},
{“test2”,“这是testvalue2;”},
{“test3”,“这是testvalue3;”},
{“test4”,“这是testvalue4;”},
{“test5”,“这是testvalue5;”},
};
然后使用MyClass.GetConstantValue(“test1”)获取常量值。这也可以:
public string GetConstantValue(string key)
{
return _constants[key];
}
private Dictionary<string, string> Constants = new Dictionary<string, string>()
{
{ "test1", "This is testvalue1;" },
{ "test2", "This is testvalue2;" },
{ "test3", "This is testvalue3;" },
{ "test4", "This is testvalue4;" },
{ "test5", "This is testvalue5;" },
};
这也可以起作用:
public string GetConstantValue(string key)
{
return _constants[key];
}
private Dictionary<string, string> Constants = new Dictionary<string, string>()
{
{ "test1", "This is testvalue1;" },
{ "test2", "This is testvalue2;" },
{ "test3", "This is testvalue3;" },
{ "test4", "This is testvalue4;" },
{ "test5", "This is testvalue5;" },
};
你可以用
List messages=newlist();
foreach(typeof(Constants).GetFields()中的FieldInfo字段,其中(f=>f.Name.StartsWith(“test”))
{
messages.Add(field.GetRawConstantValue().ToString());
}
您可以使用
List messages=newlist();
foreach(typeof(Constants).GetFields()中的FieldInfo字段,其中(f=>f.Name.StartsWith(“test”))
{
messages.Add(field.GetRawConstantValue().ToString());
}
为了获得良好的性能和可读性,我建议使用数组和枚举
List<string> messages = new List<string>();
foreach (FieldInfo field in typeof(Constants).GetFields().Where(f => f.Name.StartsWith("test")))
{
messages.Add(field.GetRawConstantValue().ToString());
}
请注意,所有变量的类型必须为string
。如果要混合和匹配数据类型,则可以使用object[]
而不是string[]
,但这样做成本更高,而且类型不安全
如果您只是在寻找一种方法来对某些属性进行分组,那么这比反射要好得多。为了获得良好的性能和可读性,我建议使用数组和枚举
public class Constants
{
public const string test1 = "This is testvalue1;"
public const string test2 = "This is testvalue1;"
public const string test3 = "This is testvalue1;"
public const string test4 = "This is testvalue1;"
public const string test5 = "This is testvalue1;"
}
List<string> messages = new List<string>();
foreach (FieldInfo field in typeof(Constants).GetFields().Where(f => f.Name.StartsWith("test")))
{
messages.Add(field.GetRawConstantValue().ToString());
}
请注意,所有变量的类型必须为string
。如果要混合和匹配数据类型,则可以使用object[]
而不是string[]
,但这样做成本更高,而且类型不安全
如果您只是在寻找一种方法来对某些属性进行分组,那么这比反射要好得多。为了实现这一点
public class Constants
{
public const string test1 = "This is testvalue1;"
public const string test2 = "This is testvalue1;"
public const string test3 = "This is testvalue1;"
public const string test4 = "This is testvalue1;"
public const string test5 = "This is testvalue1;"
}
class App
{
private enum ConstantsEnum: int
{
DefaultName = 0,
DefaultMode = 1,
DefaultStatus = 2
}
private readonly string[] Constants = new string[]{
"MyProgram",
"Test",
"Enabled" };
private void DoWork()
{
Console.WriteLine(Constants[ConstantsEnum.DefaultName]);
}
}
你可以这样做-
var p = Constants.Get("test1");//gives 'This is testvalue1'
为了得到这个,
class App
{
private enum ConstantsEnum: int
{
DefaultName = 0,
DefaultMode = 1,
DefaultStatus = 2
}
private readonly string[] Constants = new string[]{
"MyProgram",
"Test",
"Enabled" };
private void DoWork()
{
Console.WriteLine(Constants[ConstantsEnum.DefaultName]);
}
}
你可以这样做-
var p = Constants.Get("test1");//gives 'This is testvalue1'
您必须使用反射,但如果需要这样做,最好使用静态数组或字典,而不是单个常量。请解释为什么使用静态数组更好?请参阅vc 74的答案;-)反射速度较慢,在您的情况下是一种不必要的攻击,我认为。您必须使用反射,但如果您需要这样做,最好使用静态数组或字典,而不是单个常量。是否愿意解释为什么使用静态数组更好?请参阅vc 74的答案;-)我认为,在你的情况下,反射比较慢,是一种不必要的攻击。有没有办法扭转这种局面?所以我可以通过它的值来找到propertyname?已经找到了上面问题的答案:typeof.GetFields()。其中(m=>(string)m.GetValue(null)=“test1”).First();有没有办法扭转这种局面?所以我可以通过它的值来找到propertyname?已经找到了上面问题的答案:typeof.GetFields()。其中(m=>(string)m.GetValue(null)=“test1”).First();我看不出第一行有什么用。但是是的,它是有效的:)我真的看不出第一行的用途。但是的,它是有效的:)