如何使用反射获取 C# 静态类属性的名称? [英] How can I get the name of a C# static class property using reflection?

查看:65
本文介绍了如何使用反射获取 C# 静态类属性的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个 C# 字典,其中键是类中静态属性的字符串名称,值是属性的值.给定一个名为 MyResources.TOKEN_ONE 的类中的静态属性,我怎样才能获得属性的 name 而不是它的值?我只关心属性名称的结尾部分(例如TOKEN_ONE").

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").

我对此进行了编辑,以提及我不想映射字典中的所有属性值,只是类中所有内容的一小部分.所以假设我想获取单个属性的名称.鉴于 MyResources.TOKEN_ONE,我想取回MyResources.TOKEN_ONE"或只是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".

这是一些示例代码,显示了我正在尝试做什么.我需要属性名称,因为我正在尝试生成一个 javascript 变量,在该变量中我将属性名称映射到变量名称并将属性值映射到变量值.例如,我希望 C# 字典生成如下所示的行:

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 = "一";

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"; }
      }
   }
}

推荐答案

如果您只想在代码中的一个地方引用一个特定的属性,通过文字字符串引用它,然后您可以使用表达式树.例如,以下代码声明了一个将这样的表达式树转换为 PropertyInfo 对象的方法:

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天全站免登陆