Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get fields and their values from a static class in referenced assembly

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

A7 like this:

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

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

thanks

like image 759
Arian Avatar asked Sep 07 '11 12:09

Arian


People also ask

How do you access a static field in a class?

Static public fields are added to the class constructor at the time of class evaluation using Object. defineProperty() . The static fields and methods can be accessed from the class itself.

Can a static class have fields?

Static MembersA non-static class can contain static methods, fields, properties, or events. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name.

Is static class reference type?

Yes, static classes are considered as reference types as when you change a StaticClass. Property value within a method, this change will get populated everywhere you reference this class.


1 Answers

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();     } } 
like image 70
Adam Houldsworth Avatar answered Sep 27 '22 21:09

Adam Houldsworth