怎么获取enum的变量名称

怎么获取enum的变量名称,第1张

将枚举的值转换为对应的名字

inthashCode = ColorRedGetHashCode(); stringenumParseStr = EnumParse(typeof(Color), hashCodeToString())ToString();EnumParse()得到的值是object类型的,我们要转换的是值,得到的是名字,因此ToString()得到名字。

将枚举的名字转换为对应的值

stringstr = ColorRedToString(); intenumParseInt = ConvertToInt32(EnumParse(typeof(Color), str));这里要转换的是名字,得到的是值,因此用ConvertToInt32()得到该值。

局部变量在方法栈帧中根本没有名字,只有偏移地址。变量名是给程序员看的。

如果你了解了这点,你就知道你的要求是从原理上不可能实现的。

当然,你可以封装一个类:

class MyArray {

private String name;

public double[] array;

public MyArray(String name, double[] array){

thisname = name;

thisarray = array;

}

public String toString() {

return thisname + "的内容是:" + thisarray;

}

}

然后输出这个类的对象:

MyArray a = new MyArray("array1", new double[]{01, 02, 03});

Systemoutprint(a);

 class Test

        {

            public static string StaticPer = "StaticPer";

            public readonly string ReadOnlyPer = "ReadOnlyPer";

            public const string ConstPer = "ConstPer";

            public string GetPer { get { return "GetPer"; } }

            private string _SetPer;

            public string SetPer { set { _SetPer = value; } }

            public string GetSetPer { get; set; }

            public void Ok()

            {

            }

        }

        

        

        

        var obj = new Test();

            var type = objGetType();

            var p = typeGetProperties(BindingFlagsInstance | BindingFlagsPublic);

            foreach (var item in p)

            {

                ConsoleWriteLine("Name: {0}", itemName);

            }

            foreach (FieldInfo field in typeGetFields())

            {

                ConsoleWriteLine("Field: {0}, Value:{1}", fieldName, fieldGetValue(obj));

            }

using System;

using SystemCollectionsGeneric;

using SystemLinq;

using SystemText;

namespace GetVaribleName

{

    public class Program

    {

        public string str = "spp";

        public string spp = "Hello World!";

        public static void Main(string[] args)

        {

            Program p = new Program();

            ConsoleWriteLine(pGetType()GetField(pstr)ToString());//、这里就获得了 str 的变量名

            ConsoleReadKey();

        }

    }

}

以上就是关于怎么获取enum的变量名称全部的内容,包括:怎么获取enum的变量名称、Java是否有办法获取一个方法传入的对象的名字、c#中的静态变量,怎么通过反射的方式获取指定变量名称的值呢比如说一个const类型的常量:con等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9803697.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存