Search code examples
c#.netsharpdevelop

Get all the attributes name from a class in C#


I have a class to with name, c_invPrd with following code.

public  readonly string ITEM_NO                                     = "ITEM_NO";
public  readonly string PRD_INDEX                                   = "PRD_INDEX";
public  readonly string PRD_CD                                      = "PRD_CD";
public  readonly string PRD_DESC                                    = "PRD_DESC";
public  readonly string PRINCIPAL_IND                               = "PRINCIPAL_IND";
public  readonly string PROMO_IND                                   = "PROMO_IND";
public  readonly string PRDCAT1_CD                                  = "PRDCAT1_CD";
public  readonly string PRDCAT2_CD                                  = "PRDCAT2_CD";
public  readonly string PRDCAT3_CD                                  = "PRDCAT3_CD";
public  readonly string PRDCAT4_CD                                  = "PRDCAT4_CD";
public  readonly string PRDCAT5_CD                                  = "PRDCAT5_CD";
public  readonly string PRDCAT6_CD                                  = "PRDCAT6_CD";
public  readonly string PRDCAT7_CD                                  = "PRDCAT7_CD";
public  readonly string PRDCAT8_CD                                  = "PRDCAT8_CD";
public  readonly string PRDCAT9_CD                                  = "PRDCAT9_CD";
public  readonly string PRDCAT10_CD                                 = "PRDCAT10_CD";


private readonly Type   typeof_ITEM_NO                              = typeof(int);
private readonly Type   typeof_PRD_INDEX                            = typeof(int);
private readonly Type   typeof_PRD_CD                               = typeof(string);
private readonly Type   typeof_PRD_DESC                             = typeof(string);
private readonly Type   typeof_PRINCIPAL_IND                        = typeof(string);
private readonly Type   typeof_PROMO_IND                            = typeof(string);
private readonly Type   typeof_PRDCAT1_CD                           = typeof(string);
private readonly Type   typeof_PRDCAT2_CD                           = typeof(string);
private readonly Type   typeof_PRDCAT3_CD                           = typeof(string);
private readonly Type   typeof_PRDCAT4_CD                           = typeof(string);
private readonly Type   typeof_PRDCAT5_CD                           = typeof(string);
private readonly Type   typeof_PRDCAT6_CD                           = typeof(string);
private readonly Type   typeof_PRDCAT7_CD                           = typeof(string);
private readonly Type   typeof_PRDCAT8_CD                           = typeof(string);
private readonly Type   typeof_PRDCAT9_CD                           = typeof(string);
private readonly Type   typeof_PRDCAT10_CD                          = typeof(string);


public c_invIndPrd(DMSFW.Objects.Argument arg)
    : base (arg) {
}

public c_invIndPrd()
{

}

How can I create a List C# to put all the attributes like ITEM_NO, PRD_INDEX, PRD_CD, PRD_DESC, PRINCIPAL_IND and so on into a list by accessing c_invPrd class in following? I am using sharpdeveloper as my IDE.


Solution

  • This code looks very very odd - it looks like you want some private fields, but just the string ones:

    var fields = typeof(c_invPrd).GetFields(
            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
         .Where(f => f.FieldType == typeof(string)).ToArray();
    

    If you want just the names:

    var fields = typeof(c_invPrd).GetFields(
            BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
         .Where(f => f.FieldType == typeof(string)).Select(f => f.Name).ToArray();
    

    If you want the values then you'll need an instance of the type, then use FieldInfo.GetValue:

    c_invPrd obj = ...
    var fields = typeof(c_invPrd).GetFields(
              BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)
         .Where(f => f.FieldType == typeof(string))
         .Select(f => (string)f.GetValue(obj)).ToArray();
    

    Frankly this code is just baffling. Either those things should be const (for the string) or static readonly (for the Type) - or they should be regular properties, i.e.

    public int ITEM_NO {get;set;} // I'd use ItemNumber if it was me, but...
    public string PRD_CD {get;set;} // I'm going to guess... ProductCode?