LoneXcoder
LoneXcoder

Reputation: 2163

get list of filelds from of any given Class (Type) as a passed parameter

i would like to convert a method that already does the job

(only if you instantiate the class within its body)

to one that will accept the subjected class as a passed parameter...somehow.

i have tried to get that result by my self by trial and error , (mostly trial ..and errors , lots...)

but no success (:

this first class is within my helpers classes (the extensions namespace)

public static List<string> AnyClassFieldsValuesAsList<T>(this T Clss, string nestedName)
{
    // i know how to get this to work... if an instace of the class is declared 
    // only here i would like to have any given class...as passed parameter
    // couple of tests to get a hold of the passed Class...no success (:
    var T1 = typeof(T.GetType());
    var T2 = Clss.GetType();

    return typeof(Clss.GetType()).GetFields(); //ToList<string>();
}

this is the subjected class (that i have stored in same helpers file) to hold strings representing style-fonts

public class FontNames 
{
    public readonly string Aharoni = "Aharoni",
                           Andalus = "Andalus",
                           AngsanaNew = "Angsana New",
                           AngsanaUPC = "AngsanaUPC",
                           Aparajita = "Aparajita";
                           //.....etc'
}

now within the code behind of the current project i want to be able to do something like

//imports of extensions and my style namespaces ....
// then somewhere after Page_Load()...

var instnceOfFnames  = new FontNames();
list<string> FontsLst= instnceOfFnames.AnyClassFieldsValuesAsList(.....);

parameters in signature of AnyClassFieldsValuesAsList() at the top are just for testing,

as i couldn't work with them, i am not sure that they are the ones that i should pass.

what is the correct syntax to achive the results ?

Upvotes: 0

Views: 98

Answers (1)

horgh
horgh

Reputation: 18563

As far as I understood, you need to get the fields' values without an instanse of the class. I suppose, you should declare those fields as public readonly static .... Then you'll be able to utilize the following method:

public static IEnumerable<string> GetFields<T>()
{
    Type type = typeof(T);
    return type.GetFields(BindingFlags.Static | BindingFlags.Public)
                .Where(f => f.FieldType == typeof(string))
                .Select(f => (string)f.GetValue(null));
}

like this:

foreach (string f in GetFields<FontNames>())
     Console.WriteLine(f);

Based on comments:

For the current issue (as far as I understand it), it seems to be superfluos, as static fields do not need instances to be accessed. But at least it can give some ideas

public static IEnumerable<string> GetFields<T>(this T value)
{
    Type type = value.GetType();
    //...all the same as above
}

To get the same result it's enough to have the same without generics

public static IEnumerable<string> GetFields(this object value)
{
    Type type = value.GetType();
    //...all the same as above
}

Upvotes: 2

Related Questions