lundi 2 mai 2022

how to know all generics type values in c#?

i have simple class like below :

public class HelathCheck<T>
    {
        public static Dictionary<string, CircuitBreakerPolicy<T>> pollyPolicies = new Dictionary<string, CircuitBreakerPolicy<T>>();
    }

i am adding value like below to this Policies dynamically.

HelathCheck<ClassA>.pollyPolicies.Add("SportsAPI1", Policy1);
HelathCheck<ClassB>.pollyPolicies.Add("SportsAPI2", Policy2);
HelathCheck<ClassC>.pollyPolicies.Add("SportsAPI3", Policy3);
HelathCheck<ClassD>.pollyPolicies.Add("SportsAPI4", Policy4);

i am storing some CircuitBreakerPolicy in Dictionary object so i can use get value later.

now i want to know all value which is stored into SoapPollyPolicies from one method

something like this, basically how do i know values store in all class?

can you please give me some hints? Thanks ( is this is right question?, not sure )

// here i do not want to pass classA, classB..etc, just one line and all values, i wanted to get it.
                foreach (var item in HelathCheck<??>.SoapPollyPolicies) 
                {
                    response.Add(item.Key, item.Value);
                }

may be something like this but only class ==>

https://expertcodeblog.wordpress.com/2018/02/07/c-get-all-types-that-implement-an-interface/

the closest i can reach to this is :

var type = typeof(HelathCheck<>);
            var types = AppDomain.CurrentDomain.GetAssemblies()
                .SelectMany(s => s.GetTypes())
                .Where(p => type.IsAssignableFrom(p));

            foreach (var item1 in types)
            {
                //do stuff
                var prop = item1.GetField("pollyPolicies "); // FaultResponse is one of object from SOAP resposne.

                //var value= prop.GetValue("pollyPolicies");

            }

Aucun commentaire:

Enregistrer un commentaire