Home > Net >  how to know all generics type values in c#?
how to know all generics type values in c#?

Time:05-03

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");

            }

CodePudding user response:

You do not need generics here. You say they are a common base class so do

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

and put all the obects in that one dictioanry

CodePudding user response:

The most accurate way of doing this would be to create an interface for your test classes, and have each test class implement that interface.

public interface ITestClass { }

public class TestClassA : ITestClass
{
}

public class TestClassB : ITestClass
{
}

In your HealthCheck class, add a type constraint:

public class HealthCheck<T> where T : ITestClass
{
    public static Dictionary<string, T> Policies { get; set; }        
}

Now, you can then enumerate over the added items using ITestClass as your type parameter like so:

public class Consumer
{
    public Consumer()
    {
        HealthCheck<TestClassA>.Policies.Add("API1", new TestClassA());
        HealthCheck<TestClassB>.Policies.Add("API2", new TestClassB());

        foreach (var policy in HealthCheck<ITestClass>.Policies)
        {
            Console.WriteLine(policy.Key);
            Console.WriteLine(policy.Value);
        }
    }
}
  • Related