Let's say I have the following static class:
public static class Foo<T>
{
private static T Baz;
/* other members */
public static void Bar()
{
//do something with Baz
}
}
Is there a built-in way to call Bar()
for all specific versions of the generic class without knowing the type parameters that were used? That is, if the consumer of Foo<T>
has used it as Foo<Larry>
, Foo<Curly>
and Foo<Moe>
, how can I call all the Foo<Larry>.Bar()
, Foo<Curly>.Bar()
etc. methods automatically without explicitly specifying the generic types?
The number of possibilities for T is infinite. There is no way to know what might be used for T. You can, using the static constructor, track the types that have already been used for T and perform an operation against all of those types.
Here I created a class with a static variable containing a list of Actions.
Action
matches the signature ofvoid Bar()
, so to call a different signature, use a different type i.e.Action<string>
to callvoid Bar(string x)
orFunc<int,string>
to callstring Bar(int val)
.If you use the code below to call this, you will observe that
Foo<Curly>.Bar()
isn't called because the static constructor isn't run until after theCallAllBars()
call.That is not possible. Use the static constructor of
Foo<T>
to maintain a global list ofBaz
values. That global list must be rooted in a static variable that lives outside ofFoo<T>
.