1

Ok, I try to explain my question a bit better: I have a

class DynamicTypeCreator{...}

and a method in it:

static BuildASingletonClassWithTypeBuiler<T> (T object) {...}

This method not only creates a singleton type

class Singleton01<K> {...}

but also instantiates it, defining the field

static Singleton01<K> instance = new Singleton01<K> (K object)

and another field

static Type typeOfGenericParameter = typeof(K).

(The actual code on https://github.com/TThaan/ExtensionPropertiesForCSharp is different but that should be of no concern.)
However this method does not return the singleton type nor the instance. It's just creating them.
Plenty of times, indeed, only changing the name of the type from Singleton01 to Singleton02, Singleton03 etc and each time with a different T object as parameter.
So when I want to access one of those singletons later I don't know what object it holds and of what type it is. To get the value of any static field in eg "Singleton03" I first use

Type type = AssemblyBuilder.GetType("Singleton03").

Trying to get a value of a static field now does not work yet, it's just null. I seem to be forced to use

Type genericType = type.MakeGenericType(new[] { typeof(T) }).

But for this to work I already need to know the type of my parameter T object.
I want to know if there is a way to get the value of a static field in such a generic type before using .MakeGeneric().

Konrad
  • 11
  • 4
  • I believe what you want is a type initializer for initializing static fields from [this answer](https://stackoverflow.com/a/30355711/2557128). – NetMage Nov 21 '18 at 19:57
  • Thanks for your effort. If you suggest I should use a static constructor for my singleton, I don't see how this would solve my problem. I edited my question in hope to make myself clearer. – Konrad Nov 22 '18 at 15:52
  • Maybe you should say what your final goal is first. Your question is hard to read since you say what you do but not for what your final goal is. – Benoit Sanchez Nov 22 '18 at 20:36
  • I want to make extension properties. I dynamically create an assembly with generic singletons, each including a field with an object of this generic type (my "extended" object), a static field storing this Type and the extension properties. Later if I work with an object "to be extended" I use only a reference to this singleton's "object field". I have no permanent variable for an instance of the singleton. So I have to access the singleton and it's properties by first getting an instance of the singleton type, what is impossible without getting the generic type from it's "Type field" first. – Konrad Nov 23 '18 at 16:28

1 Answers1

0

I found an answer now.
Since a generic class of one type is actually a different class then "the same class" of another type,
the static field in one of those classes is different from that in the other class. Therefore I can't get the value of one of those fields before using .MakeGeneric() with the fitting type.
The solution is to change the creation of my class, eg like here: Generic classes and static fields.

Konrad
  • 11
  • 4