Click here to Skip to main content
15,749,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Currently in my class DataEntries I have to sub class, UserObjects and TagObjects. I am trying to access the properties of TagObjects in my test class.. I have another class where these objects are serialized which is DataProvider class. I am using this Reflection helper but I am unable to pick the properties. My goal is to get the response from the API and place that response ID in the property I tell my refection helper assign it to. I succesfully get the response from the API, but I am unable to then place the response value in the property of my choice in the DataEntries class of TagObjects subclass property
C#
//Here is the class of DataEntries 
public class DataEntries
{
    public class UserObjects
    {
        public string UserId { get; set; }
        public string UserId2 { get; set; }
    }

    public class TagObjects
    {
        public int id { get; set; }
        public string name { get; set; }
        public int type { get; set; } 
        public TagObjects Child { get; set; }
    }
}


What I have tried:

C#
//Refection helper
public static class ReflectionHelper 
{
    public static dynamic GetPropValue(this Object obj, String propName)
    {
        string[] nameParts = propName.Split('.');
        if (nameParts.Length == 1)
        {
            return obj.GetType().GetProperty(propName).GetValue(obj, null);
        }
        
        foreach (String part in nameParts)
        {
            if (obj == null) { return null; }
        
            Type type = obj.GetType();
            PropertyInfo info = type.GetProperty(part);
            if (info == null) { return null; }
        
            obj = info.GetValue(obj, null);
        }
        return obj;
    }
    
}

Not sure if this will help up Here is my DataProvider class and method for to help place my query parameters when calling the API
C#
public class DataProvider 
{
    public DataEntries DataEntries { get; private set; }

    public DataProvider ()
    {
        DataEntries = new DataEntries();
    }

    public Dictionary<string, object> DataProviderQueryParams(dynamic matchKey)
    {
         DataEntries dataEntries = new DataEntries();
        string data = JsonConvert.SerializeObject(dataEntries);
        Dictionary<string, object> Items = JsonConvert.DeserializeObject<Dictionary<string, object>>(data);
        if (Items.TryGetValue(matchKey, out object value))
        {

            return new Dictionary<string, object> { { matchKey, value } };
        }


        return null;
    }
}

Test class usage
C#
public class TestClass
{
    DataProvider dataprovider = new DataProvider();

    [Test]
    public void GetTagList()
    {
        parsedResponse = JToken.Parse(HttpGet(baseUrl, TestUtil.BasePath(), token, client).Content);
        tagId = parsedResponse.Last["id"].ToString();
    
        var tagId = dataprovider.GetPropValue("DataEntries.id");
        // Getting TagId as Null..
    }
}
Posted
Updated 10-Jun-20 23:44pm
v2

1 solution

As shown, your DataEntries class doesn't have any properties, so there's nothing to drill down into.

Assuming you actually have some properties in there:
C#
public class DataEntries
{
    public class UserObjects
    {
        public string UserId { get; set; }
        public string UserId2 { get; set; }
    }

    public class TagObjects
    {
        public int id { get; set; }
        public string name { get; set; }
        public int type { get; set; } 
        public TagObjects Child { get; set; }
    }
    
    public UserObjects User { get; set; }
    public TagObjects Tag { get; set; }
}
then your reflection code will work as expected:
C#
var entry = new DataEntries
{
    User = new DataEntries.UserObjects
    {
        UserId = "42",
        UserId2 = "64",
    },
    Tag = new DataEntries.TagObjects
    {
        id = 42,
        name = "Ford",
        type = 11,
    },
};

var value = entry.GetPropValue("Tag.name");
Console.WriteLine(value);
// Output: Ford
Demo[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900