如何从引用程序集中的静态类获取字段及其值 [英] How to get fields and their values from a static class in referenced assembly

查看:81
本文介绍了如何从引用程序集中的静态类获取字段及其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个命名为 A7的精密程序集(名为 DAL )中有一个静态类:

I have a static class in a refrenced assembly(named "DAL") named "A7":

A7像这样:

public static class A7
{
public static readonly bool NeedCoding = false;
public static readonly string Title = "Desc_Title"
public static readonly string F0 = "";
public static readonly string F1 = "Desc_F1";
public static readonly string F2 = "Desc_F2";
public static readonly string F3 = "Desc_F3";
public static readonly string F4 = "Desc_F4";
}

如何从 DAL获取所有属性的名称和值

How I can get All Properties name and values from DAL assemby A7 class?

谢谢

推荐答案

使用反射,您将需要查找字段;这些不是属性。从下面的代码中可以看到,它查找公共静态成员:

Using reflection, you will need to look for fields; these are not properties. As you can see from the following code, it looks for public static members:

class Program
{
    static void Main(string[] args)
    {
        Type t = typeof(A7);
        FieldInfo[] fields = t.GetFields(BindingFlags.Static | BindingFlags.Public);

        foreach (FieldInfo fi in fields)
        {
            Console.WriteLine(fi.Name);
            Console.WriteLine(fi.GetValue(null).ToString());
        }

        Console.Read();
    }
}

这篇关于如何从引用程序集中的静态类获取字段及其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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