mercredi 30 mars 2022

Trouble creating a List of properties of a certain type from a class using reflection

I have a class with (at the present time) 3 Azure CloudTable properties. I am trying to create a private method to return a list of properties of type CloudTable as there are times where I need to grab commission and fee information from all of them

Main Class

public class EquityAndOptionDataRetrievalManager
{
    private readonly IAzureStorageService _azureStorageService;

    public EquityAndOptionDataRetrievalManager(IAzureStorageService azureStorageService)
    {
        _azureStorageService = azureStorageService;
    }

    // properties

    public CloudTable EquityTradeRecordsTable => AzureHelpers.GetCloudTable(_azureStorageService.PrimaryConnectionString, Common.StorageTable.EquityTradeRecordsTable);

    public CloudTable OptionTradeRecordsTable => AzureHelpers.GetCloudTable(_azureStorageService.PrimaryConnectionString, Common.StorageTable.OptionTradeRecordsTable);

    public CloudTable SymbolsTradedTable => AzureHelpers.GetCloudTable(_azureStorageService.PrimaryConnectionString, Common.StorageTable.InteractiveSymbolsTraded);

...rest removed for brevity

and I am trying to do something like this

private List<CloudTable> AzureTables()
{
    var tables = new List<CloudTable>();
    var obj = new EquityAndOptionDataRetrievalManager(_azureStorageService);
    var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
    foreach (var p in properties)
    {
        if (p.GetType() == typeof(CloudTable))
        {
            tables.Add(p.GetValue(obj,null) as CloudTable);
        }
    }

    return tables;
}

but in the tables.Add(p) and using p. to see all the available properties and methods, I cannot find anything that works. Is this not doable?

Note: Per MakePeaceGreatAgain's comments

I added a property to test the method in the class

public List AzureTableList { get; set; }

and I'm calling, just to test it It is failing, because the if inside the foreach loop never captures the CloudTable properties, even when they actually are CloudTable properties!

It's the right type, but it exits the if statement

enter image description here





Aucun commentaire:

Enregistrer un commentaire