mardi 11 mai 2021

DataSet and Field names via Reflection using Dictionary

I have a requirement to receive a list of DataSet.FieldName as strings and return a object of the Field value. Data strings come to me as "class.[encapsulatedclass 1].[encapsulatedclass n].FieldName i.e.

"Products.ProductCode" "Products.ProductGroup.GroupName" etc...

I have this which almost does the job ...ish. But my question is at the bottom of the example and is really about Dictionaries and is it possible to gain access to the fields of an encapsulted class directly from the Dictionary.

public class ProductGroup
{
    string GroupName{get; set;}
    string GroupDescription{get; set;}
    ProductGroup(string name, string desc)
    
}   
public class ProductDetails
{
    string ProductCode {get; set;}
    ProductGroup Group {get; set;}
    
    ProductDetails (string Name, ProductGroup fred)
    {
        ProductCode = Name;
        Group = fred;
    }
}

 public static void GetTypeMembers<T>(T xRec, ref Dictionary<string, object> dict)
        {
            Type fType;
            PropertyInfo[] fieldProperties = typeof(T).GetProperties();
            object obj = new object();
            string nameStr = string.Empty; 
            foreach (var fProperty in fieldProperties)
            {
                obj = fProperty.GetValue(xRec, null);
                nameStr = fProperty.Name;
                dict.Add(nameStr, obj);
            }
        }

Main()
{
    string[] WantedDetails = {"Products.ProductCode","Products.ProductGroup.Name"}
    List<object> DataToWorkWith.Add(DoIt(WantedDetails));
}

public object DoIt(string[] WantedDetails )
{
    ProductDetails bert = new ProductDetails("Stuff", new ProductGroup({"G1","Group1"}) )
    Dictionary<string, object> dictProducts = new Dictionary<string, object>();
    GetTypeMembers<ProductDetails>(bert, ref dictProducts);

    object obj = new object();
    string[] Fields = map.ProviderData.Split('+');      //{"Products.ProductCode","Products.ProductGroup.GroupName"}
    foreach (string field in Fields)
    {
        string[] Levels = field.Split('.');         
        int lcount = Levels.Length;

        if (Levels[0] == "Products" && lcount > 1)
        { 
            if (lcount == 2) //great to get "Products.ProductCode" returns "Stuff"
                obj = dictProducts.FirstOrDefault(x => x.Key == Levels[1]).Value;
            
            //but now for the GroupName which really a member of ProductGroup
            for (int i = 1; i < lcount; i++)
            {
                //but "Products.ProductGroup" returns fred a TypeOf(ProductGroup)
                //  GroupName is visible in the dictionary entry but is there a way
                //  of getting GroupName from the dictionary without having to 
                //  cast obj as a ProductGroup and go through refection again
                //  to get GroupName?
                //  we could have a required string of 
                //  "Products.ProductGroup.ClassEncapsulatedInProductGroup.....
                //     YetAnotherEncapsultedClass.FieldName"}
                obj = dictProducts.FirstOrDefault(x => x.Key == Levels[i]).Value;.....
            }
        }
        return obj
   }
}




Aucun commentaire:

Enregistrer un commentaire