我怎样才能得到使用反射一个C#静态类属性的名字吗? [英] How can I get the name of a C# static class property using reflection?

查看:98
本文介绍了我怎样才能得到使用反射一个C#静态类属性的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个C#字典,其中最关键的是在一个类的静态属性的字符串名称和值的属性的值。给定一个静态属性称为MyResources.TOKEN_ONE类的,我怎么能得到在的名称的属性,而不是它的价值?我只关心属性名的端部(例如TOKEN_ONE)。



我已经编辑这个一提的是,我不想映射的所有的字典中的属性值,一切的在类的只是一小部分。因此,假设我想为一个单一的财产获得的名称。鉴于MyResources.TOKEN_ONE,我要回去MyResources.TOKEN_ONE或只是TOKEN_ONE。



下面是一些示例代码,显示了我想要做的。我需要的属性名称,因为我试图生成我的属性名映射到变量名和属性值的变量值的javascript变量。例如,我想C#字典生成类似下面的行:



VAR TOKEN_ONE =一;

 使用系统; 
使用System.Collections.Generic;

命名空间MyConsoleApp
{
类节目
{
静态无效的主要(字串[] args)
{
字典<字符串,字符串> KVP =新词典<字符串,字符串>();

//我如何使用静态属性的名称中的一类作为字典的关键?
//例如,我想要做类似下面的其中'PropertyNameFromReflection'
//是,将返回MyResources.TOKEN_ONE
kvp.Add(为MyResources的机制。 TOKEN_ONE.PropertyNameFromReflection,MyResources.TOKEN_ONE);
kvp.Add(MyResources.TOKEN_TWO.PropertyNameFromReflection,MyResources.TOKEN_TWO);

到Console.ReadLine();
}
}

公共静态类为MyResources
{
公共静态字符串TOKEN_ONE
{
{返回一 ; }
}

公共静态字符串TOKEN_TWO
{
{返回二; }
}
}
}


解决方案

如果你想要的是只是为了能够引用一个单一的,特定的属性在一个地方在代码的没有的具有由一个字符串来引用它,那么你可以使用表达式树。例如,下面的代码声明,变成这样一个表达式树成一个PropertyInfo对象的方法:

 公共静态的PropertyInfo的getProperty(表达式来;函数功能:LT;串GT;>表达式)
{
VAR成员= expr.Body为MemberExpression;
如果(成员== NULL)
抛出新的InvalidOperationException异常(表达式不是一个成员访问的表情。);
VAR财产= member.Member为的PropertyInfo;
如果(财产== NULL)
抛出新的InvalidOperationException异常(成员表达不是一个性质。);
返回财产;
}

现在你可以做这样的事情:

 公共无效AddJavaScriptToken(表达式来; Func键<串>> propertyExpression)
{
变种p =的getProperty(propertyExpression);
_javaScriptTokens.Add(p.Name,(串)p.GetValue(NULL,NULL));
}

公共无效RegisterJavaScriptTokens()
{
AddJavaScriptToken(()=> Tokens.TOKEN_ONE);
AddJavaScriptToken(()=> Tokens.TOKEN_TWO);
}


I want to make a C# Dictionary in which the key is the string name of a static property in a class and the value is the value of the property. Given a static property in the class called MyResources.TOKEN_ONE, how can I get the at the name of the property rather than its value? I only care about the end part of the property name (e.g. "TOKEN_ONE").

I've edited this to mention that I don't want to map all property values in the Dictionary, just a small subset of everything that's in the class. So assume that I want to get the name for a single property. Given MyResources.TOKEN_ONE, I want to get back "MyResources.TOKEN_ONE" or just "TOKEN_ONE".

Here's some sample code that shows what I'm trying to do. I need the property name because I'm trying to generate a javascript variable in which I map the property name to the variable name and the property value to the variable value. For example, I want the C# Dictionary to generate lines like the one below:

var TOKEN_ONE = "One";

using System;
using System.Collections.Generic;

namespace MyConsoleApp
{
   class Program
   {
      static void Main(string[] args)
      {
         Dictionary<String, String> kvp = new Dictionary<String, String>();

         // How can I use the name of static property in a class as the key for the dictionary?
         // For example, I'd like to do something like the following where 'PropertyNameFromReflection'
         // is a mechanism that would return "MyResources.TOKEN_ONE"
         kvp.Add(MyResources.TOKEN_ONE.PropertyNameFromReflection, MyResources.TOKEN_ONE);
         kvp.Add(MyResources.TOKEN_TWO.PropertyNameFromReflection, MyResources.TOKEN_TWO);

         Console.ReadLine();
      }
   }

   public static class MyResources
   {
      public static string TOKEN_ONE
      {
         get { return "One"; }
      }

      public static string TOKEN_TWO
      {
         get { return "Two"; }
      }
   }
}

解决方案

If all you want is just to be able to refer to a single, specific property in one place in the code without having to refer to it by a literal string, then you can use an expression tree. For example, the following code declares a method that turns such an expression tree into a PropertyInfo object:

public static PropertyInfo GetProperty(Expression<Func<string>> expr)
{
    var member = expr.Body as MemberExpression;
    if (member == null)
        throw new InvalidOperationException("Expression is not a member access expression.");
    var property = member.Member as PropertyInfo;
    if (property == null)
        throw new InvalidOperationException("Member in expression is not a property.");
    return property;
}

Now you can do something like this:

public void AddJavaScriptToken(Expression<Func<string>> propertyExpression)
{
    var p = GetProperty(propertyExpression);
    _javaScriptTokens.Add(p.Name, (string) p.GetValue(null, null));
}

public void RegisterJavaScriptTokens()
{
    AddJavaScriptToken(() => Tokens.TOKEN_ONE);
    AddJavaScriptToken(() => Tokens.TOKEN_TWO);
}

这篇关于我怎样才能得到使用反射一个C#静态类属性的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆