Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all static properties and its values of a class using reflection

Tags:

c#

reflection

I hava a class like this:

public class tbl050701_1391_Fields {     public static readonly string StateName = "State Name";     public static readonly string StateCode = "State Code";     public static readonly string AreaName = "Area Name";     public static readonly string AreaCode = "Area Code";     public static readonly string Dore = "Period";     public static readonly string Year = "Year"; } 

I want to write some statement that returns a Dictionary<string, string> that has these values:

Key                            Value -------------------------------------------- "StateName"                    "State Name" "StateCode"                    "State Code" "AreaName"                     "Area Name" "Dore"                         "Period" "Year"                         "Year" 

I have this code for getting one property value:

public static string GetValueUsingReflection(object obj, string propertyName) {     var field = obj.GetType().GetField(propertyName, BindingFlags.Public | BindingFlags.Static);     var fieldValue = field != null ? (string)field.GetValue(null) : string.Empty;     return fieldValue; } 

How I can get all properties and their values?

like image 274
Arian Avatar asked Sep 18 '12 10:09

Arian


People also ask

What does GetValue return c#?

GetValue(Object) Returns the property value of a specified object.

How do you derive a static class?

The solution couldn't be simpler: public class A { public const string ConstantA = "constant_a"; public const string ConstantB = "constant_b"; // ... } public class B : A { public const string ConstantC = "constant_c"; public const string ConstantD= "constant_d"; // ... }

Can static properties be inherited?

Static Methods and InheritanceYou can use extends to create a subclass and still have access to the foo() function. You can also use static getters and setters to set a static property on the Base class. Unfortunately, this pattern has undesirable behavior when you subclass Base .

What are static properties and methods?

A static property or method acts somewhat like a globally accessible function or variable. Simply import the class, and you can read the same value from anywhere else in the program. It's usually not advisable to rely on global variables as they can lead to spaghetti code, but that's not to say that they can't be used.


1 Answers

how I can get all properties and their values?

Well to start with, you need to distinguish between fields and properties. It looks like you've got fields here. So you'd want something like:

public static Dictionary<string, string> GetFieldValues(object obj) {     return obj.GetType()               .GetFields(BindingFlags.Public | BindingFlags.Static)               .Where(f => f.FieldType == typeof(string))               .ToDictionary(f => f.Name,                             f => (string) f.GetValue(null)); } 

Note: null parameter is necessary for GetValue for this to work since the fields are static and don't belong to an instance of the class.

like image 146
Jon Skeet Avatar answered Oct 07 '22 23:10

Jon Skeet