C#动态获取变量名和值在A级 [英] C# Dynamically Get Variable Names and Values in a Class

查看:100
本文介绍了C#动态获取变量名和值在A级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前做节目(C#.NET 4)具有多个选项,这些选项保存到一个文件。



这些选项都是自己的变量IN-代码,我想知道是否有办法让这些选项在代码中动态的变量和值。



在我的情况,我有一个这些选项 。设置类,我从我的主窗体类使用Settings.varSetting访问它们。



我获取和设置在代码中多个地方这些变量;是有可能巩固变量列表,这样我可以访问并设置它们(例如,创建一个设置形成拉可用的选项和他们的价值观和动态画的形式)更容易/始终如一?



下面是当前变量我在设置类:

 公共静态乌里uriHomePage =新的URI(http://www.google.com); 
公共静态INT intInitOpacity = 100;
公共静态字符串strWindowTitle =OpaciBrowser;
公共静态布尔boolSaveHistory = FALSE;
公共静态布尔boolAutoRemoveTask = TRUE; //自动从任务栏中删除窗口,如果下:
公共静态INT intRemoveTaskLevel = 50; //%的
公共静态布尔boolHideOnMinimized = TRUE;



感谢您的帮助,



卡尔Tatom(TheMusiKid)


解决方案

  VAR字典= typeof运算(设置)
。 GetFields(BindingFlags.Static | BindingFlags.Public)
.ToDictionary(F => f.Name,F => f.GetValue(NULL));


I am currently making a program (C# .Net 4) that has multiple options, which are saved to a file.

These options are their own variables in-code, and I was wondering if there was a way to get the variables and values of these options dynamically in code.

In my case, I have these options in a "Settings" class, and I access them from my main form class using Settings.varSetting.

I get and set these variables in multiple places in code; is it possible to consolidate the list of variables so that I can access and set them (for example, creating a Settings form which pulls the available options and their values and draws the form dynamically) more easily/consistently?

Here are the current variables I have in the Settings class:

    public static Uri uriHomePage = new Uri("http://www.google.com");
    public static int intInitOpacity = 100;
    public static string strWindowTitle = "OpaciBrowser";
    public static bool boolSaveHistory = false;
    public static bool boolAutoRemoveTask = true; //Automatically remove window from task bar if under:
    public static int intRemoveTaskLevel = 50; //percent
    public static bool boolHideOnMinimized = true;

Thanks for any help,

Karl Tatom ( TheMusiKid )

解决方案

var dict = typeof(Settings)
    .GetFields(BindingFlags.Static | BindingFlags.Public)
    .ToDictionary(f=>f.Name, f=>f.GetValue(null));

这篇关于C#动态获取变量名和值在A级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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